From a2759908c4f34c2c990ed41bc760dbbaf5c635fb Mon Sep 17 00:00:00 2001 From: Ross Date: Thu, 4 Aug 2022 09:58:29 +0100 Subject: [PATCH] . --- anatomy/models.py | 12 ++++++-- anatomy/tests/test_exams.py | 57 +++++++++++++++++++++++++++++++++---- generic/views.py | 3 ++ rad/urls.py | 5 ++++ 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/anatomy/models.py b/anatomy/models.py index 61a9947d..ba5539ef 100644 --- a/anatomy/models.py +++ b/anatomy/models.py @@ -1,3 +1,4 @@ +from __future__ import annotations import json from django.contrib.contenttypes.fields import GenericRelation 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""" # Loop through rapidimage associations @@ -281,7 +282,8 @@ class AnatomyQuestion(models.Model): "question": str(self.question_type), } - json["answers"] = list(self.get_correct_unstripped_answers()) + if answers: + json["answers"] = list(self.get_correct_unstripped_answers()) return json @@ -420,11 +422,15 @@ class Exam(ExamBase): else: image = q.image.url + annotations = [] + if str(q.image_annotations): + annotations = [str(q.image_annotations)] + exam_questions[q.id] = { "title": q.get_title(), "question": str(q.question_type), "images": [image], - "annotations": [str(q.image_annotations)], + "annotations": annotations, "type": "anatomy", } diff --git a/anatomy/tests/test_exams.py b/anatomy/tests/test_exams.py index 5625da23..466c2378 100644 --- a/anatomy/tests/test_exams.py +++ b/anatomy/tests/test_exams.py @@ -1,7 +1,11 @@ import json +import os from re import A +from django.conf import settings import pytest +import tempfile + # from django.contrib.auth.models import User from django.urls import reverse @@ -11,7 +15,7 @@ from bs4 import BeautifulSoup 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 @@ -44,9 +48,45 @@ def create_exam(db): 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): 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")) 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_mode"] == True - #exam_json = client.get(exam_metadata["url"]) - #print(exam_json.status_code) - #print(exam_json.content) + res = client.get(exam_metadata["url"]) + assert res.status_code == 200 + 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 diff --git a/generic/views.py b/generic/views.py index 9c496de9..a8934669 100644 --- a/generic/views.py +++ b/generic/views.py @@ -1,4 +1,5 @@ from collections import defaultdict, Counter +from pathlib import Path import statistics import threading 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) 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: # exam_json_cache["cached"] = True # Make sure we pass on an argument if we are forcing a reload diff --git a/rad/urls.py b/rad/urls.py index 2a48dacd..f7ad7707 100644 --- a/rad/urls.py +++ b/rad/urls.py @@ -88,6 +88,11 @@ urlpatterns = [ views.active_exams, name="active_exams_cid", ), + path( + "exam/json/unbased//", + views.active_exams, + name="active_exams_cid_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('', include('generic.urls')),