From 2c6d0f2613921252f9210102146b4337933fe894 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 2 Mar 2026 11:38:17 +0000 Subject: [PATCH] Add sorting functionality to normal cases list with user-selected options --- atlas/templates/atlas/normals_list.html | 12 ++++++--- atlas/views.py | 36 ++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/atlas/templates/atlas/normals_list.html b/atlas/templates/atlas/normals_list.html index 4a12c9c4..deb5fd2e 100644 --- a/atlas/templates/atlas/normals_list.html +++ b/atlas/templates/atlas/normals_list.html @@ -19,7 +19,13 @@
{% crispy filter.form %} -
+
+ + Clear
@@ -65,11 +71,11 @@ diff --git a/atlas/views.py b/atlas/views.py index 2332c1a8..2867511e 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -386,13 +386,47 @@ class NormalCaseList(LoginRequiredMixin, FilterView): filterset_class = NormalCaseFilter paginate_by = 25 + SORT_CHOICES = { + "newest": "-added_date", + "oldest": "added_date", + "age_asc": "age_days", + "age_desc": "-age_days", + "title_asc": "case__title", + "title_desc": "-case__title", + } + + def get_selected_sort(self): + sort_key = self.request.GET.get("sort", "newest") + if sort_key not in self.SORT_CHOICES: + return "newest" + return sort_key + def get_queryset(self): qs = NormalCase.objects.select_related("case", "examination", "modality") # Restrict similar to CaseFilter: non-editors only see authored or open_access if not self.request.user.groups.filter(name="atlas_editor").exists(): qs = qs.filter((Q(case__author__id=self.request.user.id) | Q(case__open_access=True))) - return qs.order_by("-added_date") + order_by = self.SORT_CHOICES[self.get_selected_sort()] + return qs.order_by(order_by) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["selected_sort"] = self.get_selected_sort() + context["sort_choices"] = [ + ("newest", "Newest added"), + ("oldest", "Oldest added"), + ("age_asc", "Age (youngest first)"), + ("age_desc", "Age (oldest first)"), + ("title_asc", "Title (A–Z)"), + ("title_desc", "Title (Z–A)"), + ] + + params = self.request.GET.copy() + if "page" in params: + params.pop("page") + context["querystring_without_page"] = params.urlencode() + return context @login_required @user_has_case_view_access