.
This commit is contained in:
+49
-3
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user