feat(purchases): honor ?sort= on list_purchases + eager-load games (#68)

This commit is contained in:
2026-06-21 14:11:45 +02:00
parent 6c704c40a3
commit 939d3a9e33
2 changed files with 60 additions and 2 deletions
+12 -1
View File
@@ -44,6 +44,12 @@ from common.time import dateformat
from common.utils import paginate
from games.forms import PurchaseForm
from games.models import Game, Purchase
from games.sorting import (
PURCHASE_DEFAULT_SORT,
PURCHASE_SORTS,
apply_sort,
parse_find_filter,
)
from games.views.general import use_custom_redirect
@@ -119,7 +125,7 @@ def _render_purchase_row(purchase):
@login_required
def list_purchases(request: HttpRequest) -> HttpResponse:
purchases = Purchase.objects.order_by("-date_purchased", "-created_at")
purchases = Purchase.objects.prefetch_related("games", "games__platform")
filter_json = request.GET.get("filter", "")
if filter_json:
@@ -129,6 +135,11 @@ def list_purchases(request: HttpRequest) -> HttpResponse:
if pf is not None:
purchases = purchases.filter(pf.to_q())
sort = apply_sort(purchases, parse_find_filter(request), PURCHASE_SORTS, PURCHASE_DEFAULT_SORT)
purchases = sort.queryset
for key in sort.unknown:
messages.warning(request, f"Unknown sort field '{key}' was ignored.")
purchases, page_obj, elided_page_range = paginate(request, purchases)
data = {