From 28ff5dc674658c19e4ae29577065aed6f37e5202 Mon Sep 17 00:00:00 2001 From: Ross Date: Sat, 27 Feb 2021 09:17:15 +0000 Subject: [PATCH] . --- generic/views.py | 24 ++++++++++++++++++++++++ longs/models.py | 49 ++++++++++++++++++++++++++++++++++++++++-------- longs/urls.py | 1 + 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/generic/views.py b/generic/views.py index 9f156fe2..42019ba2 100644 --- a/generic/views.py +++ b/generic/views.py @@ -358,6 +358,30 @@ class ExamViews(View, LoginRequiredMixin): exam.recreate_json = False exam.save() + # We also try to clear the cache of any associated questions (longs) + # see exam_question_json + n = exam.exam_questions.count() + question_keys = ["{}_exam_json_{}_{}".format(self.app_name, pk, i) for i in range(1, n)] + cache.delete_many(question_keys) + cache.set("{}_exam_json_{}".format(self.app_name, pk), exam_json, 3600) + return JsonResponse(exam_json) + + def exam_question_json(self, request, pk, sk): + exam = get_object_or_404(self.Exam, pk=pk) + + if not exam.active: + raise Http404("No available exam") + + exam_json_cache = cache.get("{}_exam_json_{}_{}".format(self.app_name, pk, sk)) + + if exam_json_cache is not None and not exam.recreate_json: + exam_json_cache["cached"] = True + return JsonResponse(exam_json_cache) + + exam_json = exam.get_exam_question_json() + + cache.set("{}_exam_json_{}_{}".format(self.app_name, pk, sk), exam_json, 3600) + return JsonResponse(exam_json) \ No newline at end of file diff --git a/longs/models.py b/longs/models.py index ecb52d37..5bbbb91c 100644 --- a/longs/models.py +++ b/longs/models.py @@ -417,6 +417,36 @@ class Exam(ExamBase): default=4500, ) + def get_exam_question_json(self, n): + q = self.exam_questions.all()[n] + + #exam_order.append(q.id) + + # Loop through longimage associations + images = [] + image_titles = [] + for series in q.series.all(): + image_array = [] + for i in series.images.all(): + image_array.append(image_as_base64(i.image)) + #image_array.append(i.image.url) + images.append(image_array) + image_titles.append(series.get_examination()) + + + + exam_question = { + "title": q.history, + "images": images, + "image_titles": image_titles, + #"feedback_image": [], + #"annotations": [str(q.image_annotations)], + "type": "long", + } + + return exam_question + + def get_exam_json(self): questions = self.exam_questions.all() @@ -424,6 +454,7 @@ class Exam(ExamBase): exam_order = [] + n = 1 for q in questions: exam_order.append(q.id) @@ -439,15 +470,16 @@ class Exam(ExamBase): image_titles.append(series.get_examination()) + exam_questions[q.id] = n - exam_questions[q.id] = { - "title": q.history, - "images": images, - "image_titles": image_titles, - #"feedback_image": [], - #"annotations": [str(q.image_annotations)], - "type": "long", - } + #exam_questions[q.id] = { + # "title": q.history, + # "images": images, + # "image_titles": image_titles, + # #"feedback_image": [], + # #"annotations": [str(q.image_annotations)], + # "type": "long", + #} #if feedback_images: # exam_questions[q.id]["feedback_image"] = feedback_images @@ -461,6 +493,7 @@ class Exam(ExamBase): "exam_mode": True, "exam_order": exam_order, "questions": exam_questions, + "question_requests": True, } diff --git a/longs/urls.py b/longs/urls.py index 0487e8d8..dcf2b6e4 100755 --- a/longs/urls.py +++ b/longs/urls.py @@ -54,6 +54,7 @@ urlpatterns = [ path("exam/", views.LongExamViews.exam_list, name="exam_list"), path("exam/json/", views.LongExamViews.active_exams, name="active_exams"), path("exam/json/", views.LongExamViews.exam_json, name="exam_json"), + path("exam/json///recreate", views.LongExamViews.exam_json_recreate,