Add rarity field to trade_offer instead of looking up via cards

This commit is contained in:
badblocks 2025-03-16 19:06:36 -07:00
parent ba33139993
commit f7a9b2f823
13 changed files with 87 additions and 50 deletions

View file

@ -9,10 +9,12 @@ from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.template.response import TemplateResponse
from django.http import HttpResponseRedirect
from silk.profiling.profiler import silk_profile
class HomePageView(TemplateView):
template_name = "home/home.html"
@silk_profile(name='Home Page')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
@ -29,37 +31,40 @@ class HomePageView(TemplateView):
context["most_offered_cards"] = (
Card.objects_no_prefetch.filter(tradeofferhavecard__isnull=False)
.annotate(offer_count=Sum("tradeofferhavecard__quantity"))
.order_by("-offer_count", "?")[:6]
.order_by("-offer_count")[:6]
)
# Most Wanted Cards
context["most_wanted_cards"] = (
Card.objects_no_prefetch.filter(tradeofferwantcard__isnull=False)
.annotate(offer_count=Sum("tradeofferwantcard__quantity"))
.order_by("-offer_count", "?")[:6]
.order_by("-offer_count")[:6]
)
# Least Offered Cards
context["least_offered_cards"] = (
Card.objects_no_prefetch.annotate(offer_count=Sum("tradeofferhavecard__quantity"))
.order_by("offer_count", "?")[:6]
.order_by("offer_count")[:6]
)
featured = {}
# Featured "All" offers
featured["All"] = base_offer_qs.order_by("created_at")[:6]
# Get the normalized ids for rarities with pk<=5.
normalized_ids = list(
Rarity.objects.filter(pk__lte=5).values_list("normalized_id", flat=True).distinct()
)
rarity_map = {rarity.normalized_id: rarity.icons for rarity in Rarity.objects.filter(pk__lte=5)}
rarity_map = {
rarity.normalized_id: rarity.icons
for rarity in Rarity.objects.filter(pk__lte=5)
}
# For each normalized id (sorted descending), filter base offers that have a related card with that rarity.
# For each normalized id (sorted descending), filter base offers that have the matching trade offer rarity.
for norm in sorted(normalized_ids, reverse=True):
offers_qs = base_offer_qs.filter(
have_cards__rarity__normalized_id=norm
# or want cards, but all offers share the same rarity so checking have_cards is enough
# TODO: attach rarity to offer so we don't need to do this
rarity__normalized_id=norm # now using trade_offer.rarity
).order_by("created_at").distinct()[:6]
icon_label = rarity_map.get(norm)
if icon_label: