From eaf8b5de77ada91b1c3750e0e8b8e19d71b9a264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Wed, 21 Aug 2024 06:37:15 +0200 Subject: [PATCH] Remove TOC --- core/templates/games.html | 54 ++++++++++---------- core/templates/index.html | 47 ++++++++--------- core/templates/reward_campaigns.html | 21 ++++---- core/templatetags/game_tags.py | 4 +- core/views.py | 76 +++++++--------------------- static/js/index.js | 16 ------ 6 files changed, 76 insertions(+), 142 deletions(-) delete mode 100644 static/js/index.js diff --git a/core/templates/games.html b/core/templates/games.html index 6972c02..4fc0cca 100644 --- a/core/templates/games.html +++ b/core/templates/games.html @@ -1,34 +1,34 @@ {% extends "base.html" %} {% block content %} -
-
-
{{ toc|safe }}
-
-
- {% for game in games %} -
-
- {{ game.display_name }} -
-
{{ game.display_name }}
-
-
- - -
-
- - -
-
-
-
-
- {% endfor %} +
+ {% for game in games %} + + {% endfor %} +
{% endblock content %} diff --git a/core/templates/index.html b/core/templates/index.html index 3b21752..c344942 100644 --- a/core/templates/index.html +++ b/core/templates/index.html @@ -1,30 +1,27 @@ {% extends "base.html" %} -{% load static campaign_tags game_tags %} +{% load static %} +{% load campaign_tags %} +{% load game_tags %} {% block content %}
-
-
{{ toc|safe }}
-
- {% include "partials/info_box.html" %} - {% include "partials/news.html" %} -

- Reward campaign - -
- {{ reward_campaigns.count }} - campaign{{ reward_campaigns.count|pluralize }} -
-

