From 01f67f3e327224674a98aa5cf23fd2c13187fca4 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 20 Oct 2025 11:01:47 +0100 Subject: [PATCH] Enhance LLM question import process with preview and confirmation steps, including error handling and M2M record creation --- sbas/templates/sbas/import_llm_questions.html | 10 +- .../templates/sbas/partials/import_error.html | 8 ++ .../sbas/partials/import_preview.html | 42 ++++++ .../sbas/partials/import_result.html | 21 +++ sbas/urls.py | 5 + sbas/views.py | 129 +++++++++++++++++- 6 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 sbas/templates/sbas/partials/import_error.html create mode 100644 sbas/templates/sbas/partials/import_preview.html create mode 100644 sbas/templates/sbas/partials/import_result.html diff --git a/sbas/templates/sbas/import_llm_questions.html b/sbas/templates/sbas/import_llm_questions.html index 37dfab34..28af2f12 100644 --- a/sbas/templates/sbas/import_llm_questions.html +++ b/sbas/templates/sbas/import_llm_questions.html @@ -2,10 +2,14 @@ {% block content %}

Import LLM Questions

-
+ {% csrf_token %} {{ form.as_p }} - +
+ + +
-

Upload a single JSON array, a single object, or JSONL (one JSON object per line) matching the agreed schema.

+

Upload a single JSON array, a single object, or JSONL (one JSON object per line) matching the agreed schema. Use the Preview button to inspect results before importing.

+
{% endblock %} diff --git a/sbas/templates/sbas/partials/import_error.html b/sbas/templates/sbas/partials/import_error.html new file mode 100644 index 00000000..10f3e423 --- /dev/null +++ b/sbas/templates/sbas/partials/import_error.html @@ -0,0 +1,8 @@ +
+
Import error
+ +
diff --git a/sbas/templates/sbas/partials/import_preview.html b/sbas/templates/sbas/partials/import_preview.html new file mode 100644 index 00000000..ea8f7a5a --- /dev/null +++ b/sbas/templates/sbas/partials/import_preview.html @@ -0,0 +1,42 @@ +{% for item in items %} +
+
+
Question {{ item.index }}
+ {% with payload=payloads|slice:item.index|first %} + {% if payload %} +

Stem: {{ payload.stem|default:''|safe }}

+

Answers: +

    +
  • A: {{ payload.a_answer|default:''|safe }}
  • +
  • B: {{ payload.b_answer|default:''|safe }}
  • +
  • C: {{ payload.c_answer|default:''|safe }}
  • +
  • D: {{ payload.d_answer|default:''|safe }}
  • +
  • E: {{ payload.e_answer|default:''|safe }}
  • +
+

+ {% endif %} + {% endwith %} +

Missing M2M: + {% if item.missing_m2m %} +

    + {% for m in item.missing_m2m %} +
  • {{ m.model }}: {{ m.values|join:", " }}
  • + {% endfor %} +
+ {% else %} + None + {% endif %} +

