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 help_text="If the json cache needs updating", default=False
) )
json_creation_time = models.DateTimeField(blank=True)
#time_limit = models.IntegerField( #time_limit = models.IntegerField(
# help_text="Exam time limit (in seconds). Default is 2100 secondse (35 minutes)", # help_text="Exam time limit (in seconds). Default is 2100 secondse (35 minutes)",
# default=2100, # 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 anatomy.models import Exam as AnatomyExam
from django.db.models import Case, When from django.db.models import Case, When
from django.conf import settings
from datetime import datetime
import os
# Create your views here. # Create your views here.
@@ -320,6 +324,8 @@ class ExamViews(View, LoginRequiredMixin):
"name": exam.get_exam_name(), "name": exam.get_exam_name(),
"url": request.build_absolute_uri(exam.get_json_url()), "url": request.build_absolute_uri(exam.get_json_url()),
"type": self.question_type, "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: if not exam.active:
raise Http404("No available exam") raise Http404("No available exam")
exam_json_cache = cache.get("{}_exam_json_{}".format(self.app_name, pk)) exam_json_cache = cache.get("{}_exam_json_{}".format(self.app_name, pk))
if exam_json_cache is not None and not exam.recreate_json: path= "{0}{1}/exam/{2}.json".format(settings.MEDIA_ROOT, self.app_name, pk)
exam_json_cache["cached"] = True 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) 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.recreate_json = False
exam.json_creation_time = datetime.now()
exam.save() exam.save()
# We also try to clear the cache of any associated questions (longs) # We also try to clear the cache of any associated questions (longs)
# see exam_question_json # see exam_question_json
n = exam.exam_questions.count() #n = exam.exam_questions.count()
question_keys = ["{}_exam_json_{}_{}".format(self.app_name, pk, i) for i in range(1, n)] #question_keys = ["{}_exam_json_{}_{}".format(self.app_name, pk, i) for i in range(1, n)]
cache.delete_many(question_keys) #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) return JsonResponse(exam_json)
+6 -2
View File
@@ -120,6 +120,8 @@ class Long(models.Model):
help_text="If the json cache needs updating", default=False help_text="If the json cache needs updating", default=False
) )
json_creation_time = models.DateTimeField(blank=True)
#question_file = models.FileField(upload_to=question_file_directory_path, blank=True, null=True) #question_file = models.FileField(upload_to=question_file_directory_path, blank=True, null=True)
def get_absolute_url(self): def get_absolute_url(self):
@@ -175,11 +177,11 @@ class Long(models.Model):
url= "{0}longs/questions/{1}.json".format(settings.MEDIA_URL, question_id) url= "{0}longs/questions/{1}.json".format(settings.MEDIA_URL, question_id)
with open(path, "w+") as f: with open(path, "w+") as f:
# We manually create the json for long questions to reducem memroy usade
f.write("""{{ f.write("""{{
"title": "{}", "title": "{}",
"image_titles": "{}",
"type": "long", "type": "long",
"generated": "long", "generated": "{}",
""".format(q.history, datetime.datetime.now().isoformat()) """.format(q.history, datetime.datetime.now().isoformat())
) )
@@ -209,6 +211,8 @@ class Long(models.Model):
f.write('"image_titles" : {} }}'.format(json.dumps(image_titles))) f.write('"image_titles" : {} }}'.format(json.dumps(image_titles)))
self.json_creation_time = datetime.datetime.now()
self.save()
#exam_question = { #exam_question = {
# "title": q.history, # "title": q.history,
+1 -1
View File
@@ -85,7 +85,7 @@ class RapidForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if kwargs.get("instance"): if kwargs.get("instance"):
# We get the 'initial' keyword argument or initialize it # We gexam_json_cache is not Noneet the 'initial' keyword argument or initialize it
# as a dict if it didn't exist. # as a dict if it didn't exist.
initial = kwargs.setdefault("initial", {}) initial = kwargs.setdefault("initial", {})
# The widget for a ModelMultipleChoiceField expects # The widget for a ModelMultipleChoiceField expects