basic json caching

This commit is contained in:
Ross
2020-11-23 16:21:56 +00:00
parent f345ca4447
commit 0ff1c39ffd
5 changed files with 57 additions and 8 deletions
+21 -8
View File
@@ -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)