diff --git a/anatomy/migrations/0004_auto_20201123_1619.py b/anatomy/migrations/0004_auto_20201123_1619.py
new file mode 100644
index 00000000..d7517d15
--- /dev/null
+++ b/anatomy/migrations/0004_auto_20201123_1619.py
@@ -0,0 +1,25 @@
+# Generated by Django 3.1.2 on 2020-11-23 16:19
+
+import anatomy.models
+import django.core.files.storage
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('anatomy', '0003_auto_20201119_1941'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='exam',
+ name='recreate_json',
+ field=models.BooleanField(default=False, help_text='If the json cache needs updating'),
+ ),
+ migrations.AlterField(
+ model_name='anatomyquestion',
+ name='image',
+ field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(base_url='http://161.35.163.87//media/anatomy/', location='/home/ross/sites/rad/media//anatomy/'), upload_to=anatomy.models.image_directory_path),
+ ),
+ ]
diff --git a/anatomy/models.py b/anatomy/models.py
index 5c20a2f8..851ea330 100644
--- a/anatomy/models.py
+++ b/anatomy/models.py
@@ -195,6 +195,9 @@ class Exam(models.Model):
active = models.BooleanField(help_text="If an exam should be available",
default=True)
+ recreate_json = models.BooleanField(help_text="If the json cache needs updating",
+ default=False)
+
def __str__(self):
return self.name
diff --git a/anatomy/templates/anatomy/exam_overview.html b/anatomy/templates/anatomy/exam_overview.html
index f94d8ebd..f7867481 100644
--- a/anatomy/templates/anatomy/exam_overview.html
+++ b/anatomy/templates/anatomy/exam_overview.html
@@ -12,6 +12,7 @@
{{ question }}
{% endfor %}
+JSON
{% endblock %}
\ No newline at end of file
diff --git a/anatomy/views.py b/anatomy/views.py
index d0c559ee..158dfc32 100644
--- a/anatomy/views.py
+++ b/anatomy/views.py
@@ -8,7 +8,8 @@ from django.contrib.auth.models import User
from django.db.models.functions import Lower
-from django.http import Http404, JsonResponse
+from django.core.cache import cache
+
from .forms import AnatomyAnswerForm, MarkAnatomyQuestionForm
from .models import (
@@ -453,11 +454,18 @@ def active_exams(request):
def exam_json(request, pk):
+
exam = get_object_or_404(Exam, pk=pk)
if not exam.active:
raise Http404("No available exam")
+ exam_json_cache = cache.get("exam_json_{}".format(pk))
+
+ if exam_json_cache is not None and not exam.recreate_json:
+ return JsonResponse(exam_json)
+
+
questions = exam.exam_questions.all()
exam_questions = defaultdict(dict)
@@ -470,13 +478,18 @@ def exam_json(request, pk):
"type": "anatomy",
}
- exam_json = {
- "eid": exam.id,
- "exam_type": "anatomy",
- "exam_name": exam.name,
- "exam_mode": True,
- "questions": exam_questions,
- }
+ exam_json = {
+ "eid": exam.id,
+ "exam_type": "anatomy",
+ "exam_name": exam.name,
+ "exam_mode": True,
+ "questions": exam_questions,
+ }
+
+ exam.recreate_json = False
+
+ cache.set("exam_json_{}".format(pk), exam_json, 3600)
+
return JsonResponse(exam_json)
diff --git a/rad/settings.py b/rad/settings.py
index dfd9eff3..5d37ff45 100644
--- a/rad/settings.py
+++ b/rad/settings.py
@@ -179,3 +179,10 @@ LOGGING = {
}
CORS_ORIGIN_ALLOW_ALL = True
+
+CACHES = {
+ 'default': {
+ 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
+ 'LOCATION': '/var/tmp/django_cache',
+ }
+}