feat: add new endpoints for fetching active exams by type and cid; enhance exam handling in views

This commit is contained in:
Ross
2026-07-04 22:40:48 +01:00
parent 856fb0bf08
commit d8bacfefa9
7 changed files with 73 additions and 20 deletions
+12
View File
@@ -137,6 +137,18 @@ urlpatterns = [
name="active_exams_cid_unbased",
),
path("exam/json/unbased", views.active_exams_unbased, name="active_exams_unbased"),
path("exam/json/<str:exam_type>/", views.active_exams_by_type, name="active_exams_by_type"),
path(
"exam/json/<str:exam_type>/<int:cid>/<str:passcode>",
views.active_exams_by_type,
name="active_exams_by_type_cid",
),
path(
"exam/json/<str:exam_type>/unbased/<int:cid>/<str:passcode>",
views.active_exams_by_type_unbased,
name="active_exams_by_type_cid_unbased",
),
path("exam/json/<str:exam_type>/unbased", views.active_exams_by_type_unbased, name="active_exams_by_type_unbased"),
path("exam/submit", views.exam_submit, name="global_exam_answers_submit"),
# path('', include('generic.urls')),
path(
+44
View File
@@ -879,7 +879,9 @@ def cid_user_overview(request, cid, passcode):
UserAnswer, Exam = EXAM_ANSWER_MAP[exam_type]
exam_answers = UserAnswer.objects.filter(cid=cid)
exam_ids = exam_answers.values_list("exam").distinct()
logger.debug(f"DEBUG cid_user_overview: {exam_type=}, {cid=}, answers_count={exam_answers.count()}, exam_ids={list(exam_ids)}")
exams_to_add = Exam.objects.filter(id__in=exam_ids, **kwargs)
logger.debug(f"DEBUG cid_user_overview: {exams_to_add=}")
if exams_to_add:
exams.append((exam_type, exams_to_add))
@@ -947,6 +949,48 @@ def active_exams_unbased(request, cid=None, passcode=None):
return active_exams(request, cid, passcode, based=False)
def active_exams_by_type(request, exam_type, cid=None, passcode=None, based=True):
active_exams_data = {}
if cid is not None:
active_exams_data["cid"] = cid
cid_user = CidUser.objects.filter(cid=cid).first()
if not cid_user or cid_user.passcode != passcode:
return JsonResponse({"status": "invalid"}, status=401)
exams = []
if exam_type == "anatomy":
exams = AnatomyExamViews.active_exams(
request, False, cid=cid, passcode=passcode
)
elif exam_type == "rapid":
exams = RapidsExamsViews.active_exams(
request, False, cid=cid, passcode=passcode
)
elif exam_type == "short":
exams = ShortsExamsViews.active_exams(
request, False, cid=cid, passcode=passcode
)
elif exam_type == "long":
exams = LongsExamsViews.active_exams(
request, False, cid=cid, passcode=passcode
)
active_exams_data["exams"] = exams
if request.user and request.user.is_authenticated:
active_exams_data["user"] = request.user.username
active_exams_data["user_id"] = request.user.pk
else:
active_exams_data["user"] = False
return JsonResponse(active_exams_data)
def active_exams_by_type_unbased(request, exam_type, cid=None, passcode=None):
return active_exams_by_type(request, exam_type, cid, passcode, based=False)
@csrf_exempt
def exam_submit(request):
if request.method == "POST":