Add suggestion management for incorrect answers; implement collapsed view and HTMX support for dynamic updates

This commit is contained in:
Ross
2025-11-11 13:36:55 +00:00
parent 6e7135823a
commit e4bab4a509
5 changed files with 76 additions and 25 deletions
+34 -4
View File
@@ -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":