Update importer

This commit is contained in:
2025-05-01 15:26:33 +02:00
parent 73f1870431
commit b1bd57bcc2
3 changed files with 207 additions and 96 deletions

View File

@ -30,40 +30,24 @@ def get_home(request: HttpRequest) -> HttpResponse:
Returns:
HttpResponse: The response object
"""
now = timezone.now()
now: timezone.datetime = timezone.now()
grouped_drops = defaultdict(list)
# Query for active drops, efficiently fetching related campaign and game
# Also prefetch benefits if you need them in the template
current_drops_qs = (
TimeBasedDrop.objects.filter(start_at__lte=now, end_at__gte=now)
.select_related(
"campaign__game", # Follows ForeignKey relationships campaign -> game
)
.prefetch_related(
"benefits", # Efficiently fetches ManyToMany benefits
)
.order_by(
"campaign__game__display_name", # Order by game name first
"name", # Then by drop name
)
.select_related("campaign__game")
.prefetch_related("benefits")
.order_by("campaign__game__display_name", "name")
)
# Group the drops by game in Python
for drop in current_drops_qs:
# Check if the drop has an associated campaign and game
if drop.campaign and drop.campaign.game:
game = drop.campaign.game
game: Game = drop.campaign.game
grouped_drops[game].append(drop)
else:
# Handle drops without a game (optional, based on your data integrity)
# You could group them under a 'None' key or log a warning
# grouped_drops[None].append(drop)
pass # Or ignore them
logger.warning("Drop %s does not have an associated game or campaign.", drop.name)
context = {
"grouped_drops": dict(grouped_drops), # Convert defaultdict back to dict for template if preferred
}
context = {"grouped_drops": dict(grouped_drops)}
return render(request, "index.html", context)