From a2864d07551d134043cd99331c3badac6b27ff77 Mon Sep 17 00:00:00 2001 From: Ross Date: Thu, 28 Jan 2021 10:58:10 +0000 Subject: [PATCH] . --- anatomy/views.py | 30 +++++++++++++++++------------- rad/views.py | 15 ++++++++++++--- rapids/views.py | 15 +++++++-------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/anatomy/views.py b/anatomy/views.py index 3e10147a..42205f19 100644 --- a/anatomy/views.py +++ b/anatomy/views.py @@ -261,32 +261,36 @@ def flag_question(request): def loadJsonAnswer(answer): + # Backend eid is just the object pk number + # in the frontend this is appended with the type + # e.g. anatomy/1 + eid = int(answer["eid"].split("/")[1]) # 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)) + not isinstance(eid, int) or (not isinstance(answer["ans"], str)) ): return JsonResponse({"success": False, "error": "cid or eid or answers not defined"}) # The model should catch invalid data but this should be less intensive max_int = 999999999999999999999 - if answer["cid"] > max_int or answer["eid"] > max_int: + if answer["cid"] > max_int or eid > max_int: return JsonResponse({"success": False, "error": "invalid cid"}) - exam = get_object_or_404(Exam, pk=answer["eid"]) + exam = get_object_or_404(Exam, pk=eid) if not exam.active: return JsonResponse( - {"success": False, "error": "No active exam: {}".format(answer["eid"])} + {"success": False, "error": "No active exam: {}".format(eid)} ) exiting_answers = CidUserAnswer.objects.filter( - question__id=answer["qid"], exam__id=answer["eid"], cid=answer["cid"] + question__id=answer["qid"], exam__id=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.exam_id = eid ans.full_clean() @@ -331,14 +335,14 @@ def postExamAnswers(request): n = 0 # horrible but it works - for k in request.POST.dict(): - print(k) - for answer in json.loads(k): - ret = loadJsonAnswer(answer) + #for k in request.POST.dict(): - if ret is not True: - return ret - n = n + 1 + for answer in json.loads(request.POST.get("answers")): + ret = loadJsonAnswer(answer) + + if ret is not True: + return ret + n = n + 1 # print(UserAnswer.objects.filter(exam__id=q["eid"])) # print(request.urlencode()) diff --git a/rad/views.py b/rad/views.py index 51110367..8a817f90 100644 --- a/rad/views.py +++ b/rad/views.py @@ -27,8 +27,8 @@ from physics.models import Exam as PhysicsExam from anatomy.models import CidUserAnswer as AnatomyCidUserAnswer from anatomy.models import Exam as AnatomyExam -from anatomy.views import active_exams as active_anatomy_exams -from rapids.views import active_exams as active_rapid_exams +from anatomy.views import active_exams as active_anatomy_exams, postExamAnswers as postAnatomyExamAnswers +from rapids.views import active_exams as active_rapid_exams, postExamAnswers as postRapidExamAnswers @@ -94,5 +94,14 @@ def active_exams(request): @csrf_exempt def exam_submit(request): + if request.is_ajax and request.method == "POST": + exam_type, exam_id = request.POST.get("eid").split("/") + + if exam_type == "anatomy": + return postAnatomyExamAnswers(request) + elif exam_type == "rapid": + return postRapidExamAnswers(request) + return JsonResponse({"success": False, "error": "Invalid exam type"}) + # postExamAnswers - pass \ No newline at end of file + return JsonResponse({"success": False, "error": "Invalid data"}) \ No newline at end of file diff --git a/rapids/views.py b/rapids/views.py index 3a7f7e99..d9fb01a2 100755 --- a/rapids/views.py +++ b/rapids/views.py @@ -1,3 +1,4 @@ +import json from django.shortcuts import render, get_object_or_404, redirect from django import forms @@ -572,15 +573,13 @@ def postExamAnswers(request): if request.is_ajax and request.method == "POST": n = 0 - # horrible but it works - for k in request.POST.dict(): - print(k) - for answer in json.loads(k): - ret = loadJsonAnswer(answer) + + for answer in json.loads(request.POST.get("answers")): + ret = loadJsonAnswer(answer) - if ret is not True: - return ret - n = n + 1 + if ret is not True: + return ret + n = n + 1 # print(UserAnswer.objects.filter(exam__id=q["eid"])) # print(request.urlencode())