modulise postExamAnswers

This commit is contained in:
Ross
2020-10-23 10:29:16 +01:00
parent 9d3957226d
commit 71dc80e9d2
2 changed files with 51 additions and 40 deletions
+27 -18
View File
@@ -219,41 +219,35 @@ def flag_question(request):
return JsonResponse(data) return JsonResponse(data)
@csrf_exempt def loadJsonAnswer(answer):
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 # As access is not restricted make sure the data appears valid
if (not isinstance(q["cid"], if (not isinstance(answer["cid"],
int)) or (not isinstance(q["eid"], int) or int)) or (not isinstance(answer["eid"], int) or
(not isinstance(q["ans"], str))): (not isinstance(answer["ans"], str))):
return JsonResponse({"success": False}) return JsonResponse({"success": False})
# The model should catch invalid data but this should be less intensive # The model should catch invalid data but this should be less intensive
max_int = 10000000 max_int = 10000000
if q["cid"] > max_int or q["eid"] > max_int: if answer["cid"] > max_int or answer["eid"] > max_int:
return JsonResponse({"success": False}) return JsonResponse({"success": False})
exam = get_object_or_404(Exam, pk=q["eid"]) exam = get_object_or_404(Exam, pk=answer["eid"])
if not exam.active: if not exam.active:
return JsonResponse({ return JsonResponse({
"success": "success":
False, False,
"error": "error":
"No active exam: {}".format(q["eid"]) "No active exam: {}".format(answer["eid"])
}) })
exiting_answers = CidUserAnswer.objects.filter( exiting_answers = CidUserAnswer.objects.filter(
question__id=q["qid"], exam__id=q["eid"], cid=q["cid"]) question__id=answer["qid"], exam__id=answer["eid"], cid=answer["cid"])
if not exiting_answers: if not exiting_answers:
ans = CidUserAnswer(answer=q["ans"], cid=q["cid"]) ans = CidUserAnswer(answer=answer["ans"], cid=answer["cid"])
ans.question_id = q["qid"] ans.question_id = answer["qid"]
ans.exam_id = q["eid"] ans.exam_id = answer["eid"]
ans.full_clean() ans.full_clean()
@@ -262,13 +256,25 @@ def postExamAnswers(request):
# Update an existing answer # Update an existing answer
# should never be more than one (famous last words) # should never be more than one (famous last words)
ans = exiting_answers[0] ans = exiting_answers[0]
ans.answer = q["ans"] ans.answer = answer["ans"]
ans.save() 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():
print(k)
for answer in json.loads(k):
loadJsonAnswer(answer)
# print(UserAnswer.objects.filter(exam__id=q["eid"])) # print(UserAnswer.objects.filter(exam__id=q["eid"]))
# print(request.urlencode()) # 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}) # 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) return redirect("anatomy:mark", pk=pk, sk=n + 1)
elif "previous" in request.POST: elif "previous" in request.POST:
return redirect("anatomy:mark", pk=pk, sk=n - 1) return redirect("anatomy:mark", pk=pk, sk=n - 1)
# Reset user answers (relies on the functional javascript)
user_answers = set()
else: else:
form = MarkAnatomyQuestionForm() form = MarkAnatomyQuestionForm()
return render( return render(
+2
View File
@@ -43,6 +43,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'debug_toolbar',
#"tagulous", #"tagulous",
] ]
@@ -56,6 +57,7 @@ MIDDLEWARE = [
"django.contrib.messages.middleware.MessageMiddleware", "django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware",
"reversion.middleware.RevisionMiddleware", "reversion.middleware.RevisionMiddleware",
'debug_toolbar.middleware.DebugToolbarMiddleware',
] ]
ROOT_URLCONF = 'rad.urls' ROOT_URLCONF = 'rad.urls'