This commit is contained in:
Ross
2021-03-09 15:18:19 +00:00
parent 6b2c7a1a80
commit f0efcd0c32
4 changed files with 34 additions and 10 deletions
+2
View File
@@ -64,6 +64,8 @@ class ExamBase(models.Model):
help_text="If the json cache needs updating", default=False
)
json_creation_time = models.DateTimeField(blank=True)
#time_limit = models.IntegerField(
# help_text="Exam time limit (in seconds). Default is 2100 secondse (35 minutes)",
# default=2100,
+25 -7
View File
@@ -32,6 +32,10 @@ from anatomy.models import AnatomyQuestion as AnatomyQuestion
from anatomy.models import Exam as AnatomyExam
from django.db.models import Case, When
from django.conf import settings
from datetime import datetime
import os
# Create your views here.
@@ -320,6 +324,8 @@ class ExamViews(View, LoginRequiredMixin):
"name": exam.get_exam_name(),
"url": request.build_absolute_uri(exam.get_json_url()),
"type": self.question_type,
"eid": "{}/{}".format(self.question_type, exam.pk),
"json_creation_time": exam.json_creation_time.isoformat()
}
)
@@ -356,24 +362,36 @@ class ExamViews(View, LoginRequiredMixin):
if not exam.active:
raise Http404("No available exam")
exam_json_cache = cache.get("{}_exam_json_{}".format(self.app_name, pk))
if exam_json_cache is not None and not exam.recreate_json:
exam_json_cache["cached"] = True
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)
if os.path.isfile(path) and not exam.recreate_json:
#exam_json_cache["cached"] = True
return redirect(url)
return JsonResponse(exam_json_cache)
exam_json = exam.get_exam_json()
with open(path, "w+") as f:
exam_json = exam.get_exam_json()
exam_json["generated"] = datetime.now().isoformat()
f.write(json.dumps(exam_json))
exam.recreate_json = False
exam.json_creation_time = datetime.now()
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)
#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)
#cache.set("{}_exam_json_{}".format(self.app_name, pk), exam_json, 3600)
return JsonResponse(exam_json)