Skip to content

Commit

Permalink
fix: Fix data for PSQL transfer (#1747)
Browse files Browse the repository at this point in the history
* fix: Remove invalid attempts and snake case-ify field names

* Remove tester code

* Also rename traffic lights field

* Also level decor

* Move level decor helper method to migrations

* Fix key
  • Loading branch information
faucomte97 authored Nov 1, 2024
1 parent 3677c63 commit 0c25ab9
Show file tree
Hide file tree
Showing 32 changed files with 248 additions and 114 deletions.
2 changes: 1 addition & 1 deletion game/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AttemptAdmin(admin.ModelAdmin):

class LevelDecorAdmin(admin.ModelAdmin):
search_fields = ["level__name"]
list_display = ["id", "level", "x", "y", "decorName"]
list_display = ["id", "level", "x", "y", "decor_name"]


admin.site.register(Level, LevelAdmin)
Expand Down
4 changes: 2 additions & 2 deletions game/end_to_end_tests/test_cow_crashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setUpClass(cls):
TestCowCrashes.cow_level = Level(
name="Cow crashing",
anonymous=False,
blocklyEnabled=True,
blockly_enabled=True,
character=van,
cows='[{"minCows":"7","maxCows":"7","potentialCoordinates":[{"x":4,"y":4},{"x":2,"y":4},{"x":3,"y":7},{"x":4,"y":6},{"x":2,"y":6},{"x":3,"y":1},{"x":4,"y":2}],"type":"WHITE"}]',
default=False,
Expand All @@ -57,7 +57,7 @@ def setUpClass(cls):
model_solution="[1]",
origin='{"coordinate":[2,5],"direction":"E"}',
path='[{"coordinate":[2,5],"connectedNodes":[1]},{"coordinate":[3,5],"connectedNodes":[0,4,2,5]},{"coordinate":[4,5],"connectedNodes":[1]},{"coordinate":[3,7],"connectedNodes":[4]},{"coordinate":[3,6],"connectedNodes":[8,3,6,1]},{"coordinate":[3,4],"connectedNodes":[10,1,11,16]},{"coordinate":[4,6],"connectedNodes":[4,7]},{"coordinate":[4,7],"connectedNodes":[6]},{"coordinate":[2,6],"connectedNodes":[9,4]},{"coordinate":[2,7],"connectedNodes":[8]},{"coordinate":[2,4],"connectedNodes":[13,5,12]},{"coordinate":[4,4],"connectedNodes":[5,14,15]},{"coordinate":[2,3],"connectedNodes":[10]},{"coordinate":[1,4],"connectedNodes":[10]},{"coordinate":[5,4],"connectedNodes":[11]},{"coordinate":[4,3],"connectedNodes":[11,19]},{"coordinate":[3,3],"connectedNodes":[5,17]},{"coordinate":[3,2],"connectedNodes":[18,16,19,20]},{"coordinate":[2,2],"connectedNodes":[17]},{"coordinate":[4,2],"connectedNodes":[17,15,23,22]},{"coordinate":[3,1],"connectedNodes":[21,17,22]},{"coordinate":[2,1],"connectedNodes":[20]},{"coordinate":[4,1],"connectedNodes":[20,19]},{"coordinate":[5,2],"connectedNodes":[19]}]',
pythonEnabled=False,
python_enabled=False,
theme=grass,
threads=1,
traffic_lights="[]",
Expand Down
4 changes: 2 additions & 2 deletions game/end_to_end_tests/test_python_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_animal_sound_horn(self):
animal_level = Level(
name="Animal commands",
anonymous=False,
blocklyEnabled=False,
blockly_enabled=False,
character=van,
cows='[{"minCows":1,"maxCows":1,"potentialCoordinates":[{"x":3,"y":4}],"type":"WHITE"}]',
default=False,
Expand All @@ -55,7 +55,7 @@ def test_animal_sound_horn(self):
model_solution="",
origin='{"coordinate":[2,4],"direction":"E"}',
path='[{"coordinate":[2,4],"connectedNodes":[1]},{"coordinate":[3,4],"connectedNodes":[0,2]},{"coordinate":[4,4],"connectedNodes":[1]}]',
pythonEnabled=True,
python_enabled=True,
theme=grass,
threads=1,
traffic_lights="[]",
Expand Down
19 changes: 11 additions & 8 deletions game/level_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def get_decor(level):
"""Helper method parsing decor into a dictionary format 'sendable' to javascript."""
decorData = []
for ld in LevelDecor.objects.filter(level=level):
decor = get_decor_element(name=ld.decorName, theme=level.theme)
decor = get_decor_element(name=ld.decor_name, theme=level.theme)
decorData.append(
{
"x": int(ld.x),
"y": int(ld.y),
"z": int(decor.z_index),
"decorName": str(ld.decorName),
"decorName": str(ld.decor_name),
"width": int(decor.width),
"height": int(decor.height),
"url": str(decor.url),
Expand All @@ -81,7 +81,10 @@ def set_decor_inner(level, decor, LevelDecor):
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data["decorName"]
level_id=level.id,
x=data["x"],
y=data["y"],
decor_name=data["decorName"],
)
)
LevelDecor.objects.bulk_create(level_decors)
Expand Down Expand Up @@ -160,9 +163,9 @@ def save_level(level, data):
level.max_fuel = data["max_fuel"]
level.traffic_lights = data["traffic_lights"]
level.cows = data["cows"]
level.blocklyEnabled = data.get("blocklyEnabled", True)
level.pythonEnabled = data.get("pythonEnabled", False)
level.pythonViewEnabled = data.get("pythonViewEnabled", False)
level.blockly_enabled = data.get("blockly_enabled", True)
level.python_enabled = data.get("python_enabled", False)
level.python_view_enabled = data.get("python_view_enabled", False)
level.theme = get_theme_by_pk(pk=data["theme"])
level.character = get_character_by_pk(pk=data["character"])
level.disable_algorithm_score = data.get("disable_algorithm_score", False)
Expand Down Expand Up @@ -203,6 +206,6 @@ def email_new_custom_level(
"STUDENT_NAME": student_name,
"CLASS_NAME": class_name,
"LEVEL_URL": level_url,
"MODERATE_URL": moderate_url
}
"MODERATE_URL": moderate_url,
},
)
17 changes: 16 additions & 1 deletion game/migrations/0001_squashed_0025_levels_ordering_pt1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db import models, migrations
from django.conf import settings
import json
from game.level_management import set_decor_inner, set_blocks_inner
from game.level_management import set_blocks_inner


# Functions from the following migrations need manual copying.
Expand Down Expand Up @@ -2527,6 +2527,21 @@ def delete_old_limit_level(apps, schema_editor):
old_limit_level.delete()


def set_decor_inner(level, decor, LevelDecor):
"""Helper method creating LevelDecor objects given a list of decor in dictionary form."""
LevelDecor.objects.filter(level=level).delete()

level_decors = []
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data[
"decorName"]
)
)
LevelDecor.objects.bulk_create(level_decors)


