diff --git a/common/components.py b/common/components.py index e1582e8..cbd7d9f 100644 --- a/common/components.py +++ b/common/components.py @@ -263,6 +263,8 @@ def NameWithIcon( game = Game.objects.get(pk=game_id) name = edition.name if edition else game.name platform = edition.platform if edition else None + if game.platform: + platform = game.platform link = reverse("view_game", args=[int(game_id)]) content = Div( [("class", "inline-flex gap-2 items-center")], diff --git a/games/forms.py b/games/forms.py index 4793367..78b9ace 100644 --- a/games/forms.py +++ b/games/forms.py @@ -156,7 +156,7 @@ class EditionForm(forms.ModelForm): class GameForm(forms.ModelForm): class Meta: model = Game - fields = ["name", "sort_name", "year_released", "wikidata"] + fields = ["name", "sort_name", "platform", "year_released", "wikidata"] widgets = {"name": autofocus_input_widget} diff --git a/games/migrations/0048_game_platform.py b/games/migrations/0048_game_platform.py new file mode 100644 index 0000000..25760db --- /dev/null +++ b/games/migrations/0048_game_platform.py @@ -0,0 +1,58 @@ +# Generated by Django 5.1.5 on 2025-01-29 17:08 + +import django.db.models.deletion +from django.db import migrations, models + +from games.models import Game + + +def copy_platform_to_game(apps, schema_editor): + single_edition_games = Game.objects.annotate( + num_editions=models.Count("editions") + ).filter(num_editions=1) + multi_edition_games = Game.objects.annotate( + num_editions=models.Count("editions") + ).filter(num_editions__gt=1) + for game in single_edition_games: + game.platform = game.editions.first().platform + game.save() + + for game in multi_edition_games: + all_editions = game.editions.all() + for e in all_editions: + # game with this platform edition already exists + if game.platform == e.platform: + print( + f"Game '{game}' with ID '{game.pk}' already has edition with platform '{game.platform}', skipping creation." + ) + else: + print( + f"Game '{game}' with ID '{game.pk}' missing edition with platform '{e.platform}', creating..." + ) + Game.objects.create( + name=e.name, + sort_name=e.sort_name, + platform=e.platform, + year_released=e.year_released, + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("games", "0047_alter_edition_game"), + ] + + 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.RunPython(copy_platform_to_game), + ] diff --git a/games/migrations/0049_alter_game_unique_together.py b/games/migrations/0049_alter_game_unique_together.py new file mode 100644 index 0000000..93c7a96 --- /dev/null +++ b/games/migrations/0049_alter_game_unique_together.py @@ -0,0 +1,17 @@ +# Generated by Django 5.1.5 on 2025-01-29 17:34 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('games', '0048_game_platform'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='game', + unique_together={('name', 'platform', 'year_released')}, + ), + ] diff --git a/games/models.py b/games/models.py index e3e432e..9a67dd3 100644 --- a/games/models.py +++ b/games/models.py @@ -10,10 +10,17 @@ from common.time import format_duration 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) wikidata = models.CharField(max_length=50, null=True, blank=True, default=None) + platform = models.ForeignKey( + "Platform", on_delete=models.SET_DEFAULT, null=True, blank=True, default=None + ) + created_at = models.DateTimeField(auto_now_add=True) session_average: float | int | timedelta | None