Fix pagination controls, move mixin to common app, fix pagination invocation on all views, and other random bug fixes

This commit is contained in:
badblocks 2025-04-01 23:01:05 -07:00
parent 7edefe23c3
commit 6a61b79bbe
425 changed files with 51656 additions and 243 deletions

View file

@ -3,7 +3,7 @@ from django.urls import reverse_lazy
from django.views.generic import UpdateView, DeleteView, CreateView, ListView, DetailView
from cards.models import Card
from trades.models import TradeOffer
from cards.mixins import ReusablePaginationMixin
from common.mixins import ReusablePaginationMixin
class CardDetailView(DetailView):
model = Card
@ -65,7 +65,7 @@ class TradeOfferWantCardListView(ListView):
class CardListView(ReusablePaginationMixin, ListView):
model = Card
paginate_by = 36 # For non-grouped mode; grouping mode will override default pagination.
# Removed built-in pagination; using custom mixin instead
context_object_name = "cards"
def get_template_names(self):
@ -88,13 +88,6 @@ class CardListView(ReusablePaginationMixin, ListView):
qs = qs.order_by(ordering)
return qs.prefetch_related("decks").distinct()
def get_paginate_by(self, queryset):
group_by = self.request.GET.get("group_by")
if group_by in ("deck", "cardset", "rarity"):
# When grouping is enabled, we want to paginate manually so disable default pagination.
return None
return self.paginate_by
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
order = self.request.GET.get("order", "absolute")
@ -106,7 +99,6 @@ class CardListView(ReusablePaginationMixin, ListView):
full_qs = self.get_queryset()
all_cards = list(full_qs)
flat_cards = []
if group_by == "deck":
for card in all_cards:
for deck in card.decks.all():
@ -121,16 +113,10 @@ class CardListView(ReusablePaginationMixin, ListView):
flat_cards.append({"group": card.rarity_icon, "sort_group": card.rarity_level, "card": card})
flat_cards.sort(key=lambda x: x["sort_group"], reverse=True)
try:
page_number = int(self.request.GET.get("page", 1))
except ValueError:
page_number = 1
# Use our custom mixin logic here
page_number = self.get_page_number()
self.per_page = 36
page_flat_cards, pagination_context = self.paginate_data(flat_cards, page_number)
# Reassemble the flat list into groups for the current page.
page_groups = []
for item in page_flat_cards:
group_value = item["group"]
@ -143,19 +129,11 @@ class CardListView(ReusablePaginationMixin, ListView):
context["page_obj"] = pagination_context
context["total_cards"] = len(flat_cards)
context["object_list"] = full_qs
return context
else:
# For non-grouped mode, transform the built-in paginator page
if "page_obj" in context:
page = context["page_obj"]
# Create a unified pagination context dict with updated keys
custom_page_obj = {
"number": page.number,
"has_previous": page.has_previous(),
"has_next": page.has_next(),
"previous_page_number": page.previous_page_number() if page.has_previous() else 1,
"next_page_number": page.next_page_number() if page.has_next() else page.paginator.num_pages,
"paginator": {"num_pages": page.paginator.num_pages},
}
context["page_obj"] = custom_page_obj
return context
page_number = self.get_page_number()
self.per_page = 36
paginated_cards, pagination_context = self.paginate_data(self.get_queryset(), page_number)
context["cards"] = paginated_cards
context["page_obj"] = pagination_context
context["object_list"] = self.get_queryset()
return context