From d342e7e04777c7ef61d53ea3527bdff4867ac9d4 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 20 Oct 2025 21:01:03 +0100 Subject: [PATCH] Implement exam selection feature with HTMX support in exam edit view and add exam selection fragment template --- generic/views.py | 26 +++++++++++++++++++ .../partials/exam_select_fragment.html | 23 ++++++++++++++++ templates/question_table_view.html | 6 ++--- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 templates/generic/partials/exam_select_fragment.html diff --git a/generic/views.py b/generic/views.py index 25734508..a04c5824 100644 --- a/generic/views.py +++ b/generic/views.py @@ -433,6 +433,22 @@ def get_examination_id(request): # Generic view to dispatch to indivuals app views @login_required def generic_exam_json_edit(request): + # GET: return a small fragment with an exam selector for the given type + if request.method == "GET": + question_type = request.GET.get("type") + if not question_type: + return JsonResponse({"status": "error", "message": "missing type"}, status=400) + try: + Exam = get_exam_model_from_app_name(question_type) + except Exception: + return JsonResponse({"status": "error", "message": "unknown type"}, status=400) + + # Limit to exams that are not archived and are exam_mode + exams = Exam.objects.filter(archive=False, exam_mode=True).order_by("name")[:200] + return render(request, "generic/partials/exam_select_fragment.html", {"exams": exams, "type": question_type}) + + # Support POST additions via either an explicit JSON 'add_exam_questions' or + # via a list of checkboxes named 'selection' sent by HTMX from the question list. if request.method == "POST": question_type = request.POST.get("type") @@ -468,6 +484,16 @@ def generic_exam_json_edit(request): if "add_exam_questions" in request.POST: question_ids = json.loads(request.POST.get("add_exam_questions")) + else: + # Support HTMX flow where checkboxes named 'selection' are posted + # (multiple values). Convert to list of ints. + sel = request.POST.getlist("selection") or request.POST.getlist("selection[]") + question_ids = [] + for s in sel: + try: + question_ids.append(int(s)) + except Exception: + continue # question_objects = Question.objects.filter(pk__in=question_ids) add_count = 0 diff --git a/templates/generic/partials/exam_select_fragment.html b/templates/generic/partials/exam_select_fragment.html new file mode 100644 index 00000000..fbf517fb --- /dev/null +++ b/templates/generic/partials/exam_select_fragment.html @@ -0,0 +1,23 @@ +
+
+
+ +
+ + +
+
+ +
(will be taken from selected checkboxes)
+
+
+ + +
+
+
+
\ No newline at end of file diff --git a/templates/question_table_view.html b/templates/question_table_view.html index e429d537..e70f9fe1 100644 --- a/templates/question_table_view.html +++ b/templates/question_table_view.html @@ -21,9 +21,9 @@
+ hx-get="{% url 'generic:generic_exam_json_edit' %}?type={{ app_name }}" + hx-target="#action-result" + hx-swap="innerHTML">Add to exam