This commit is contained in:
Ross
2021-01-30 15:16:18 +00:00
parent 37a2f2d88e
commit d4fa92b220
4 changed files with 75 additions and 8 deletions
+20 -8
View File
@@ -502,9 +502,11 @@ class RapidView(SingleTableMixin, FilterView):
def loadJsonAnswer(answer):
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"}
@@ -512,24 +514,33 @@ def loadJsonAnswer(answer):
# 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"]
)
posted_answer = answer["ans"]
# Normal answers are just posted with the answer "Normal"
normal = False
if posted_answer == "Normal":
normal = True
posted_answer = ""
if not exiting_answers:
ans = CidUserAnswer(answer=answer["ans"], cid=answer["cid"])
ans = CidUserAnswer(answer=posted_answer, normal=normal, cid=answer["cid"])
ans.question_id = answer["qid"]
ans.exam_id = answer["eid"]
ans.exam_id = eid
ans.full_clean()
@@ -538,7 +549,8 @@ def loadJsonAnswer(answer):
# Update an existing answer
# should never be more than one (famous last words)
ans = exiting_answers[0]
ans.answer = answer["ans"]
ans.normal = normal
ans.answer = posted_answer
ans.full_clean()
ans.save()