This commit is contained in:
Ross
2022-08-04 09:58:29 +01:00
parent be70054cf7
commit a2759908c4
4 changed files with 69 additions and 8 deletions
+8 -2
View File
@@ -1,3 +1,4 @@
from __future__ import annotations
import json import json
from django.contrib.contenttypes.fields import GenericRelation from django.contrib.contenttypes.fields import GenericRelation
from django.db import models from django.db import models
@@ -256,7 +257,7 @@ class AnatomyQuestion(models.Model):
] ]
) )
def get_question_json(self, based=True, feedback=False): def get_question_json(self, based=True, feedback=False, answers=True):
"""Returns a json representation of the question""" """Returns a json representation of the question"""
# Loop through rapidimage associations # Loop through rapidimage associations
@@ -281,6 +282,7 @@ class AnatomyQuestion(models.Model):
"question": str(self.question_type), "question": str(self.question_type),
} }
if answers:
json["answers"] = list(self.get_correct_unstripped_answers()) json["answers"] = list(self.get_correct_unstripped_answers())
return json return json
@@ -420,11 +422,15 @@ class Exam(ExamBase):
else: else:
image = q.image.url image = q.image.url
annotations = []
if str(q.image_annotations):
annotations = [str(q.image_annotations)]
exam_questions[q.id] = { exam_questions[q.id] = {
"title": q.get_title(), "title": q.get_title(),
"question": str(q.question_type), "question": str(q.question_type),
"images": [image], "images": [image],
"annotations": [str(q.image_annotations)], "annotations": annotations,
"type": "anatomy", "type": "anatomy",
} }
+52 -5
View File
@@ -1,7 +1,11 @@
import json import json
import os
from re import A from re import A
from django.conf import settings
import pytest import pytest
import tempfile
# from django.contrib.auth.models import User # from django.contrib.auth.models import User
from django.urls import reverse from django.urls import reverse
@@ -11,7 +15,7 @@ from bs4 import BeautifulSoup
from anatomy.views import GenericExamViews as AnatomyExamViews from anatomy.views import GenericExamViews as AnatomyExamViews
from anatomy.models import Exam from anatomy.models import Exam, AnatomyQuestion as Question, QuestionType, Structure
from generic.models import CidUser, CidUserGroup from generic.models import CidUser, CidUserGroup
@@ -44,9 +48,45 @@ def create_exam(db):
assert exam assert exam
return exam
def create_structures(db):
structure = Structure.objects.create(structure="test structure")
structure.save()
structure = Structure.objects.create(structure="test structure 2")
structure.save()
def create_question_types(db):
question_type = QuestionType.objects.create(text="test question type")
question_type.save()
question_type = QuestionType.objects.create(text="test question type 2")
question_type.save()
def create_question(exam):
# Just assign random stuff
image = tempfile.NamedTemporaryFile(dir=settings.MEDIA_ROOT, suffix=".jpg", delete=False)
print(image.name)
print(image.file)
print(os.listdir(settings.MEDIA_ROOT))
question: Question = Question.objects.create(question_type=QuestionType.objects.first(), structure=Structure.objects.first(), image=image.name)
question.exams.set([exam])
return question
def test_exams(db, client): def test_exams(db, client):
create_cid_user_and_groups(db) create_cid_user_and_groups(db)
create_exam(db) exam = create_exam(db)
create_structures(db)
create_question_types(db)
question1 = create_question(exam)
create_question(exam)
create_question(exam)
active_exams = client.get(reverse(f"{APP_NAME}:active_exams")) active_exams = client.get(reverse(f"{APP_NAME}:active_exams"))
assert active_exams.status_code == 200 assert active_exams.status_code == 200
@@ -81,8 +121,15 @@ def test_exams(db, client):
assert exam_metadata["exam_active"] == True assert exam_metadata["exam_active"] == True
assert exam_metadata["exam_mode"] == True assert exam_metadata["exam_mode"] == True
#exam_json = client.get(exam_metadata["url"]) res = client.get(exam_metadata["url"])
#print(exam_json.status_code) assert res.status_code == 200
#print(exam_json.content) exam_json = json.loads(res.content)
print("---", exam_json["questions"].values())
assert question1.get_question_json(answers=False) in exam_json["questions"].values()
assert len(exam_json["questions"]) == 3
+3
View File
@@ -1,4 +1,5 @@
from collections import defaultdict, Counter from collections import defaultdict, Counter
from pathlib import Path
import statistics import statistics
import threading import threading
from dal import autocomplete from dal import autocomplete
@@ -1479,6 +1480,8 @@ class ExamViews(View, LoginRequiredMixin):
path = "{0}{1}/exam/{2}.json".format(settings.MEDIA_ROOT, self.app_name, pk) path = "{0}{1}/exam/{2}.json".format(settings.MEDIA_ROOT, self.app_name, pk)
url = "{0}{1}/exam/{2}.json".format(settings.MEDIA_URL, self.app_name, pk) url = "{0}{1}/exam/{2}.json".format(settings.MEDIA_URL, self.app_name, pk)
Path(path).parent.mkdir(parents=True, exist_ok=True)
if os.path.isfile(path) and not exam.recreate_json: if os.path.isfile(path) and not exam.recreate_json:
# exam_json_cache["cached"] = True # exam_json_cache["cached"] = True
# Make sure we pass on an argument if we are forcing a reload # Make sure we pass on an argument if we are forcing a reload
+5
View File
@@ -88,6 +88,11 @@ urlpatterns = [
views.active_exams, views.active_exams,
name="active_exams_cid", name="active_exams_cid",
), ),
path(
"exam/json/unbased/<int:cid>/<str:passcode>",
views.active_exams,
name="active_exams_cid_unbased",
),
path("exam/json/unbased", views.active_exams_unbased, name="active_exams_unbased"), path("exam/json/unbased", views.active_exams_unbased, name="active_exams_unbased"),
path("exam/submit", views.exam_submit, name="global_exam_answers_submit"), path("exam/submit", views.exam_submit, name="global_exam_answers_submit"),
# path('', include('generic.urls')), # path('', include('generic.urls')),