Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions games/__tests__/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,29 @@ def test_total_price_paid_display_zero(self):

def test_latest_added_date_display(self):
"""Test latest_added_date shows most recent platform addition date"""

game = GameFactory(name="Test Game")
# Prevent factory from creating automatic platform relationship
game = GameFactory(name="Test Game", platforms=False)
older_date = date.today() - timedelta(days=10)
newer_date = date.today() - timedelta(days=5)

# Add game to platform on different dates
GameOnPlatform.objects.create(
gop1 = GameOnPlatform.objects.create(
game=game,
platform=self.platform,
vendor=self.vendor1,
added=older_date,
deleted=False,
)
GameOnPlatform.objects.create(
# Set the date after creation to bypass auto_now_add
GameOnPlatform.objects.filter(pk=gop1.pk).update(added=older_date)

gop2 = GameOnPlatform.objects.create(
game=game,
platform=self.platform,
vendor=self.vendor2,
added=newer_date,
deleted=False,
)
# Set the date after creation to bypass auto_now_add
GameOnPlatform.objects.filter(pk=gop2.pk).update(added=newer_date)

# Annotate as admin would
game = (
Expand All @@ -116,9 +119,20 @@ def test_latest_added_date_display(self):

def test_latest_added_date_display_never(self):
"""Test latest_added_date shows 'Never' for games without dates"""
game = GameFactory(name="Undated Game")
# Prevent factory from creating automatic platform relationship
game = GameFactory(name="Undated Game", platforms=False)

# Game with no platform relationships
# Create GameOnPlatform with added=None
gop = GameOnPlatform.objects.create(
game=game,
platform=self.platform,
vendor=self.vendor1,
deleted=False,
)
# Set added to None after creation (bypasses auto_now_add)
GameOnPlatform.objects.filter(pk=gop.pk).update(added=None)

# Annotate as admin would - AFTER updating the added field
game = (
Game.objects.all_with_orphaned()
.annotate(latest_added=models.Max("platforms_meta_data__added"))
Expand Down
29 changes: 29 additions & 0 deletions games/migrations/0006_alter_gameonplatform_added_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.2.5 on 2026-01-24 12:20

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("games", "0005_rename_source_to_vendor"),
]

operations = [
migrations.AlterField(
model_name="gameonplatform",
name="added",
field=models.DateField(auto_now_add=True, null=True),
),
migrations.AlterField(
model_name="gameonplatform",
name="price",
field=models.DecimalField(
blank=True,
decimal_places=2,
default=0.0,
max_digits=6,
null=True,
verbose_name="Purchase price",
),
),
]
3 changes: 2 additions & 1 deletion games/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class GameOnPlatform(models.Model):
platform = models.ForeignKey(
Platform, on_delete=models.CASCADE, related_name="games_meta_data"
)
added = models.DateField(null=True, blank=True)
added = models.DateField(null=True, blank=True, auto_now_add=True)
identifier = models.CharField(
verbose_name="ID in the platform for generating URLs",
max_length=255,
Expand All @@ -128,6 +128,7 @@ class GameOnPlatform(models.Model):
max_digits=6,
blank=True,
null=True,
default=0.0,
)
vendor = models.ForeignKey(Vendor, on_delete=models.SET_NULL, null=True, blank=True)
deleted = models.BooleanField(default=False, db_index=True)
Expand Down