From 0098fa5dfc54fe55fad4b5eb87db7b16cef35eb6 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 17 Nov 2025 09:27:02 +0000 Subject: [PATCH] Refactor case prior display: extract prior card markup into a separate partial for improved maintainability and readability --- .../atlas/collection_case_priors.html | 68 +------------------ .../templates/atlas/partials/_prior_card.html | 67 ++++++++++++++++++ atlas/views.py | 64 +++++++++++++---- 3 files changed, 119 insertions(+), 80 deletions(-) create mode 100644 atlas/templates/atlas/partials/_prior_card.html diff --git a/atlas/templates/atlas/collection_case_priors.html b/atlas/templates/atlas/collection_case_priors.html index 089cfd32..e75a2fcd 100644 --- a/atlas/templates/atlas/collection_case_priors.html +++ b/atlas/templates/atlas/collection_case_priors.html @@ -38,73 +38,7 @@
{% for case, added, relation, visibility in available_priors %} -
-
-
-
-
{{case.title}}
-
- {% if added %} - Added - {% else %} - Available - {% endif %} -
-
- -

Relation: {{ relation }}

- -
{{case.get_series_blocks|safe}}
- -
-
- View case -
- - {% if not added %} -
- {% csrf_token %} - -
- -
-
- -
-
- -
-
- {% else %} -
- {% csrf_token %} - -
Current relation: {{relation}}
-
- - -
-
- {% endif %} - -
-
- - -
-
+ {% include 'atlas/partials/_prior_card.html' with case=case added=added relation=relation visibility=visibility case_detail=case_detail collection=collection %} {% endfor %}
diff --git a/atlas/templates/atlas/partials/_prior_card.html b/atlas/templates/atlas/partials/_prior_card.html new file mode 100644 index 00000000..f013a78b --- /dev/null +++ b/atlas/templates/atlas/partials/_prior_card.html @@ -0,0 +1,67 @@ +
+
+
+
+
{{case.title}}
+
+ {% if added %} + Added + {% else %} + Available + {% endif %} +
+
+ +

Relation: {{ relation }}

+ +
{{case.get_series_blocks|safe}}
+ +
+
+ View case +
+ + {% if not added %} +
+ {% csrf_token %} + +
+ +
+
+ +
+
+ +
+
+ {% else %} +
+ {% csrf_token %} + +
Current relation: {{relation}}
+
+ + +
+
+ {% endif %} + +
+
+ + +
+
diff --git a/atlas/views.py b/atlas/views.py index 740fd600..776da8a3 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -2758,33 +2758,71 @@ def collection_take_start(request, pk, cid=None, passcode=None): @user_is_collection_author_or_atlas_editor def collection_case_priors(request, exam_id, case_id): case_detail = CaseDetail.objects.get(case=case_id, collection=exam_id) + collection = case_detail.collection if request.htmx: + # Ensure we can render the updated single-card partial and return it so HTMX + # can swap the card on the client side. if "remove" in request.POST: - p = CasePrior.objects.get( - case_detail=case_detail, prior_case=request.POST["remove"] + prior_pk = request.POST["remove"] + try: + p = CasePrior.objects.get(case_detail=case_detail, prior_case=prior_pk) + p.delete() + except CasePrior.DoesNotExist: + # Already removed; continue and render the not-added card + pass + + prior_case = Case.objects.get(pk=prior_pk) + added = False + relation = "" + visibility = "AL" + + html = render_to_string( + "atlas/partials/_prior_card.html", + { + "case": prior_case, + "added": added, + "relation": relation, + "visibility": visibility, + "case_detail": case_detail, + "collection": collection, + }, + request=request, ) - p.delete() - return HttpResponse(f"Case removed") + return HttpResponse(html) elif "prior_case_id" in request.POST: - if not request.POST["relation"]: + if not request.POST.get("relation"): return HttpResponse( - "You need to enter text to describe the relationship between the cases" + "You need to enter text to describe the relationship between the cases", + status=400, ) prior_case = Case.objects.get(pk=request.POST["prior_case_id"]) p, created = CasePrior.objects.get_or_create( case_detail=case_detail, prior_case=prior_case ) - p.relation_text = request.POST["relation"] - - p.prior_visibility = request.POST["prior_visibility"] - + p.relation_text = request.POST.get("relation", "") + p.prior_visibility = request.POST.get("prior_visibility", "AL") p.save() - return HttpResponse(f"Case added") + added = True + relation = p.relation_text + visibility = p.prior_visibility + + html = render_to_string( + "atlas/partials/_prior_card.html", + { + "case": prior_case, + "added": added, + "relation": relation, + "visibility": visibility, + "case_detail": case_detail, + "collection": collection, + }, + request=request, + ) + return HttpResponse(html) else: - return HttpResponse("False") - raise Http404() + return HttpResponse("False", status=400) collection = case_detail.collection