Compare commits
3 Commits
14f2cdc9f9
...
4e40bc032c
Author | SHA1 | Date | |
---|---|---|---|
4e40bc032c
|
|||
ef02f7878e
|
|||
ebed72f5fa
|
@ -10,8 +10,7 @@
|
||||
<span class="d-inline text-muted">{{ grouped_drops|length }} game{{ grouped_drops|length|pluralize }}</span>
|
||||
</h2>
|
||||
{% if grouped_drops %}
|
||||
{% for game, drops_list in grouped_drops.items %}
|
||||
{# Retain card structure for layout, replace table with list #}
|
||||
{% for game, campaigns in grouped_drops.items %}
|
||||
<div class="card mb-4 shadow-sm">
|
||||
<div class="row g-0">
|
||||
<!-- Game Box Art -->
|
||||
@ -20,7 +19,7 @@
|
||||
<img src="{{ game.box_art_url }}" alt="{{ game.display_name|default:'Game name unknown' }} box art"
|
||||
class="img-fluid rounded-start" height="283" width="212" loading="lazy" />
|
||||
{% else %}
|
||||
<img src="{% static 'images/404_boxart.jpg' %}"
|
||||
<img src="https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg"
|
||||
alt="{% if game %}{{ game.display_name }}{% else %}Unknown Game{% endif %} box art"
|
||||
class="img-fluid rounded-start" height="283" width="212" loading="lazy" />
|
||||
{% endif %}
|
||||
@ -41,54 +40,78 @@
|
||||
{% endif %}
|
||||
</h2>
|
||||
|
||||
{% if drops_list %}
|
||||
<!-- NEW Drop List Structure -->
|
||||
<ul class="list-unstyled mt-3">
|
||||
{% for drop in drops_list %}
|
||||
<li class="mb-3"> {# Add margin between list items #}
|
||||
<strong>{{ drop.name|default:'Unknown Drop' }}</strong>
|
||||
(Requires {{ drop.required_minutes_watched|minutes_to_hours }})
|
||||
<br>
|
||||
<em>Campaign:
|
||||
<a href="{{ drop.campaign.details_url|default:'#' }}" class="text-decoration-none"
|
||||
title="{{ drop.campaign.name|default:'Unknown Campaign' }}">
|
||||
{{ drop.campaign.name|truncatechars:40|default:'N/A' }} {# Adjusted truncate #}
|
||||
</a>
|
||||
{% if drop.campaign.details_url != drop.campaign.account_link_url and drop.campaign.account_link_url %}
|
||||
| <a href="{{ drop.campaign.account_link_url }}" class="text-decoration-none"
|
||||
title="Link Account for {{ drop.campaign.name }}">Link</a>
|
||||
{% endif %}
|
||||
</em>
|
||||
<br>
|
||||
Ends in: <abbr
|
||||
title="{{ drop.campaign.start_at|date:'l d F H:i' }} - {{ drop.campaign.end_at|date:'l d F H:i' }}">
|
||||
{{ drop.campaign.end_at|timeuntil }}
|
||||
</abbr>
|
||||
|
||||
{% if drop.benefits.exists %}
|
||||
<br>
|
||||
Benefits:
|
||||
<ul class="list-inline">
|
||||
{% for benefit in drop.benefits.all %}
|
||||
<li class="list-inline-item">
|
||||
<abbr title="{{ benefit.name|default:'Unknown Benefit' }}">
|
||||
{% if benefit.image_asset_url %}
|
||||
<img src="{{ benefit.image_asset_url|default:'https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg' }}"
|
||||
alt="{{ benefit.name|default:'Unknown Benefit' }}"
|
||||
class="img-fluid rounded me-1 align-middle" {# Added align-middle #}
|
||||
height="20" width="20" loading="lazy" />
|
||||
{% endif %}
|
||||
<small>{{ benefit.name|truncatechars:25|default:'Unknown Benefit' }}</small>
|
||||
{# Wrap text in small #}
|
||||
</abbr>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
{# Removed hr, using li margin instead #}
|
||||
</li>
|
||||
{% if campaigns %}
|
||||
<div class="mt-4">
|
||||
{% for campaign, drops_list in campaigns.items %}
|
||||
<div class="card mb-3">
|
||||
<div class="card-header bg-secondary bg-opacity-25">
|
||||
<h4 class="h6 mb-0"><a
|
||||
href="{{ campaign.details_url|default:'#' }}">{{ campaign.name|default:"Unknown Campaign" }}</a>
|
||||
</h4>
|
||||
<div class="mt-1">
|
||||
{% 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>
|
||||
{% endif %}
|
||||
</div>
|
||||
<p class="mb-0 mt-1 text-muted small">
|
||||
{{ campaign.start_at|date:'l d F H:i' }} -
|
||||
{{ campaign.end_at|date:'l d F H:i' }} | {{ campaign.end_at|timeuntil }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="card-body p-0">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-hover align-middle mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Benefit Image</th>
|
||||
<th>Drop Name</th>
|
||||
<th>Benefit Name</th>
|
||||
<th>Required Minutes Watched</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for drop in drops_list %}
|
||||
{% if drop.benefits.exists %}
|
||||
{% for benefit in drop.benefits.all %}
|
||||
<tr>
|
||||
<td>
|
||||
<img src="{{ benefit.image_asset_url|default:'https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg' }}"
|
||||
alt="{{ benefit.name|default:'Unknown Benefit' }}"
|
||||
class="img-fluid rounded" height="50" width="50"
|
||||
loading="lazy" />
|
||||
</td>
|
||||
<td>{{ drop.name|default:'Unknown Drop' }}</td>
|
||||
<td>
|
||||
{{ benefit.name|default:'Unknown Benefit' }}
|
||||
</td>
|
||||
<td>
|
||||
{{ drop.required_minutes_watched|minutes_to_hours }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr>
|
||||
<td>
|
||||
<img src="https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg"
|
||||
alt="{{ drop.name|default:'Unknown Drop' }}"
|
||||
class="img-fluid rounded" height="50" width="50"
|
||||
loading="lazy" />
|
||||
</td>
|
||||
<td>{{ drop.name|default:'Unknown Drop' }}</td>
|
||||
<td>N/A</td>
|
||||
<td>{{ drop.required_minutes_watched|minutes_to_hours|default:'N/A' }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% else %}
|
||||
<p class="mt-3 text-muted">No active drops found for this game currently.</p>
|
||||
{% endif %}
|
||||
|
@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from django.db.models import Prefetch
|
||||
@ -22,33 +21,79 @@ logger: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
@require_http_methods(request_method_list=["GET", "HEAD"])
|
||||
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. Drops within each campaign are sorted by
|
||||
required minutes watched.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The request object.
|
||||
|
||||
Returns:
|
||||
HttpResponse: The response object
|
||||
HttpResponse: The rendered index template with grouped drops.
|
||||
"""
|
||||
now: timezone.datetime = timezone.now()
|
||||
grouped_drops = defaultdict(list)
|
||||
|
||||
current_drops_qs = (
|
||||
TimeBasedDrop.objects.filter(start_at__lte=now, end_at__gte=now)
|
||||
.select_related("campaign__game")
|
||||
.prefetch_related("benefits")
|
||||
.order_by("campaign__game__display_name", "name")
|
||||
)
|
||||
try:
|
||||
# Dictionary structure: {Game: {Campaign: [TimeBasedDrop, ...]}}
|
||||
grouped_drops: dict[Game, dict[DropCampaign, list[TimeBasedDrop]]] = {}
|
||||
|
||||
for drop in current_drops_qs:
|
||||
if drop.campaign and drop.campaign.game:
|
||||
game: Game = drop.campaign.game
|
||||
grouped_drops[game].append(drop)
|
||||
else:
|
||||
logger.warning("Drop %s does not have an associated game or campaign.", drop.name)
|
||||
# Fetch all currently active drops with optimized queries
|
||||
current_drops_qs = (
|
||||
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", "required_minutes_watched")
|
||||
)
|
||||
|
||||
context = {"grouped_drops": dict(grouped_drops)}
|
||||
return render(request, "index.html", context)
|
||||
# Drops without associated games or campaigns
|
||||
orphaned_drops: list[TimeBasedDrop] = []
|
||||
|
||||
for drop in current_drops_qs:
|
||||
# Check if drop has both campaign and game
|
||||
if drop.campaign and drop.campaign.game:
|
||||
game: Game = drop.campaign.game
|
||||
campaign: DropCampaign = drop.campaign
|
||||
|
||||
# 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)
|
||||
|
||||
# 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,
|
||||
"current_time": now,
|
||||
}
|
||||
|
||||
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"])
|
||||
|
Reference in New Issue
Block a user