Merge Edition into Game #85
@ -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")],
 | 
			
		||||
 | 
			
		||||
@ -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}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										58
									
								
								games/migrations/0048_game_platform.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								games/migrations/0048_game_platform.py
									
									
									
									
									
										Normal 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),
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										17
									
								
								games/migrations/0049_alter_game_unique_together.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								games/migrations/0049_alter_game_unique_together.py
									
									
									
									
									
										Normal 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')},
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user