# Add episode 7 to 9
# Add levels 62, 51, 59, 57, 60, 61, 70
def add_episode_7_to_9(apps, schema_editor):
Expand Down
15 changes: 13 additions & 2 deletions game/migrations/0026_levels_pt2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals

from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
from game.level_management import set_blocks_inner
import json


Expand All @@ -13,7 +13,18 @@ def change_levels(apps, schema_editor):
Block = apps.get_model("game", "Block")

def set_decor(level, decor):
set_decor_inner(level, decor, LevelDecor)
"""Helper method creating LevelDecor objects given a list of decor in dictionary form."""
LevelDecor.objects.filter(level=level).delete()

level_decors = []
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data[
"decorName"]
)
)
LevelDecor.objects.bulk_create(level_decors)

def set_blocks(level, blocks):
set_blocks_inner(level, blocks, LevelBlock, Block)
Expand Down
15 changes: 13 additions & 2 deletions game/migrations/0032_cannot_turn_left_level.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
from game.level_management import set_blocks_inner
import json


Expand All @@ -12,7 +12,18 @@ def new_level(apps, schema_editor):
Block = apps.get_model("game", "Block")

def set_decor(level, decor):
set_decor_inner(level, decor, LevelDecor)
"""Helper method creating LevelDecor objects given a list of decor in dictionary form."""
LevelDecor.objects.filter(level=level).delete()

level_decors = []
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data[
"decorName"]
)
)
LevelDecor.objects.bulk_create(level_decors)

def set_blocks(level, blocks):
set_blocks_inner(level, blocks, LevelBlock, Block)
Expand Down
15 changes: 13 additions & 2 deletions game/migrations/0033_recursion_level.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
from game.level_management import set_blocks_inner
import json


Expand All @@ -15,7 +15,18 @@ def new_level(apps, schema_editor):
Block = apps.get_model("game", "Block")

def set_decor(level, decor):
set_decor_inner(level, decor, LevelDecor)
"""Helper method creating LevelDecor objects given a list of decor in dictionary form."""
LevelDecor.objects.filter(level=level).delete()

level_decors = []
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data[
"decorName"]
)
)
LevelDecor.objects.bulk_create(level_decors)

