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
@@ -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),
),
]
@@ -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),
),
]
+10
View File
@@ -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(
+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()