Group drops by campaign
This commit is contained in:
@ -10,7 +10,7 @@
|
|||||||
<span class="d-inline text-muted">{{ grouped_drops|length }} game{{ grouped_drops|length|pluralize }}</span>
|
<span class="d-inline text-muted">{{ grouped_drops|length }} game{{ grouped_drops|length|pluralize }}</span>
|
||||||
</h2>
|
</h2>
|
||||||
{% if grouped_drops %}
|
{% if grouped_drops %}
|
||||||
{% for game, drops_list in grouped_drops.items %}
|
{% for game, campaigns in grouped_drops.items %}
|
||||||
<div class="card mb-4 shadow-sm">
|
<div class="card mb-4 shadow-sm">
|
||||||
<div class="row g-0">
|
<div class="row g-0">
|
||||||
<!-- Game Box Art -->
|
<!-- Game Box Art -->
|
||||||
@ -40,41 +40,51 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
{% if drops_list %}
|
{% if campaigns %}
|
||||||
<div class="mt-4">
|
<div class="mt-4">
|
||||||
{% for drop in drops_list %}
|
{% for campaign, drops_list in campaigns.items %}
|
||||||
{% with campaign=drop.campaign %}
|
<div class="card mb-3">
|
||||||
<h4 class="h6">{{ campaign.name|default:"Unknown Campaign" }}</h4>
|
<div class="card-header bg-secondary bg-opacity-25">
|
||||||
<a href="{{ campaign.details_url|default:'#' }}" class="text-decoration-none">Details</a>
|
<h4 class="h6 mb-0">{{ campaign.name|default:"Unknown Campaign" }}</h4>
|
||||||
|
<div class="mt-1">
|
||||||
|
<a href="{{ campaign.details_url|default:'#' }}"
|
||||||
|
class="text-decoration-none">Details</a>
|
||||||
{% if campaign.details_url != campaign.account_link_url and campaign.account_link_url %}
|
{% if campaign.details_url != campaign.account_link_url and campaign.account_link_url %}
|
||||||
| <a href="{{ campaign.account_link_url }}" class="text-decoration-none">Link Account</a>
|
| <a href="{{ campaign.account_link_url }}" class="text-decoration-none">Link
|
||||||
|
Account</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<p class="mb-2 text-muted">
|
</div>
|
||||||
|
<p class="mb-0 mt-1 text-muted small">
|
||||||
Ends in:
|
Ends in:
|
||||||
<abbr
|
<abbr
|
||||||
title="{{ campaign.start_at|date:'l d F H:i' }} - {{ campaign.end_at|date:'l d F H:i' }}">
|
title="{{ campaign.start_at|date:'l d F H:i' }} - {{ campaign.end_at|date:'l d F H:i' }}">
|
||||||
{{ campaign.end_at|timeuntil }}
|
{{ campaign.end_at|timeuntil }}
|
||||||
</abbr>
|
</abbr>
|
||||||
</p>
|
</p>
|
||||||
|
</div>
|
||||||
<div class="table-responsive mb-4">
|
<div class="card-body p-0">
|
||||||
<table class="table table-striped table-hover align-middle">
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped table-hover align-middle mb-0">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Benefit Image</th>
|
<th>Benefit Image</th>
|
||||||
|
<th>Drop Name</th>
|
||||||
<th>Benefit Name</th>
|
<th>Benefit Name</th>
|
||||||
<th>Required Minutes Watched</th>
|
<th>Required Minutes Watched</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
{% for drop in drops_list %}
|
||||||
{% if drop.benefits.exists %}
|
{% if drop.benefits.exists %}
|
||||||
{% for benefit in drop.benefits.all %}
|
{% for benefit in drop.benefits.all %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<img src="{{ benefit.image_asset_url|default:'https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg' }}"
|
<img src="{{ benefit.image_asset_url|default:'https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg' }}"
|
||||||
alt="{{ benefit.name|default:'Unknown Benefit' }}"
|
alt="{{ benefit.name|default:'Unknown Benefit' }}"
|
||||||
class="img-fluid rounded" height="50" width="50" loading="lazy" />
|
class="img-fluid rounded" height="50" width="50"
|
||||||
|
loading="lazy" />
|
||||||
</td>
|
</td>
|
||||||
|
<td>{{ drop.name|default:'Unknown Drop' }}</td>
|
||||||
<td>
|
<td>
|
||||||
<abbr title="{{ drop.name|default:'Unknown Drop' }}">
|
<abbr title="{{ drop.name|default:'Unknown Drop' }}">
|
||||||
{{ benefit.name|default:'Unknown Benefit' }}
|
{{ benefit.name|default:'Unknown Benefit' }}
|
||||||
@ -88,16 +98,21 @@
|
|||||||
<td>
|
<td>
|
||||||
<img src="https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg"
|
<img src="https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg"
|
||||||
alt="{{ drop.name|default:'Unknown Drop' }}"
|
alt="{{ drop.name|default:'Unknown Drop' }}"
|
||||||
class="img-fluid rounded" height="50" width="50" loading="lazy" />
|
class="img-fluid rounded" height="50" width="50"
|
||||||
|
loading="lazy" />
|
||||||
</td>
|
</td>
|
||||||
<td>{{ drop.name|default:'Unknown Drop' }}</td>
|
<td>{{ drop.name|default:'Unknown Drop' }}</td>
|
||||||
<td>N/A</td>
|
<td>N/A</td>
|
||||||
|
<td>{{ drop.required_minutes_watched|minutes_to_hours|default:'N/A' }}
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
{% endwith %}
|
</div>
|
||||||
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from collections import defaultdict
|
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from django.db.models import Prefetch
|
from django.db.models import Prefetch
|
||||||
@ -22,34 +21,70 @@ logger: logging.Logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
@require_http_methods(request_method_list=["GET", "HEAD"])
|
@require_http_methods(request_method_list=["GET", "HEAD"])
|
||||||
def get_home(request: HttpRequest) -> HttpResponse:
|
def get_home(request: HttpRequest) -> HttpResponse:
|
||||||
"""Render the index page.
|
"""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.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
request (HttpRequest): The request object.
|
request (HttpRequest): The request object.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
HttpResponse: The response object
|
HttpResponse: The rendered index template with grouped drops.
|
||||||
"""
|
"""
|
||||||
now: timezone.datetime = timezone.now()
|
now: timezone.datetime = timezone.now()
|
||||||
grouped_drops = defaultdict(list)
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Dictionary structure: {Game: {Campaign: [TimeBasedDrop, ...]}}
|
||||||
|
grouped_drops: dict[Game, dict[DropCampaign, list[TimeBasedDrop]]] = {}
|
||||||
|
|
||||||
|
# Fetch all currently active drops with optimized queries
|
||||||
current_drops_qs = (
|
current_drops_qs = (
|
||||||
TimeBasedDrop.objects.filter(start_at__lte=now, end_at__gte=now)
|
TimeBasedDrop.objects.filter(start_at__lte=now, end_at__gte=now)
|
||||||
.select_related("campaign__game")
|
.select_related("campaign", "campaign__game", "campaign__owner")
|
||||||
.prefetch_related("benefits")
|
.prefetch_related("benefits")
|
||||||
.order_by("campaign__game__display_name", "name")
|
.order_by("campaign__game__display_name", "campaign__name", "name")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Drops without associated games or campaigns
|
||||||
|
orphaned_drops: list[TimeBasedDrop] = []
|
||||||
|
|
||||||
for drop in current_drops_qs:
|
for drop in current_drops_qs:
|
||||||
|
# Check if drop has both campaign and game
|
||||||
if drop.campaign and drop.campaign.game:
|
if drop.campaign and drop.campaign.game:
|
||||||
game: Game = drop.campaign.game
|
game: Game = drop.campaign.game
|
||||||
grouped_drops[game].append(drop)
|
campaign: DropCampaign = drop.campaign
|
||||||
else:
|
|
||||||
logger.warning("Drop %s does not have an associated game or campaign.", drop.name)
|
# Initialize the game entry if it doesn't exist
|
||||||
|
if game not in grouped_drops:
|
||||||
|
grouped_drops[game] = {}
|
||||||
|
|
||||||
|
# Initialize the campaign entry if it doesn't exist
|
||||||
|
if campaign not in grouped_drops[game]:
|
||||||
|
grouped_drops[game][campaign] = []
|
||||||
|
|
||||||
|
# Add the drop to the appropriate campaign
|
||||||
|
grouped_drops[game][campaign].append(drop)
|
||||||
|
else:
|
||||||
|
# Store drops without proper association separately
|
||||||
|
orphaned_drops.append(drop)
|
||||||
|
logger.warning("Drop %s does not have an associated game or campaign.", drop.name or drop.drop_id)
|
||||||
|
|
||||||
|
context: dict[str, Any] = {
|
||||||
|
"grouped_drops": grouped_drops,
|
||||||
|
"orphaned_drops": orphaned_drops,
|
||||||
|
"current_time": now,
|
||||||
|
}
|
||||||
|
|
||||||
context = {"grouped_drops": dict(grouped_drops)}
|
|
||||||
return render(request, "index.html", context)
|
return render(request, "index.html", context)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Error in get_home view")
|
||||||
|
return HttpResponse(
|
||||||
|
status=500,
|
||||||
|
content="An error occurred while processing your request. Please try again later.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@require_http_methods(request_method_list=["GET", "HEAD"])
|
@require_http_methods(request_method_list=["GET", "HEAD"])
|
||||||
def get_game(request: HttpRequest, twitch_id: int) -> HttpResponse:
|
def get_game(request: HttpRequest, twitch_id: int) -> HttpResponse:
|
||||||
|
Reference in New Issue
Block a user