+
+ +
+
+
+{% endfor %} + +
+ + +
+ +
diff --git a/sbas/templates/sbas/partials/import_result.html b/sbas/templates/sbas/partials/import_result.html new file mode 100644 index 00000000..f91a1bd7 --- /dev/null +++ b/sbas/templates/sbas/partials/import_result.html @@ -0,0 +1,21 @@ +
Imported {{ created }} questions.
+{% if errors %} +
+
Errors
+ +
+{% endif %} +{% if actually_created %} +
+
Created M2M records
+ +
+{% endif %} diff --git a/sbas/urls.py b/sbas/urls.py index 6fb3ee3d..a1f8a334 100644 --- a/sbas/urls.py +++ b/sbas/urls.py @@ -94,6 +94,11 @@ urlpatterns = [ views.import_llm_questions, name="import_llm_questions", ), + path( + "import_llm_confirm/", + views.import_llm_confirm, + name="import_llm_confirm", + ), #path( # "exam//scores///", # views.exam_scores_cid_user, diff --git a/sbas/views.py b/sbas/views.py index 5f42d70c..78e4047a 100644 --- a/sbas/views.py +++ b/sbas/views.py @@ -35,6 +35,7 @@ import re import logging logger = logging.getLogger(__name__) +import uuid from loguru import logger @@ -761,4 +762,130 @@ def import_llm_questions(request): for k, v in aggregated_actually_created.items(): report["actually_created_m2m"][k] = v - return JsonResponse({"ok": True, "report": report}) \ No newline at end of file + # If this was a dry-run and the request came from HTMX, render a preview partial and store the parsed candidates in session + if dry_run and request.headers.get("HX-Request"): + session_key = f"llm_import_{uuid.uuid4().hex}" + # store minimal necessary data in session (the raw payloads) + request.session[session_key] = candidates + request.session.modified = True + preview_ctx = { + "report": report, + "items": report["per_item"], + "payloads": candidates, + "session_key": session_key, + "allow_create_m2m": allow_create_m2m, + } + return render(request, "sbas/partials/import_preview.html", preview_ctx) + + # Non-HTMX callers get JSON + return JsonResponse({"ok": True, "report": report}) + + +@login_required +@user_passes_test(lambda u: u.is_superuser) +@require_http_methods(["POST"]) +def import_llm_confirm(request): + """Confirm import of previously-previewed candidates stored in session. + + Expects POST with 'session_key' and 'selected' (comma-separated indices) and optional allow_create_m2m. + Returns an HTML fragment suitable for HTMX replacement summarising the import results. + """ + session_key = request.POST.get("session_key") + # support both a comma-separated 'selected' string or multiple 'selected' values + selected_raw = request.POST.get("selected") + selected_list = request.POST.getlist("selected") + allow_create_m2m = request.POST.get("allow_create_m2m") in ("on", "true", "True", "1") + if selected_list: + # selected_list contains strings of indices + selected = ",".join(selected_list) + else: + selected = selected_raw + if not session_key or session_key not in request.session: + return render(request, "sbas/partials/import_error.html", {"errors": ["Session expired or invalid; please re-run preview."]}, status=400) + + candidates = request.session.get(session_key, []) + indices = [] + if selected: + for part in selected.split(','): + try: + indices.append(int(part)) + except Exception: + continue + + # default: import all if none specified + if not indices: + indices = list(range(len(candidates))) + + # perform actual import for selected indices + from atlas.models import Finding, Structure, Condition, Presentation, Subspecialty + + created = 0 + errors = [] + actually_created = defaultdict(list) + for idx in indices: + try: + payload = candidates[idx] + except Exception: + errors.append({"index": idx, "errors": ["Missing candidate"]}) + continue + + try: + with transaction.atomic(): + q = Question() + q.stem = payload.get("stem", "").strip() + title = payload.get("title") + if title: + q.stem = f"{title}\n" + q.stem + q.a_answer = payload.get("a_answer", "").strip() + q.a_feedback = payload.get("a_feedback", "") + q.b_answer = payload.get("b_answer", "").strip() + q.b_feedback = payload.get("b_feedback", "") + q.c_answer = payload.get("c_answer", "").strip() + q.c_feedback = payload.get("c_feedback", "") + q.d_answer = payload.get("d_answer", "").strip() + q.d_feedback = payload.get("d_feedback", "") + q.e_answer = payload.get("e_answer", "").strip() + q.e_feedback = payload.get("e_feedback", "") + q.feedback = payload.get("feedback", "") + q.best_answer = payload.get("best_answer") + cat_val = payload.get("category") + if isinstance(cat_val, str): + cat_obj, _ = Category.objects.get_or_create(category=cat_val) + q.category = cat_obj + q.save() + + for key, model_cls in (("finding", Finding), ("structure", Structure), ("condition", Condition), ("presentation", Presentation), ("subspecialty", Subspecialty)): + vals = payload.get(key) or [] + final_objs = [] + for v in vals: + if isinstance(v, int): + try: + final_objs.append(model_cls.objects.get(pk=v)) + except model_cls.DoesNotExist: + continue + elif isinstance(v, str): + qs = model_cls.objects.filter(name__iexact=v) + if not qs.exists(): + qs = model_cls.objects.filter(name__icontains=v) + if qs.exists(): + final_objs.extend(list(qs[:5])) + else: + if allow_create_m2m: + obj, created_flag = model_cls.objects.get_or_create(name=v) + final_objs.append(obj) + actually_created[model_cls.__name__].append(obj.pk) + if final_objs: + getattr(q, key).add(*[o.pk for o in final_objs]) + + created += 1 + except Exception as e: + errors.append({"index": idx, "errors": [str(e)]}) + + # cleanup session + try: + del request.session[session_key] + request.session.modified = True + except Exception: + pass + + return render(request, "sbas/partials/import_result.html", {"created": created, "errors": errors, "actually_created": dict(actually_created)}) \ No newline at end of file