From 279d6344f1e9261d958ce35e72320017d34bc226 Mon Sep 17 00:00:00 2001 From: Ross Date: Sun, 16 Nov 2025 23:16:06 +0000 Subject: [PATCH] Enhance case search functionality: display recent cases in search results and adjust initial load behavior --- .../atlas/partials/case_search_results.html | 59 +++++++++++++------ .../atlas/partials/case_search_widget.html | 6 +- atlas/views.py | 27 +++++++-- 3 files changed, 70 insertions(+), 22 deletions(-) diff --git a/atlas/templates/atlas/partials/case_search_results.html b/atlas/templates/atlas/partials/case_search_results.html index 92eac272..cab4deba 100644 --- a/atlas/templates/atlas/partials/case_search_results.html +++ b/atlas/templates/atlas/partials/case_search_results.html @@ -1,20 +1,45 @@ -{% if cases and cases.exists %} -
- {% for case in cases %} -
-
-
{{ case.title }}
-
{% if case.description %}{{ case.description|truncatechars:100 }}{% endif %}
-
+{# Search results #} +{% if q %} + {% if cases and cases.exists %} +
+ {% for case in cases %} +
+
+
{{ case.title }}
+
{% if case.description %}{{ case.description|truncatechars:100 }}{% endif %}
+
-
- {% csrf_token %} - - -
-
- {% endfor %} +
+ {% csrf_token %} + + +
+
+ {% endfor %} +
+ {% else %} +
No cases found.
+ {% endif %} +{% endif %} + +{# Recent cases quick-add (rendered below search results) #} +{% if recent_cases and recent_cases|length > 0 %} +
+
Recently created
+
+ {% for case in recent_cases %} +
+
+
{{ case.title }}
+
{% if case.description %}{{ case.description|truncatechars:100 }}{% endif %}
+
+
+ {% csrf_token %} + + +
+
+ {% endfor %} +
-{% else %} -
No cases found.
{% endif %} diff --git a/atlas/templates/atlas/partials/case_search_widget.html b/atlas/templates/atlas/partials/case_search_widget.html index b334c1a5..ec1a3ca5 100644 --- a/atlas/templates/atlas/partials/case_search_widget.html +++ b/atlas/templates/atlas/partials/case_search_widget.html @@ -7,5 +7,9 @@ hx-trigger="keyup changed delay:400ms" autocomplete="off"> -
+
+ {# Render initial results (search + recent) on initial GET; HTMX searches will replace this div with partial results #} + {% include 'atlas/partials/case_search_results.html' with cases=cases recent_cases=recent_cases collection=collection %} +
+ diff --git a/atlas/views.py b/atlas/views.py index 1a737218..be365e65 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -1173,12 +1173,18 @@ def add_case_to_collection(request, collection_id): # if request.method == "POST": - cases = get_cases_available_to_user(request.user) + # don't show search results on initial load; the search partial will render only when a query exists + cases = Case.objects.none() + + # Also compute recent cases for initial display (authored by current user and not in collection) + recent_qs = get_cases_available_to_user(request.user).filter(author=request.user) + recent_qs = recent_qs.exclude(pk__in=collection.cases.values_list("pk", flat=True)) + recent_cases = recent_qs.order_by("-created_date")[:5] return render( request, "atlas/add_case_to_collection.html", - {"cases": cases, "collection": collection}, + {"cases": cases, "collection": collection, "recent_cases": recent_cases, "q": ""}, ) @@ -1201,9 +1207,22 @@ def case_search(request): if q: qs = qs.filter(title__icontains=q) - cases = qs.order_by("title")[:30] + # Recent cases: last 5 created that are available to the user and not already in the collection + # Restrict recent cases to those authored by the current user and not already in the collection + recent_qs = get_cases_available_to_user(request.user).filter(author=request.user) + if collection is not None: + recent_qs = recent_qs.exclude(pk__in=collection.cases.values_list("pk", flat=True)) + recent_cases = recent_qs.order_by("-created_date")[:5] - return render(request, "atlas/partials/case_search_results.html", {"cases": cases, "collection": collection}) + # Exclude recent cases from the main search results to avoid duplication + recent_pks = [c.pk for c in recent_cases] + cases = qs.exclude(pk__in=recent_pks).order_by("title")[:30] + + return render( + request, + "atlas/partials/case_search_results.html", + {"cases": cases, "collection": collection, "recent_cases": recent_cases, "q": q}, + ) @login_required