From 7111431048c6dba071100adf3497e14cfba72772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 29 Jan 2025 12:43:57 +0100 Subject: [PATCH] Initial commit --- games/migrations/0043_auto_20250107_2117.py | 4 -- ...ame_platform_alter_game_unique_together.py | 28 +++++++++ games/migrations/0047_auto_20250119_2129.py | 61 +++++++++++++++++++ games/models.py | 36 ++++++----- games/static/base.css | 8 --- 5 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 games/migrations/0046_game_platform_alter_game_unique_together.py create mode 100644 games/migrations/0047_auto_20250119_2129.py diff --git a/games/migrations/0043_auto_20250107_2117.py b/games/migrations/0043_auto_20250107_2117.py index 5884030..b2f7840 100644 --- a/games/migrations/0043_auto_20250107_2117.py +++ b/games/migrations/0043_auto_20250107_2117.py @@ -7,11 +7,7 @@ def migrate_edition_to_editions_temp(apps, schema_editor): Purchase = apps.get_model("games", "Purchase") for purchase in Purchase.objects.all(): if purchase.edition: - print( - f"Migrating Purchase {purchase.id} with Edition {purchase.edition.id}" - ) purchase.editions_temp.add(purchase.edition) - print(purchase.editions_temp.all()) purchase.save() else: print(f"No edition found for Purchase {purchase.id}") diff --git a/games/migrations/0046_game_platform_alter_game_unique_together.py b/games/migrations/0046_game_platform_alter_game_unique_together.py new file mode 100644 index 0000000..22bf58b --- /dev/null +++ b/games/migrations/0046_game_platform_alter_game_unique_together.py @@ -0,0 +1,28 @@ +# Generated by Django 5.1.3 on 2025-01-08 20:06 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("games", "0045_alter_purchase_editions"), + ] + + operations = [ + migrations.AddField( + model_name="game", + name="platform", + field=models.ForeignKey( + blank=True, + default=None, + null=True, + on_delete=django.db.models.deletion.SET_DEFAULT, + to="games.platform", + ), + ), + migrations.AlterUniqueTogether( + name="game", + unique_together={("name", "platform", "year_released")}, + ), + ] diff --git a/games/migrations/0047_auto_20250119_2129.py b/games/migrations/0047_auto_20250119_2129.py new file mode 100644 index 0000000..8bcaccd --- /dev/null +++ b/games/migrations/0047_auto_20250119_2129.py @@ -0,0 +1,61 @@ +# Generated by Django 5.1.3 on 2025-01-19 20:29 + +from django.db import connection, migrations, models + + +def recreate_games(apps, schema_editor): + Edition = apps.get_model("games", "Edition") + Game = apps.get_model("games", "Game") + Purchase = apps.get_model("games", "Purchase") + + with connection.cursor() as cursor: + print("Create table games_gametemp") + cursor.execute( + "CREATE TABLE games_gametemp AS SELECT * FROM games_game WHERE 1=0;" + ) + + for edition in Edition.objects.all(): + print(f"Re-create edition with ID {edition.id}") + cursor.execute( + """ + INSERT INTO games_gametemp ( + id, name, sort_name, year_released, platform_id, wikidata, created_at + ) + VALUES (%s, %s, %s, %s, %s, %s, %s) + """, + [ + edition.id, # Reuse the Edition ID + edition.name, + edition.sort_name, + edition.year_released, + edition.platform_id, + Game.objects.get(id=edition.game.id).wikidata, + edition.created_at, + ], + ) + print("Turn foreign keys off") + cursor.execute("PRAGMA foreign_keys = OFF;") + print("Drop table games_game") + cursor.execute("DROP TABLE games_game;") + print("Drop table games_edition") + cursor.execute("DROP TABLE games_edition;") + print("Rename table games_gametemp to games_game") + # cursor.execute("ALTER TABLE games_gametemp RENAME TO games_game;") + cursor.execute("CREATE TABLE games_game AS SELECT * FROM games_gametemp;") + + +class Migration(migrations.Migration): + dependencies = [ + ("games", "0046_game_platform_alter_game_unique_together"), + ] + + operations = [ + migrations.RunPython(recreate_games), + migrations.AlterField( + model_name="purchase", + name="editions", + field=models.ManyToManyField( + blank=True, related_name="purchases", to="games.game" + ), + ), + ] diff --git a/games/models.py b/games/models.py index 94323c6..0bbd5fb 100644 --- a/games/models.py +++ b/games/models.py @@ -9,20 +9,6 @@ from django.utils import timezone from common.time import format_duration -class Game(models.Model): - name = models.CharField(max_length=255) - sort_name = models.CharField(max_length=255, null=True, blank=True, default=None) - year_released = models.IntegerField(null=True, blank=True, default=None) - wikidata = models.CharField(max_length=50, null=True, blank=True, default=None) - created_at = models.DateTimeField(auto_now_add=True) - - session_average: float | int | timedelta | None - session_count: int | None - - def __str__(self): - return self.name - - class Platform(models.Model): name = models.CharField(max_length=255) group = models.CharField(max_length=255, null=True, blank=True, default=None) @@ -44,6 +30,26 @@ def get_sentinel_platform(): )[0] +class Game(models.Model): + class Meta: + unique_together = [["name", "platform", "year_released"]] + + name = models.CharField(max_length=255) + sort_name = models.CharField(max_length=255, null=True, blank=True, default=None) + year_released = models.IntegerField(null=True, blank=True, default=None) + platform = models.ForeignKey( + Platform, on_delete=models.SET_DEFAULT, null=True, blank=True, default=None + ) + wikidata = models.CharField(max_length=50, null=True, blank=True, default=None) + created_at = models.DateTimeField(auto_now_add=True) + + session_average: float | int | timedelta | None + session_count: int | None + + def __str__(self): + return self.name + + class Edition(models.Model): class Meta: unique_together = [["name", "platform", "year_released"]] @@ -113,7 +119,7 @@ class Purchase(models.Model): objects = PurchaseQueryset().as_manager() - editions = models.ManyToManyField(Edition, related_name="purchases", blank=True) + editions = models.ManyToManyField(Game, related_name="purchases", blank=True) platform = models.ForeignKey( Platform, on_delete=models.CASCADE, default=None, null=True, blank=True ) diff --git a/games/static/base.css b/games/static/base.css index d02ca26..b27e1bf 100644 --- a/games/static/base.css +++ b/games/static/base.css @@ -1443,10 +1443,6 @@ input:checked + .toggle-bg { margin-top: 1rem; } -.ml-4 { - margin-left: 1rem; -} - .block { display: block; } @@ -1475,10 +1471,6 @@ input:checked + .toggle-bg { display: grid; } -.list-item { - display: list-item; -} - .hidden { display: none; } -- 2.40.1