numerous updates

This commit is contained in:
Ross
2022-05-20 20:26:02 +01:00
parent 0373b22e78
commit 77ccdbc92a
246 changed files with 22615 additions and 2409 deletions
+49 -18
View File
@@ -509,26 +509,48 @@ class RapidView(LoginRequiredMixin, SingleTableMixin, FilterView):
filterset_class = RapidFilter
def loadJsonAnswer(answer):
def loadJsonAnswer(answer, user=None):
eid = int(answer["eid"].split("/")[1])
cid = int(answer["cid"])
uid = False
# 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"}
)
try:
cid = int(answer["cid"])
except ValueError:
if answer["cid"].startswith("u-"):
cid = False
uid = int(answer["cid"][2:])
# 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"})
print(f"{uid=}")
print(f"{uid=}")
if not uid == user.id:
return False, JsonResponse(
{"success": False, "error": "user / uid mismatch"}
)
else:
return False, JsonResponse(
{"success": False, "error": "cid or eid or answers not defined"}
)
#uid = int(answer["uid"])
if not uid:
# 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"]):
if not exam.check_cid_user(cid, answer["passcode"], user_id=uid):
return False, JsonResponse(
{"success": False, "error": "invalid cid / passcode"}
)
@@ -538,9 +560,14 @@ def loadJsonAnswer(answer):
# {"success": False, "error": "No active exam: {}".format(eid)}
# )
exiting_answers = CidUserAnswer.objects.filter(
question__id=answer["qid"], exam__id=eid, cid=cid
)
if uid:
exiting_answers = CidUserAnswer.objects.filter(
question__id=answer["qid"], exam__id=eid, user__id=uid
)
else:
exiting_answers = CidUserAnswer.objects.filter(
question__id=answer["qid"], exam__id=eid, cid=cid
)
posted_answer = answer["ans"]
@@ -554,7 +581,11 @@ def loadJsonAnswer(answer):
normal = True
if not exiting_answers:
ans = CidUserAnswer(answer=posted_answer, normal=normal, cid=cid)
if uid:
ans = CidUserAnswer(answer=posted_answer, normal=normal, user=User.objects.get(id=uid))
else:
ans = CidUserAnswer(answer=posted_answer, normal=normal, cid=cid)
ans.question_id = answer["qid"]
ans.exam_id = eid