From 71dc80e9d2cee044076a52162352961243f51932 Mon Sep 17 00:00:00 2001 From: Ross Date: Fri, 23 Oct 2020 10:29:16 +0100 Subject: [PATCH] modulise postExamAnswers --- anatomy/views.py | 89 ++++++++++++++++++++++++++---------------------- rad/settings.py | 2 ++ 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/anatomy/views.py b/anatomy/views.py index 070d9755..3e5bb1a6 100644 --- a/anatomy/views.py +++ b/anatomy/views.py @@ -219,56 +219,62 @@ def flag_question(request): return JsonResponse(data) +def loadJsonAnswer(answer): + # As access is not restricted make sure the data appears valid + if (not isinstance(answer["cid"], + int)) or (not isinstance(answer["eid"], int) or + (not isinstance(answer["ans"], str))): + return JsonResponse({"success": False}) + + # The model should catch invalid data but this should be less intensive + max_int = 10000000 + if answer["cid"] > max_int or answer["eid"] > max_int: + return JsonResponse({"success": False}) + + exam = get_object_or_404(Exam, pk=answer["eid"]) + + if not exam.active: + return JsonResponse({ + "success": + False, + "error": + "No active exam: {}".format(answer["eid"]) + }) + + exiting_answers = CidUserAnswer.objects.filter( + question__id=answer["qid"], exam__id=answer["eid"], cid=answer["cid"]) + + if not exiting_answers: + ans = CidUserAnswer(answer=answer["ans"], cid=answer["cid"]) + ans.question_id = answer["qid"] + ans.exam_id = answer["eid"] + + ans.full_clean() + + ans.save() + else: + # Update an existing answer + # should never be more than one (famous last words) + ans = exiting_answers[0] + ans.answer = answer["ans"] + ans.save() + + @csrf_exempt def postExamAnswers(request): if request.is_ajax and request.method == "POST": # horrible but it works for k in request.POST.dict(): - for q in json.loads(k): - # As access is not restricted make sure the data appears valid - if (not isinstance(q["cid"], - int)) or (not isinstance(q["eid"], int) or - (not isinstance(q["ans"], str))): - return JsonResponse({"success": False}) - - # The model should catch invalid data but this should be less intensive - max_int = 10000000 - if q["cid"] > max_int or q["eid"] > max_int: - return JsonResponse({"success": False}) - - exam = get_object_or_404(Exam, pk=q["eid"]) - - if not exam.active: - return JsonResponse({ - "success": - False, - "error": - "No active exam: {}".format(q["eid"]) - }) - - exiting_answers = CidUserAnswer.objects.filter( - question__id=q["qid"], exam__id=q["eid"], cid=q["cid"]) - - if not exiting_answers: - ans = CidUserAnswer(answer=q["ans"], cid=q["cid"]) - ans.question_id = q["qid"] - ans.exam_id = q["eid"] - - ans.full_clean() - - ans.save() - else: - # Update an existing answer - # should never be more than one (famous last words) - ans = exiting_answers[0] - ans.answer = q["ans"] - ans.save() + print(k) + for answer in json.loads(k): + loadJsonAnswer(answer) # print(UserAnswer.objects.filter(exam__id=q["eid"])) # print(request.urlencode()) - return JsonResponse({"success": True}) + return JsonResponse({"success": True}) + return JsonResponse({"success": False, "error": "Invalid data"}) # return render(request, "anatomy/exam.html", {"exam" : exam, "question" : question}) @@ -366,6 +372,9 @@ def mark(request, pk, sk): return redirect("anatomy:mark", pk=pk, sk=n + 1) elif "previous" in request.POST: return redirect("anatomy:mark", pk=pk, sk=n - 1) + + # Reset user answers (relies on the functional javascript) + user_answers = set() else: form = MarkAnatomyQuestionForm() return render( diff --git a/rad/settings.py b/rad/settings.py index 6d09483d..dfd9eff3 100644 --- a/rad/settings.py +++ b/rad/settings.py @@ -43,6 +43,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'debug_toolbar', #"tagulous", ] @@ -56,6 +57,7 @@ MIDDLEWARE = [ "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "reversion.middleware.RevisionMiddleware", + 'debug_toolbar.middleware.DebugToolbarMiddleware', ] ROOT_URLCONF = 'rad.urls'