Various small bug fixes, break out pagination for cards into its own mixin and templatetag
This commit is contained in:
parent
05a279fa3a
commit
138a929da6
17 changed files with 225 additions and 136 deletions
|
|
@ -3,6 +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
|
||||
|
||||
class CardDetailView(DetailView):
|
||||
model = Card
|
||||
|
|
@ -62,9 +63,9 @@ class TradeOfferWantCardListView(ListView):
|
|||
context['side'] = 'want'
|
||||
return context
|
||||
|
||||
class CardListView(ListView):
|
||||
class CardListView(ReusablePaginationMixin, ListView):
|
||||
model = Card
|
||||
paginate_by = 100 # For non-grouped mode; grouping mode will override default pagination.
|
||||
paginate_by = 36 # For non-grouped mode; grouping mode will override default pagination.
|
||||
context_object_name = "cards"
|
||||
|
||||
def get_template_names(self):
|
||||
|
|
@ -102,13 +103,11 @@ class CardListView(ListView):
|
|||
context["group_by"] = group_by
|
||||
|
||||
if group_by in ("deck", "cardset", "rarity"):
|
||||
# Fetch the complete queryset (no slicing)
|
||||
full_qs = self.get_queryset()
|
||||
all_cards = list(full_qs)
|
||||
flat_cards = []
|
||||
|
||||
if group_by == "deck":
|
||||
# Each card may belong to multiple decks – reproduce the existing logic.
|
||||
for card in all_cards:
|
||||
for deck in card.decks.all():
|
||||
flat_cards.append({"group": deck.name, "card": card})
|
||||
|
|
@ -119,23 +118,19 @@ class CardListView(ListView):
|
|||
flat_cards.sort(key=lambda x: x["group"].lower())
|
||||
elif group_by == "rarity":
|
||||
for card in all_cards:
|
||||
flat_cards.append({"group": card.rarity_level, "card": card})
|
||||
flat_cards.sort(key=lambda x: x["group"], reverse=True)
|
||||
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)
|
||||
|
||||
total_cards = len(flat_cards)
|
||||
try:
|
||||
page_number = int(self.request.GET.get("page", 1))
|
||||
if page_number < 1:
|
||||
page_number = 1
|
||||
except ValueError:
|
||||
page_number = 1
|
||||
|
||||
per_page = 96
|
||||
start = (page_number - 1) * per_page
|
||||
end = page_number * per_page
|
||||
page_flat_cards = flat_cards[start:end]
|
||||
# Use our custom mixin logic here
|
||||
self.per_page = 36
|
||||
page_flat_cards, pagination_context = self.paginate_data(flat_cards, page_number)
|
||||
|
||||
# Reassemble the flat list into grouped structure for just this page.
|
||||
# Reassemble the flat list into groups for the current page.
|
||||
page_groups = []
|
||||
for item in page_flat_cards:
|
||||
group_value = item["group"]
|
||||
|
|
@ -145,25 +140,22 @@ class CardListView(ListView):
|
|||
else:
|
||||
page_groups.append({"group": group_value, "cards": [card_obj]})
|
||||
context["groups"] = page_groups
|
||||
|
||||
# Set up custom pagination context.
|
||||
from math import ceil
|
||||
num_pages = ceil(total_cards / per_page)
|
||||
page_obj = {
|
||||
"number": page_number,
|
||||
"has_previous": page_number > 1,
|
||||
"has_next": page_number < num_pages,
|
||||
"previous_page_number": page_number - 1 if page_number > 1 else None,
|
||||
"next_page_number": page_number + 1 if page_number < num_pages else None,
|
||||
"paginator": {
|
||||
"num_pages": num_pages,
|
||||
},
|
||||
}
|
||||
context["page_obj"] = page_obj
|
||||
context["is_paginated"] = total_cards > per_page
|
||||
context["total_cards"] = total_cards
|
||||
# Optionally, keep the full queryset in object_list.
|
||||
context["page_obj"] = pagination_context
|
||||
context["total_cards"] = len(flat_cards)
|
||||
context["object_list"] = full_qs
|
||||
return context
|
||||
|
||||
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
|
||||
custom_page_obj = {
|
||||
"number": page.number,
|
||||
"has_previous": page.has_previous(),
|
||||
"has_next": page.has_next(),
|
||||
"previous_page": page.previous_page_number() if page.has_previous() else 1,
|
||||
"next_page": 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue