Refactor HTML and Python code for improved readability and consistency

This commit is contained in:
Joakim Hellsén 2025-07-10 04:31:57 +02:00
commit b8a1cc4ff4
3 changed files with 83 additions and 114 deletions

View file

@ -53,7 +53,7 @@ class DropCampaignListView(ListView):
context = super().get_context_data(**kwargs)
# Load all games in a single query instead of multiple queries per game
context["games"] = Game.objects.all().order_by('display_name')
context["games"] = Game.objects.all().order_by("display_name")
# Add status options for filtering
context["status_options"] = [status[0] for status in DropCampaign.STATUS_CHOICES]
@ -166,42 +166,26 @@ class GameListView(ListView):
# Step 1: Get all organizations with games in a single query
# We'll prefetch the games related to each organization through drop_campaigns
organizations_with_games = Organization.objects.filter(
drop_campaigns__isnull=False
).distinct().order_by('name')
organizations_with_games = Organization.objects.filter(drop_campaigns__isnull=False).distinct().order_by("name")
# Step 2: Get all game-organization relationships in a single efficient query
# This query gets all games with their campaign counts and organization info
game_org_relations = DropCampaign.objects.values(
'game_id', 'owner_id', 'owner__name'
).annotate(
campaign_count=Count('id', distinct=True),
active_count=Count(
'id',
filter=Q(
start_at__lte=now,
end_at__gte=now,
status="ACTIVE"
),
distinct=True
)
game_org_relations = DropCampaign.objects.values("game_id", "owner_id", "owner__name").annotate(
campaign_count=Count("id", distinct=True),
active_count=Count("id", filter=Q(start_at__lte=now, end_at__gte=now, status="ACTIVE"), distinct=True),
)
# Step 3: Get all games in a single query with their display names
all_games = {
game.id: game for game in Game.objects.all()
}
all_games = {game.id: game for game in Game.objects.all()}
# Step 4: Create a mapping of organization_id to organization_name
org_names = {
org.id: org.name for org in organizations_with_games
}
org_names = {org.id: org.name for org in organizations_with_games}
# Step 5: Group games by organization
game_org_map = {}
for relation in game_org_relations:
org_id = relation['owner_id']
game_id = relation['game_id']
org_id = relation["owner_id"]
game_id = relation["game_id"]
if org_id not in game_org_map:
game_org_map[org_id] = {}
@ -210,16 +194,16 @@ class GameListView(ListView):
game = all_games.get(game_id)
if game:
game_org_map[org_id][game_id] = {
'game': game,
'campaign_count': relation['campaign_count'],
'active_count': relation['active_count']
"game": game,
"campaign_count": relation["campaign_count"],
"active_count": relation["active_count"],
}
# Step 6: Convert the nested dictionary to the format expected by the template
for org_id, games in game_org_map.items():
if org_id in org_names:
# Create an Organization-like object with id and name
org_obj = type('Organization', (), {'id': org_id, 'name': org_names[org_id]})
org_obj = type("Organization", (), {"id": org_id, "name": org_names[org_id]})
games_by_org[org_obj] = list(games.values())
# Create the flattened games_with_counts for backward compatibility
@ -255,21 +239,15 @@ class GameDetailView(DetailView):
# Get all campaigns for this game in a single query with prefetching
now = timezone.now()
all_campaigns = DropCampaign.objects.filter(
game=game
).select_related("owner").order_by('-end_at')
all_campaigns = DropCampaign.objects.filter(game=game).select_related("owner").order_by("-end_at")
# Filter the campaigns in Python instead of making multiple queries
active_campaigns = [
campaign for campaign in all_campaigns
if campaign.start_at <= now and campaign.end_at >= now and campaign.status == "ACTIVE"
campaign for campaign in all_campaigns if campaign.start_at <= now and campaign.end_at >= now and campaign.status == "ACTIVE"
]
active_campaigns.sort(key=lambda c: c.end_at) # Sort by end_at ascending
upcoming_campaigns = [
campaign for campaign in all_campaigns
if campaign.start_at > now and campaign.status == "UPCOMING"
]
upcoming_campaigns = [campaign for campaign in all_campaigns if campaign.start_at > now and campaign.status == "UPCOMING"]
upcoming_campaigns.sort(key=lambda c: c.start_at) # Sort by start_at ascending
# No need to fetch expired_campaigns separately as we already have all_campaigns
@ -296,19 +274,10 @@ def dashboard(request: HttpRequest) -> HttpResponse:
# Get active campaigns with prefetching to reduce queries
now = timezone.now()
active_campaigns = (
DropCampaign.objects.filter(
start_at__lte=now,
end_at__gte=now,
status="ACTIVE"
)
DropCampaign.objects.filter(start_at__lte=now, end_at__gte=now, status="ACTIVE")
.select_related("game", "owner")
# Prefetch the time-based drops with their benefits to avoid N+1 queries
.prefetch_related(
Prefetch(
'time_based_drops',
queryset=TimeBasedDrop.objects.prefetch_related('benefits')
)
)
.prefetch_related(Prefetch("time_based_drops", queryset=TimeBasedDrop.objects.prefetch_related("benefits")))
)
return render(