From 3b920e819e15fb35bdda99c1c3f8aa4bbd15e530 Mon Sep 17 00:00:00 2001 From: Ross Date: Fri, 5 Dec 2025 14:42:25 +0000 Subject: [PATCH] . --- cards/templates/cards/list.html | 52 +++++++++++++++++++++++++++++++++ cards/views.py | 52 +++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/cards/templates/cards/list.html b/cards/templates/cards/list.html index 22eb8c9..1eb98e8 100644 --- a/cards/templates/cards/list.html +++ b/cards/templates/cards/list.html @@ -17,6 +17,58 @@
+
+
+
+ +
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+ +
+
+
+
{% include 'cards/_cards_list.html' %}
diff --git a/cards/views.py b/cards/views.py index 75c2039..2e1bae7 100644 --- a/cards/views.py +++ b/cards/views.py @@ -13,11 +13,57 @@ def is_htmx(request): def card_list(request): - cards = Dinosaur.objects.all() + # Basic filtering/search/sort via GET params so the UI can request + # updated lists using HTMX or a regular browser GET. + qs = Dinosaur.objects.all() + + # Search by name + q = (request.GET.get('q') or '').strip() + if q: + qs = qs.filter(name__icontains=q) + + # Filter by era and diet + era = request.GET.get('era') + if era and era != 'all': + qs = qs.filter(era=era) + diet = request.GET.get('diet') + if diet and diet != 'all': + qs = qs.filter(diet=diet) + + # Sorting: allow a small whitelist of fields to avoid unexpected orders + sort_map = { + 'name': 'name', + 'created': 'created_at', + 'speed': 'speed', + 'weight': 'weight', + 'height': 'height', + 'intelligence': 'intelligence', + } + sort_by = request.GET.get('sort') + order = request.GET.get('order', 'asc') + if sort_by in sort_map: + field = sort_map[sort_by] + if order == 'desc': + field = '-' + field + qs = qs.order_by(field) + background = BackgroundImage.get_active() if BackgroundImage.objects.exists() else None + + context = { + 'cards': qs, + 'background': background, + 'eras': Dinosaur.ERA_CHOICES, + 'diets': Dinosaur.DIET_CHOICES, + 'current_q': q, + 'current_era': era or 'all', + 'current_diet': diet or 'all', + 'current_sort': sort_by or '', + 'current_order': order, + } + if is_htmx(request): - return render(request, 'cards/_cards_list.html', {'cards': cards, 'background': background}) - return render(request, 'cards/list.html', {'cards': cards, 'background': background}) + return render(request, 'cards/_cards_list.html', context) + return render(request, 'cards/list.html', context) def card_create(request):