- {% for campaign in reward_campaigns %} - {% render_campaign campaign %} {# Stored in /core/templatetags/campaign_tags.py #} - {% endfor %} -

- Drop campaigns - -
{{ games.count }} game{{ games.count|pluralize }}
-

- {% for game in games %} - {% render_game_card game %} {# Stored in /core/templatetags/game_tags.py #} - {% endfor %} -
-
+ {% include "partials/info_box.html" %} + {% include "partials/news.html" %} +

+ Reward campaign - + + {{ reward_campaigns.count }} + campaign{{ reward_campaigns.count|pluralize }} + +

+ {% for campaign in reward_campaigns %} + {% render_campaign campaign %} + {% endfor %} +

+ Drop campaigns - + {{ games.count }} game{{ games.count|pluralize }} +

+ {% for game in games %} + {% render_game_card game %} + {% endfor %}
{% endblock content %} diff --git a/core/templates/reward_campaigns.html b/core/templates/reward_campaigns.html index ac05b56..dc80bbf 100644 --- a/core/templates/reward_campaigns.html +++ b/core/templates/reward_campaigns.html @@ -1,17 +1,14 @@ {% extends "base.html" %} {% load static %} +{% load campaign_tags %} +{% load game_tags %} {% block content %} -
-
-
{{ toc }}
-
-

Reward Campaigns

-
- {% for campaign in reward_campaigns %} - {% include "partials/reward_campaign_card.html" %} - {% endfor %} -
-
-
+
+
+

Reward Campaigns

+ {% for campaign in reward_campaigns %} + {% render_campaign campaign %} + {% endfor %}
+
{% endblock content %} diff --git a/core/templatetags/game_tags.py b/core/templatetags/game_tags.py index 6da6391..d7bd411 100644 --- a/core/templatetags/game_tags.py +++ b/core/templatetags/game_tags.py @@ -19,14 +19,13 @@ def render_game_card(game: Game) -> SafeText: Returns: The rendered HTML string. """ - twitch_id: str = game.twitch_id box_art_url: str = game.box_art_url or "https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg" name: str = game.name or "Game name unknown" slug: str = game.slug or "game-name-unknown" drop_campaigns: list[DropCampaign] = game.drop_campaigns.all() # type: ignore # noqa: PGH003 return format_html( """ -
+
{} box art @@ -45,7 +44,6 @@ def render_game_card(game: Game) -> SafeText:
""", - twitch_id, box_art_url, name, slug, diff --git a/core/views.py b/core/views.py index f353e84..e6947a4 100644 --- a/core/views.py +++ b/core/views.py @@ -1,8 +1,7 @@ from __future__ import annotations import logging -from dataclasses import dataclass -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import requests_cache from django.db.models import Prefetch @@ -21,38 +20,6 @@ if TYPE_CHECKING: logger: logging.Logger = logging.getLogger(__name__) -@dataclass -class TOCItem: - """Table of contents item.""" - - name: str - toc_id: str - - -def build_toc(list_of_things: list[TOCItem]) -> str: - """Build the table of contents. - - Args: - list_of_things (list[TOCItem]): The list of table of contents items. - - Returns: - str: The HTML for the table of contents. - """ - html: str = """ -
-
-
-
- """ - - for item in list_of_things: - html += ( - f'{item.name}' - ) - html += """
""" - return html - - def get_reward_campaigns() -> BaseManager[RewardCampaign]: """Get the reward campaigns. @@ -68,32 +35,25 @@ def get_games_with_drops() -> BaseManager[Game]: Returns: BaseManager[Game]: The games with drops. """ - # Filter active drop campaigns - active_campaigns: BaseManager[DropCampaign] = DropCampaign.objects.filter(ends_at__gte=timezone.now()) - - # Prefetch Benefits for each TimeBasedDrop - benefits_prefetch = Prefetch(lookup="benefits", queryset=Benefit.objects.all()) - - # Filter active time-based drops + active_campaigns_query: BaseManager[DropCampaign] = DropCampaign.objects.filter(ends_at__gte=timezone.now()) active_time_based_drops: BaseManager[TimeBasedDrop] = TimeBasedDrop.objects.filter(ends_at__gte=timezone.now()) - # Prefetch Benefits for each active TimeBasedDrop benefits_prefetch = Prefetch(lookup="benefits", queryset=Benefit.objects.all()) - - # Prefetch TimeBasedDrops for each DropCampaign and include the prefetch of Benefits time_based_drops_prefetch = Prefetch( lookup="drops", queryset=active_time_based_drops.prefetch_related(benefits_prefetch), ) - - # Prefetch DropCampaigns for each Game and include the prefetch of TimeBasedDrops campaigns_prefetch = Prefetch( lookup="drop_campaigns", - queryset=active_campaigns.prefetch_related(time_based_drops_prefetch), + queryset=active_campaigns_query.prefetch_related(time_based_drops_prefetch), ) - # Query the games with the prefetched data - return Game.objects.filter(drop_campaigns__in=active_campaigns).prefetch_related(campaigns_prefetch).distinct() + return ( + Game.objects.filter(drop_campaigns__in=active_campaigns_query) + .distinct() + .prefetch_related(campaigns_prefetch) + .order_by("name") + ) def index(request: HttpRequest) -> HttpResponse: @@ -105,21 +65,19 @@ def index(request: HttpRequest) -> HttpResponse: Returns: HttpResponse: The response object """ - reward_campaigns: BaseManager[RewardCampaign] = get_reward_campaigns() - games: BaseManager[Game] = get_games_with_drops() - tocs: list[TOCItem] = [] - for game in games.all(): - game_name: str = game.name or "
Game name unknown
" - tocs.append(TOCItem(name=game_name, toc_id=f"#{game.twitch_id}")) + try: + reward_campaigns: BaseManager[RewardCampaign] = get_reward_campaigns() + games: BaseManager[Game] = get_games_with_drops() - toc: str = build_toc(tocs) + except Exception: + logger.exception("Error fetching reward campaigns or games.") + return HttpResponse(status=500) - context: dict[str, BaseManager[RewardCampaign] | str | BaseManager[Game]] = { + context: dict[str, Any] = { "reward_campaigns": reward_campaigns, "games": games, - "toc": toc, } - return TemplateResponse(request=request, template="index.html", context=context) + return TemplateResponse(request, "index.html", context) def game_view(request: HttpRequest) -> HttpResponse: diff --git a/static/js/index.js b/static/js/index.js deleted file mode 100644 index f103c32..0000000 --- a/static/js/index.js +++ /dev/null @@ -1,16 +0,0 @@ -// Create a new ScrollSpy instance that will track scroll position and update the active item in the table of contents -const scrollSpy = new bootstrap.ScrollSpy(document.body, { - target: '.toc' // The target element that contains the table of contents -}); - -// Listen for the 'activate.bs.scrollspy' event, which is triggered when a new item becomes active in the table of contents -// This is needed because the toc can be longer than our screen and we want to scroll the active item into view -document.body.addEventListener('activate.bs.scrollspy', function (event) { - // Find the currently active item in the table of contents - const activeItem = document.querySelector('.toc .active'); - - // If an active item is found, scroll it into view smoothly - if (activeItem) { - activeItem.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'nearest' }); - } -});