Add platform to Game

This commit is contained in:
Lukáš Kucharczyk 2025-01-29 18:42:13 +01:00
parent 23c1ce1f96
commit e571feadef
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
5 changed files with 85 additions and 1 deletions

View File

@ -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")],

View File

@ -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}

View File

@ -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),
]

View File

@ -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')},
),
]

View File

@ -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