Move prefetching to own function
This commit is contained in:
@ -19,7 +19,7 @@ def render_campaign(campaign: RewardCampaign) -> SafeText:
|
|||||||
Returns:
|
Returns:
|
||||||
The rendered HTML string.
|
The rendered HTML string.
|
||||||
"""
|
"""
|
||||||
time_remaining = timesince(now(), campaign.ends_at)
|
time_remaining: str = timesince(now(), campaign.ends_at)
|
||||||
ends_in: str = f'{campaign.ends_at.strftime("%A %d %B %H:%M %Z")}' if campaign.ends_at else ""
|
ends_in: str = f'{campaign.ends_at.strftime("%A %d %B %H:%M %Z")}' if campaign.ends_at else ""
|
||||||
starts_in: str = f'{campaign.starts_at.strftime("%A %d %B %H:%M %Z")}' if campaign.starts_at else ""
|
starts_in: str = f'{campaign.starts_at.strftime("%A %d %B %H:%M %Z")}' if campaign.starts_at else ""
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ def render_game_card(game: Game) -> SafeText:
|
|||||||
box_art_url: str = game.box_art_url or "https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg"
|
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"
|
name: str = game.name or "Game name unknown"
|
||||||
slug: str = game.slug or "game-name-unknown"
|
slug: str = game.slug or "game-name-unknown"
|
||||||
drop_campaigns = game.drop_campaigns.all() # type: ignore # noqa: PGH003
|
drop_campaigns: list[DropCampaign] = game.drop_campaigns.all() # type: ignore # noqa: PGH003
|
||||||
return format_html(
|
return format_html(
|
||||||
"""
|
"""
|
||||||
<div class="card mb-4 shadow-sm" id="#{}">
|
<div class="card mb-4 shadow-sm" id="#{}">
|
||||||
@ -115,7 +115,7 @@ def render_drops(drops: list[TimeBasedDrop]) -> SafeText:
|
|||||||
benefits: list[Benefit] = drop.benefits.all() # type: ignore # noqa: PGH003
|
benefits: list[Benefit] = drop.benefits.all() # type: ignore # noqa: PGH003
|
||||||
for benefit in benefits:
|
for benefit in benefits:
|
||||||
image_url: str = benefit.image_url or "https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg"
|
image_url: str = benefit.image_url or "https://static-cdn.jtvnw.net/ttv-static/404_boxart.jpg"
|
||||||
name = benefit.name or "Drop name unknown"
|
name: str = benefit.name or "Drop name unknown"
|
||||||
drop_html += format_html(
|
drop_html += format_html(
|
||||||
"""
|
"""
|
||||||
<div class="col d-flex align-items-center position-relative">
|
<div class="col d-flex align-items-center position-relative">
|
||||||
|
@ -12,7 +12,7 @@ from django.template.response import TemplateResponse
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.views import View
|
from django.views import View
|
||||||
|
|
||||||
from core.models import DropCampaign, Game, RewardCampaign, Webhook
|
from core.models import Benefit, DropCampaign, Game, RewardCampaign, TimeBasedDrop, Webhook
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from django.db.models.manager import BaseManager
|
from django.db.models.manager import BaseManager
|
||||||
@ -53,6 +53,43 @@ def build_toc(list_of_things: list[TOCItem]) -> str:
|
|||||||
return html
|
return html
|
||||||
|
|
||||||
|
|
||||||
|
def get_reward_campaigns() -> BaseManager[RewardCampaign]:
|
||||||
|
"""Get the reward campaigns.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
BaseManager[RewardCampaign]: The reward campaigns.
|
||||||
|
"""
|
||||||
|
return RewardCampaign.objects.all().prefetch_related("rewards").order_by("-created_at")
|
||||||
|
|
||||||
|
|
||||||
|
def get_games_with_drops() -> BaseManager[Game]:
|
||||||
|
"""Get the games with drops.
|
||||||
|
|
||||||
|
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())
|
||||||
|
|
||||||
|
# Prefetch TimeBasedDrops for each DropCampaign and include the prefetch of Benefits
|
||||||
|
time_based_drops_prefetch = Prefetch(
|
||||||
|
lookup="drops",
|
||||||
|
queryset=TimeBasedDrop.objects.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),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Query the games with the prefetched data
|
||||||
|
return Game.objects.filter(drop_campaigns__in=active_campaigns).prefetch_related(campaigns_prefetch).distinct()
|
||||||
|
|
||||||
|
|
||||||
def index(request: HttpRequest) -> HttpResponse:
|
def index(request: HttpRequest) -> HttpResponse:
|
||||||
"""Render the index page.
|
"""Render the index page.
|
||||||
|
|
||||||
@ -62,19 +99,8 @@ def index(request: HttpRequest) -> HttpResponse:
|
|||||||
Returns:
|
Returns:
|
||||||
HttpResponse: The response object
|
HttpResponse: The response object
|
||||||
"""
|
"""
|
||||||
reward_campaigns: BaseManager[RewardCampaign] = (
|
reward_campaigns: BaseManager[RewardCampaign] = get_reward_campaigns()
|
||||||
RewardCampaign.objects.all()
|
games: BaseManager[Game] = get_games_with_drops()
|
||||||
.prefetch_related("rewards")
|
|
||||||
.filter(ends_at__gt=timezone.now(), starts_at__lt=timezone.now())
|
|
||||||
)
|
|
||||||
future_campaigns: BaseManager[DropCampaign] = DropCampaign.objects.filter(
|
|
||||||
ends_at__gt=timezone.now(),
|
|
||||||
starts_at__lt=timezone.now(),
|
|
||||||
)
|
|
||||||
|
|
||||||
games: BaseManager[Game] = Game.objects.all().prefetch_related(
|
|
||||||
Prefetch("drop_campaigns", queryset=future_campaigns.prefetch_related("drops__benefits")),
|
|
||||||
)
|
|
||||||
tocs: list[TOCItem] = []
|
tocs: list[TOCItem] = []
|
||||||
for game in games.all():
|
for game in games.all():
|
||||||
game_name: str = game.name or "<div class='text-muted'>Game name unknown</div>"
|
game_name: str = game.name or "<div class='text-muted'>Game name unknown</div>"
|
||||||
|
Reference in New Issue
Block a user