Sort drops within campaigns by required minutes watched
All checks were successful
Ruff / ruff (push) Successful in 10s

This commit is contained in:
2025-05-01 21:46:15 +02:00
parent ef02f7878e
commit 4e40bc032c
2 changed files with 22 additions and 15 deletions

View File

@ -24,7 +24,8 @@ def get_home(request: HttpRequest) -> HttpResponse:
"""Render the index page with drops grouped hierarchically by game and campaign.
This view fetches all currently active drops (where current time is between start_at and end_at),
and organizes them by game and campaign for display.
and organizes them by game and campaign for display. Drops within each campaign are sorted by
required minutes watched.
Args:
request (HttpRequest): The request object.
@ -43,7 +44,7 @@ def get_home(request: HttpRequest) -> HttpResponse:
TimeBasedDrop.objects.filter(start_at__lte=now, end_at__gte=now)
.select_related("campaign", "campaign__game", "campaign__owner")
.prefetch_related("benefits")
.order_by("campaign__game__display_name", "campaign__name", "name")
.order_by("campaign__game__display_name", "campaign__name", "required_minutes_watched")
)
# Drops without associated games or campaigns
@ -70,6 +71,15 @@ def get_home(request: HttpRequest) -> HttpResponse:
orphaned_drops.append(drop)
logger.warning("Drop %s does not have an associated game or campaign.", drop.name or drop.drop_id)
# Make sure drops within each campaign are sorted by required_minutes_watched
for campaigns in grouped_drops.values():
for drops in campaigns.values():
drops.sort(key=lambda drop: drop.required_minutes_watched or 0)
# Also sort orphaned drops if any
if orphaned_drops:
orphaned_drops.sort(key=lambda drop: drop.required_minutes_watched or 0)
context: dict[str, Any] = {
"grouped_drops": grouped_drops,
"orphaned_drops": orphaned_drops,