From d4fa92b220c008799f853df2529aa1804bc2ea8e Mon Sep 17 00:00:00 2001 From: Ross Date: Sat, 30 Jan 2021 15:16:18 +0000 Subject: [PATCH] . --- anatomy/migrations/0024_auto_20210130_1515.py | 20 +++++++++++++ rapids/migrations/0008_auto_20210130_1515.py | 25 +++++++++++++++++ rapids/models.py | 10 +++++++ rapids/views.py | 28 +++++++++++++------ 4 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 anatomy/migrations/0024_auto_20210130_1515.py create mode 100644 rapids/migrations/0008_auto_20210130_1515.py diff --git a/anatomy/migrations/0024_auto_20210130_1515.py b/anatomy/migrations/0024_auto_20210130_1515.py new file mode 100644 index 00000000..ae48739d --- /dev/null +++ b/anatomy/migrations/0024_auto_20210130_1515.py @@ -0,0 +1,20 @@ +# Generated by Django 3.1.3 on 2021-01-30 15:15 + +import anatomy.models +import django.core.files.storage +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('anatomy', '0023_auto_20210125_1534'), + ] + + operations = [ + migrations.AlterField( + model_name='anatomyquestion', + name='image', + field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(base_url='/media/anatomy/', location='/home/ross/scripts/sites/rad/media//anatomy/'), upload_to=anatomy.models.image_directory_path), + ), + ] diff --git a/rapids/migrations/0008_auto_20210130_1515.py b/rapids/migrations/0008_auto_20210130_1515.py new file mode 100644 index 00000000..46779ebd --- /dev/null +++ b/rapids/migrations/0008_auto_20210130_1515.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.3 on 2021-01-30 15:15 + +import django.core.files.storage +from django.db import migrations, models +import rapids.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('rapids', '0007_auto_20210125_1534'), + ] + + operations = [ + migrations.AddField( + model_name='ciduseranswer', + name='normal', + field=models.BooleanField(default=False), + ), + migrations.AlterField( + model_name='rapidimage', + name='image', + field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(base_url='/media/rapids/', location='/home/ross/scripts/sites/rad/media//rapids/'), upload_to=rapids.models.image_directory_path), + ), + ] diff --git a/rapids/models.py b/rapids/models.py index ac434ab0..505e6f4e 100644 --- a/rapids/models.py +++ b/rapids/models.py @@ -386,6 +386,9 @@ class CidUserAnswer(models.Model): question = models.ForeignKey( Rapid, related_name="cid_user_answers", on_delete=models.CASCADE ) + + # For rapids the answer can be normal in which case the below field is true + normal = models.BooleanField(default=False) answer = models.TextField(max_length=500, blank=True) answer_compare = models.TextField(max_length=500, blank=True) @@ -430,6 +433,13 @@ class CidUserAnswer(models.Model): def get_answer_score(self): q = self.question + + # First step we check that the normal/abnormal states match + if q.normal != self.normal: + # If they don't match the answer is wrong (score is 0) + return 0 + + # Then compare answer strings (as per anatomy questions) ans = self.answer_compare if ( q.answers.filter( diff --git a/rapids/views.py b/rapids/views.py index d9fb01a2..6c3f62a8 100755 --- a/rapids/views.py +++ b/rapids/views.py @@ -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()