basic json caching
This commit is contained in:
+21
-8
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user