Refactor exam take functionality to redirect to full overview page instead of returning HTMX fragment
This commit is contained in:
+4
-36
@@ -282,42 +282,10 @@ def exam_take(request, pk: int, sk: int, cid: str | None = None, passcode: str |
|
|||||||
return exam_take_fragment(request, pk=pk, sk=pos - 1, cid=cid, passcode=passcode)
|
return exam_take_fragment(request, pk=pk, sk=pos - 1, cid=cid, passcode=passcode)
|
||||||
return redirect(take_url, sk=pos - 1, **kwargs)
|
return redirect(take_url, sk=pos - 1, **kwargs)
|
||||||
elif "finish" in request.POST:
|
elif "finish" in request.POST:
|
||||||
# For HTMX clients, return an overview fragment so the page can
|
# The overview is a full page; always navigate to the overview
|
||||||
# update in-place instead of performing a full-page redirect.
|
# URL rather than returning an HTMX fragment. This ensures a
|
||||||
if is_htmx:
|
# consistent full-page overview experience (and prevents the
|
||||||
# Build the same context used by exam_take_overview
|
# overview HTML from being swapped into the question container).
|
||||||
questions = exam.get_questions()
|
|
||||||
if cid is not None:
|
|
||||||
answers = UserAnswer.objects.filter(cid=cid, exam=exam)
|
|
||||||
else:
|
|
||||||
answers = UserAnswer.objects.filter(user=request.user, exam=exam)
|
|
||||||
|
|
||||||
answer_question_map = {ans.question: ans for ans in answers}
|
|
||||||
|
|
||||||
question_answer_tuples = []
|
|
||||||
answer_count = 0
|
|
||||||
for q in questions:
|
|
||||||
if q in answer_question_map:
|
|
||||||
question_answer_tuples.append((q, answer_question_map[q]))
|
|
||||||
answer_count += 1
|
|
||||||
else:
|
|
||||||
question_answer_tuples.append((q, None))
|
|
||||||
|
|
||||||
cid_user_exam = exam.get_or_create_cid_user_exam(cid=cid, user_user=request.user)
|
|
||||||
|
|
||||||
return render(
|
|
||||||
request,
|
|
||||||
"physics/partials/exam_take_overview_fragment.html",
|
|
||||||
{
|
|
||||||
"exam": exam,
|
|
||||||
"cid": cid,
|
|
||||||
"question_answer_tuples": question_answer_tuples,
|
|
||||||
"answer_count": answer_count,
|
|
||||||
"exam_length": len(questions),
|
|
||||||
"passcode": passcode,
|
|
||||||
"cid_user_exam": cid_user_exam,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return redirect(finish_url, **kwargs)
|
return redirect(finish_url, **kwargs)
|
||||||
elif "save" in request.POST:
|
elif "save" in request.POST:
|
||||||
# Save action should not trigger a full-page load for HTMX clients.
|
# Save action should not trigger a full-page load for HTMX clients.
|
||||||
|
|||||||
Reference in New Issue
Block a user