def set_blocks(level, blocks):
set_blocks_inner(level, blocks, LevelBlock, Block)
Expand Down
15 changes: 13 additions & 2 deletions game/migrations/0034_joes_level.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
from game.level_management import set_blocks_inner
import json


Expand All @@ -12,7 +12,18 @@ def new_level(apps, schema_editor):
Block = apps.get_model("game", "Block")

def set_decor(level, decor):
set_decor_inner(level, decor, LevelDecor)
"""Helper method creating LevelDecor objects given a list of decor in dictionary form."""
LevelDecor.objects.filter(level=level).delete()

level_decors = []
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data[
"decorName"]
)
)
LevelDecor.objects.bulk_create(level_decors)

def set_blocks(level, blocks):
set_blocks_inner(level, blocks, LevelBlock, Block)
Expand Down
2 changes: 0 additions & 2 deletions game/migrations/0035_disable_route_score_level_70.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


def disable_route_score(apps, schema_editor):
Expand Down
2 changes: 0 additions & 2 deletions game/migrations/0036_level_score_73.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


def update_level(apps, schema_editor):
Expand Down
2 changes: 0 additions & 2 deletions game/migrations/0037_level_score_79.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


def update_level(apps, schema_editor):
Expand Down
1 change: 0 additions & 1 deletion game/migrations/0038_level_score_40.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


Expand Down
2 changes: 0 additions & 2 deletions game/migrations/0042_level_score_73.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


def update_level(apps, schema_editor):
Expand Down
2 changes: 0 additions & 2 deletions game/migrations/0048_add_cow_field_and_blocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json

from django.db import models, migrations
from game.level_management import set_blocks_inner, set_decor_inner


def add_cows_block(apps, schema_editor):
Expand Down
2 changes: 0 additions & 2 deletions game/migrations/0049_level_score_34.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


def update_level(apps, schema_editor):
Expand Down
2 changes: 0 additions & 2 deletions game/migrations/0050_level_score_40.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


def update_level(apps, schema_editor):
Expand Down
1 change: 0 additions & 1 deletion game/migrations/0051_level_score_49.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import migrations
from game.level_management import set_decor_inner, set_blocks_inner
import json


Expand Down
15 changes: 13 additions & 2 deletions game/migrations/0086_loop_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.apps.registry import Apps
from django.db import migrations

from game.level_management import set_decor_inner, set_blocks_inner
from game.level_management import set_blocks_inner


def add_loop_levels(apps: Apps, *args):
Expand Down Expand Up @@ -365,7 +365,18 @@ def add_loop_levels(apps: Apps, *args):
)

def set_decor(level, decor):
set_decor_inner(level, decor, LevelDecor)
"""Helper method creating LevelDecor objects given a list of decor in dictionary form."""
LevelDecor.objects.filter(level=level).delete()

level_decors = []
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data[
"decorName"]
)
)
LevelDecor.objects.bulk_create(level_decors)

set_decor(
level_110,
Expand Down
15 changes: 13 additions & 2 deletions game/migrations/0097_add_python_den_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.db import migrations

from game.level_management import set_decor_inner, set_blocks_inner
from game.level_management import set_blocks_inner


def add_python_den_levels(apps, schema_editor):
Expand Down Expand Up @@ -931,7 +931,18 @@ def add_python_den_decor(apps, schema_editor):
LevelDecor = apps.get_model("game", "LevelDecor")

def set_decor(level, decor):
set_decor_inner(level, decor, LevelDecor)
"""Helper method creating LevelDecor objects given a list of decor in dictionary form."""
LevelDecor.objects.filter(level=level).delete()

level_decors = []
for data in decor:
level_decors.append(
LevelDecor(
level_id=level.id, x=data["x"], y=data["y"], decorName=data[
"decorName"]
)
)
LevelDecor.objects.bulk_create(level_decors)

def bulk_copy_decor(old_level_name, new_level_name):
old_level = Level.objects.get(name=old_level_name, default=True)
Expand Down
18 changes: 18 additions & 0 deletions game/migrations/0105_delete_invalid_attempts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.apps.registry import Apps
from django.db import migrations


def delete_invalid_attempts(apps: Apps, *args):
Attempt = apps.get_model("game", "Attempt")

Attempt.objects.filter(start_time__isnull=True).delete()


class Migration(migrations.Migration):
dependencies = [("game", "0104_remove_level_direct_drive")]
operations = [
migrations.RunPython(
delete_invalid_attempts,
reverse_code=migrations.RunPython.noop,
)
]
Loading

0 comments on commit 0c25ab9

Please sign in to comment.