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)
+6 -2
View File
@@ -120,6 +120,8 @@ class Long(models.Model):
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)
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)
with open(path, "w+") as f:
# We manually create the json for long questions to reducem memroy usade
f.write("""{{
"title": "{}",
"image_titles": "{}",
"type": "long",
"generated": "long",
"generated": "{}",
""".format(q.history, datetime.datetime.now().isoformat())
)
@@ -209,6 +211,8 @@ class Long(models.Model):
f.write('"image_titles" : {} }}'.format(json.dumps(image_titles)))
self.json_creation_time = datetime.datetime.now()
self.save()
#exam_question = {
# "title": q.history,
+1 -1
View File
@@ -85,7 +85,7 @@ class RapidForm(ModelForm):
def __init__(self, *args, **kwargs):
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.
initial = kwargs.setdefault("initial", {})
# The widget for a ModelMultipleChoiceField expects