From e4bab4a50967e9e2ef446d53a9e5a24c62ff2e7d Mon Sep 17 00:00:00 2001 From: Ross Date: Tue, 11 Nov 2025 13:36:55 +0000 Subject: [PATCH] Add suggestion management for incorrect answers; implement collapsed view and HTMX support for dynamic updates --- .../partials/suggest_incorrect_collapsed.html | 16 ++++++++ .../partials/suggest_incorrect_fragment.html | 35 +++++++++-------- .../templates/anatomy/question_detail.html | 7 +--- anatomy/urls.py | 5 +++ anatomy/views.py | 38 +++++++++++++++++-- 5 files changed, 76 insertions(+), 25 deletions(-) create mode 100644 anatomy/templates/anatomy/partials/suggest_incorrect_collapsed.html diff --git a/anatomy/templates/anatomy/partials/suggest_incorrect_collapsed.html b/anatomy/templates/anatomy/partials/suggest_incorrect_collapsed.html new file mode 100644 index 00000000..93b7c7b6 --- /dev/null +++ b/anatomy/templates/anatomy/partials/suggest_incorrect_collapsed.html @@ -0,0 +1,16 @@ +
+ Answer suggest incorrect: +
+ {% if question.answer_suggest_incorrect %} + {% for s in question.answer_suggest_incorrect %} + {{ s }} + {% endfor %} + {% else %} + No suggestions + {% endif %} +
+ +
+ +
+
diff --git a/anatomy/templates/anatomy/partials/suggest_incorrect_fragment.html b/anatomy/templates/anatomy/partials/suggest_incorrect_fragment.html index ae9b3df7..8dfed57d 100644 --- a/anatomy/templates/anatomy/partials/suggest_incorrect_fragment.html +++ b/anatomy/templates/anatomy/partials/suggest_incorrect_fragment.html @@ -1,20 +1,25 @@
Suggest incorrect answers
-
+ {% csrf_token %}
- - {{ form.answer_suggest_incorrect }} + +
+ {% for s in question.answer_suggest_incorrect %} + + {% empty %} +
No suggestions
+ {% endfor %} +
+ +
+ + +
- - +
@@ -28,7 +33,7 @@
{{ c.answer }}
{{ c.count }} - +
{% endfor %} @@ -37,7 +42,7 @@
- + diff --git a/anatomy/templates/anatomy/question_detail.html b/anatomy/templates/anatomy/question_detail.html index 4716284b..2573decf 100644 --- a/anatomy/templates/anatomy/question_detail.html +++ b/anatomy/templates/anatomy/question_detail.html @@ -76,12 +76,7 @@ {% endif %}
- {% partialdef suggest-incorrect-form inline %} -
- Answer suggest incorrect: {{ question.answer_suggest_incorrect }} -
-
- {% endpartialdef %} + {% include 'anatomy/partials/suggest_incorrect_collapsed.html' %}
diff --git a/anatomy/urls.py b/anatomy/urls.py index 3b4d2d59..9e42e44c 100644 --- a/anatomy/urls.py +++ b/anatomy/urls.py @@ -38,6 +38,11 @@ urlpatterns = [ views.question_suggest_incorrect_answers, name="question_suggest_incorrect_answers", ), + path( + "question//suggest_incorrect_collapsed", + views.question_suggest_incorrect_collapsed, + name="question_suggest_incorrect_collapsed", + ), path("question//answer/", views.answer_question, name="answer_question"), path( diff --git a/anatomy/views.py b/anatomy/views.py index 2f52eb16..f5e8899e 100644 --- a/anatomy/views.py +++ b/anatomy/views.py @@ -1442,7 +1442,25 @@ def question_suggest_incorrect_answers(request, pk): # Support HTMX partial rendering with suggestion candidates and inline add if request.method == "POST": - # Support single-candidate add via HTMX (button posts 'suggestion') + # Support remove action from the edit fragment + if request.htmx and request.POST.get("remove"): + to_remove = request.POST.get("remove").strip() + if to_remove: + existing = [s for s in question.answer_suggest_incorrect if s] + if to_remove in existing: + existing = [s for s in existing if s != to_remove] + question.answer_suggest_incorrect = existing + question.save() + # Recompute candidates and render the edit fragment + form = SuggestIncorrectAnswerForm(instance=question) + candidates = build_candidates(question) + context = {"form": form, "question": question, "candidates": candidates} + return render(request, "anatomy/partials/suggest_incorrect_fragment.html", context) + + # Support single-candidate add via HTMX (button posts 'suggestion'). + # Edits should only be performed via the edit fragment; when a + # suggestion is posted we re-render the edit fragment so the + # user remains in edit mode and sees the updated candidate list. if request.htmx and request.POST.get("suggestion"): suggestion = request.POST.get("suggestion").strip() if suggestion: @@ -1451,7 +1469,7 @@ def question_suggest_incorrect_answers(request, pk): existing.append(suggestion) question.answer_suggest_incorrect = existing question.save() - # Recompute candidates and render fragment + # Recompute candidates and render the edit fragment form = SuggestIncorrectAnswerForm(instance=question) candidates = build_candidates(question) context = {"form": form, "question": question, "candidates": candidates} @@ -1464,9 +1482,13 @@ def question_suggest_incorrect_answers(request, pk): obj = form.save() obj.save() - # If HTMX requested, return the fragment so the UI can update in-place + # If HTMX requested, return the fragment or collapsed partial if request.htmx: - # Recompute candidates and render fragment + # If the close button was used, return the collapsed small partial + if request.POST.get("close"): + return render(request, "anatomy/partials/suggest_incorrect_collapsed.html", {"question": question}) + + # Otherwise re-render the edit fragment so the user stays in edit mode form = SuggestIncorrectAnswerForm(instance=question) candidates = build_candidates(question) context = {"form": form, "question": question, "candidates": candidates} @@ -1487,6 +1509,14 @@ def question_suggest_incorrect_answers(request, pk): return HttpResponse("Error", status=400) + +@user_is_author_or_anatomy_checker +def question_suggest_incorrect_collapsed(request, pk): + """Return the small collapsed 'Edit suggestions' box as a partial.""" + question = get_object_or_404(AnatomyQuestion, pk=pk) + context = {"question": question} + return render(request, "anatomy/partials/suggest_incorrect_collapsed.html", context) + @user_is_author_or_anatomy_checker def question_save_annotation(request, pk): if request.method == "POST":