.
This commit is contained in:
+17
-13
@@ -261,32 +261,36 @@ def flag_question(request):
|
|||||||
|
|
||||||
|
|
||||||
def loadJsonAnswer(answer):
|
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
|
# As access is not restricted make sure the data appears valid
|
||||||
if (not isinstance(answer["cid"], int)) or (
|
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"})
|
return JsonResponse({"success": False, "error": "cid or eid or answers not defined"})
|
||||||
|
|
||||||
# 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 = 999999999999999999999
|
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"})
|
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:
|
if not exam.active:
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{"success": False, "error": "No active exam: {}".format(answer["eid"])}
|
{"success": False, "error": "No active exam: {}".format(eid)}
|
||||||
)
|
)
|
||||||
|
|
||||||
exiting_answers = CidUserAnswer.objects.filter(
|
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:
|
if not exiting_answers:
|
||||||
ans = CidUserAnswer(answer=answer["ans"], cid=answer["cid"])
|
ans = CidUserAnswer(answer=answer["ans"], cid=answer["cid"])
|
||||||
ans.question_id = answer["qid"]
|
ans.question_id = answer["qid"]
|
||||||
ans.exam_id = answer["eid"]
|
ans.exam_id = eid
|
||||||
|
|
||||||
ans.full_clean()
|
ans.full_clean()
|
||||||
|
|
||||||
@@ -331,14 +335,14 @@ def postExamAnswers(request):
|
|||||||
|
|
||||||
n = 0
|
n = 0
|
||||||
# horrible but it works
|
# horrible but it works
|
||||||
for k in request.POST.dict():
|
#for k in request.POST.dict():
|
||||||
print(k)
|
|
||||||
for answer in json.loads(k):
|
|
||||||
ret = loadJsonAnswer(answer)
|
|
||||||
|
|
||||||
if ret is not True:
|
for answer in json.loads(request.POST.get("answers")):
|
||||||
return ret
|
ret = loadJsonAnswer(answer)
|
||||||
n = n + 1
|
|
||||||
|
if ret is not True:
|
||||||
|
return ret
|
||||||
|
n = n + 1
|
||||||
|
|
||||||
# print(UserAnswer.objects.filter(exam__id=q["eid"]))
|
# print(UserAnswer.objects.filter(exam__id=q["eid"]))
|
||||||
# print(request.urlencode())
|
# print(request.urlencode())
|
||||||
|
|||||||
+12
-3
@@ -27,8 +27,8 @@ from physics.models import Exam as PhysicsExam
|
|||||||
from anatomy.models import CidUserAnswer as AnatomyCidUserAnswer
|
from anatomy.models import CidUserAnswer as AnatomyCidUserAnswer
|
||||||
from anatomy.models import Exam as AnatomyExam
|
from anatomy.models import Exam as AnatomyExam
|
||||||
|
|
||||||
from anatomy.views import active_exams as active_anatomy_exams
|
from anatomy.views import active_exams as active_anatomy_exams, postExamAnswers as postAnatomyExamAnswers
|
||||||
from rapids.views import active_exams as active_rapid_exams
|
from rapids.views import active_exams as active_rapid_exams, postExamAnswers as postRapidExamAnswers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -94,5 +94,14 @@ def active_exams(request):
|
|||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def exam_submit(request):
|
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
|
# postExamAnswers
|
||||||
pass
|
return JsonResponse({"success": False, "error": "Invalid data"})
|
||||||
+7
-8
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
from django.shortcuts import render, get_object_or_404, redirect
|
from django.shortcuts import render, get_object_or_404, redirect
|
||||||
from django import forms
|
from django import forms
|
||||||
|
|
||||||
@@ -572,15 +573,13 @@ def postExamAnswers(request):
|
|||||||
if request.is_ajax and request.method == "POST":
|
if request.is_ajax and request.method == "POST":
|
||||||
|
|
||||||
n = 0
|
n = 0
|
||||||
# horrible but it works
|
|
||||||
for k in request.POST.dict():
|
|
||||||
print(k)
|
|
||||||
for answer in json.loads(k):
|
|
||||||
ret = loadJsonAnswer(answer)
|
|
||||||
|
|
||||||
if ret is not True:
|
for answer in json.loads(request.POST.get("answers")):
|
||||||
return ret
|
ret = loadJsonAnswer(answer)
|
||||||
n = n + 1
|
|
||||||
|
if ret is not True:
|
||||||
|
return ret
|
||||||
|
n = n + 1
|
||||||
|
|
||||||
# print(UserAnswer.objects.filter(exam__id=q["eid"]))
|
# print(UserAnswer.objects.filter(exam__id=q["eid"]))
|
||||||
# print(request.urlencode())
|
# print(request.urlencode())
|
||||||
|
|||||||
Reference in New Issue
Block a user