This commit is contained in:
Ross
2022-05-21 12:21:52 +01:00
parent 160bb13b64
commit ee9b942893
14 changed files with 194 additions and 284 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ from django.utils.html import format_html
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from sortedm2m.fields import SortedManyToManyField
+1 -77
View File
@@ -221,82 +221,6 @@ def answer_question(request, pk):
# return JsonResponse(data)
def loadJsonAnswer(answer):
# Backend eid is just the object pk number
# in the frontend this is appended with the type
# e.g. anatomy/1
# TODO Update to only allow valid Candidates
eid = int(answer["eid"].split("/")[1])
cid = int(answer["cid"])
# As access is not restricted make sure the data appears valid
if (not isinstance(cid, int)) or (
not isinstance(eid, int) or (not isinstance(answer["ans"], str))
):
return False, 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 cid > max_int or eid > max_int:
return False, JsonResponse({"success": False, "error": "invalid cid"})
exam = get_object_or_404(Exam, pk=eid)
if not exam.check_cid_user(cid, answer["passcode"]):
return False, JsonResponse(
{"success": False, "error": "invalid cid / passcode"}
)
exiting_answers = CidUserAnswer.objects.filter(
question__id=answer["qid"], exam__id=eid, cid=cid
)
if not exiting_answers:
ans = CidUserAnswer(answer=answer["ans"], cid=cid)
ans.question_id = answer["qid"]
ans.exam_id = eid
ans.score = Answer.MarkOptions.UNMARKED
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.score = Answer.MarkOptions.UNMARKED
ans.full_clean()
ans.save()
return True, None
# @csrf_exempt
# def postExamAnswers(request):
# if request.is_ajax and request.method == "POST":
#
# n = 0
# # horrible but it works
# #for k in request.POST.dict():
#
# 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())
#
# return JsonResponse({"success": True, "question_count": n})
# return JsonResponse({"success": False, "error": "Invalid data"})
#
# # return render(request, "anatomy/exam.html", {"exam" : exam, "question" : question})
@login_required
@@ -1006,7 +930,7 @@ def question_save_annotation(request, pk):
GenericExamViews = ExamViews(
Exam, AnatomyQuestion, Answer, CidUserAnswer, "anatomy", "anatomy", loadJsonAnswer
Exam, AnatomyQuestion, Answer, CidUserAnswer, "anatomy", "anatomy"
)