From 9d57d03cbb5cc0ac7e453619b2e8365a2dc5a14c Mon Sep 17 00:00:00 2001 From: Ross Date: Thu, 20 Nov 2025 21:59:50 +0000 Subject: [PATCH] Refactor synonym management: extract synonyms list and add forms for adding synonyms with HTMX support --- atlas/templates/atlas/condition_detail.html | 30 ++--------- .../atlas/partials/_synonyms_list.html | 54 +++++++++++++++++++ atlas/views.py | 14 +++++ 3 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 atlas/templates/atlas/partials/_synonyms_list.html diff --git a/atlas/templates/atlas/condition_detail.html b/atlas/templates/atlas/condition_detail.html index b56332d3..24b03640 100755 --- a/atlas/templates/atlas/condition_detail.html +++ b/atlas/templates/atlas/condition_detail.html @@ -31,33 +31,9 @@
Synonyms
- {% for syn in condition.synonym.all %} - {{ syn }} - {% empty %} - — - {% endfor %} - {% if request.user.is_authenticated and can_merge %} - -
-
-
- {% csrf_token %} - -
- - {{ synonym_form.condition }} -
-
- - -
- -
-
-
- {% endif %} +
+ {% include 'atlas/partials/_synonyms_list.html' %} +
Parent
diff --git a/atlas/templates/atlas/partials/_synonyms_list.html b/atlas/templates/atlas/partials/_synonyms_list.html new file mode 100644 index 00000000..c3a99c84 --- /dev/null +++ b/atlas/templates/atlas/partials/_synonyms_list.html @@ -0,0 +1,54 @@ +{% comment %}Partial that renders the synonyms list + add form. Used by condition_detail and HTMX responses.{% endcomment %} +{% load static %} + +{% if condition.synonym.all %} + {% for syn in condition.synonym.all %} + {{ syn }} + {% endfor %} +{% else %} + — +{% endif %} + +{% if request.user.is_authenticated and can_merge %} +
+ + + + + +
+ + +
+
+
+ {% csrf_token %} + +
+ + {{ synonym_form.condition }} +
+ +
+
+
+ + +
+
+
+ {% csrf_token %} + +
+ + +
+ +
+
+
+{% endif %} diff --git a/atlas/views.py b/atlas/views.py index 9f92498c..7bb09520 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -860,6 +860,9 @@ def condition_detail(request, pk): if form.is_valid(): other = form.cleaned_data.get("condition") + # keep the form (possibly with errors) to render back in HTMX responses + synonym_form = form + # If no autocomplete selection, accept a free-text name synonym_name = request.POST.get("synonym_name") if not other and synonym_name: @@ -874,6 +877,17 @@ def condition_detail(request, pk): condition.synonym.add(other) other.synonym.add(condition) + # If this was an HTMX request return the updated synonyms partial so the + # page can be updated without a full redirect. Otherwise perform the + # existing redirect back to the detail page. + if request.htmx: + html = render_to_string( + "atlas/partials/_synonyms_list.html", + {"condition": condition, "synonym_form": synonym_form, "can_merge": can_merge}, + request=request, + ) + return HttpResponse(html) + return redirect("atlas:condition_detail", pk=condition.pk) # logging.debug(atlas.subspecialty.first().name.all())