diff --git a/generic/models.py b/generic/models.py index b812654c..732c6352 100644 --- a/generic/models.py +++ b/generic/models.py @@ -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, diff --git a/generic/views.py b/generic/views.py index a0d5d5a9..e55e39a8 100644 --- a/generic/views.py +++ b/generic/views.py @@ -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) diff --git a/longs/models.py b/longs/models.py index 172d4cc6..bb71b396 100644 --- a/longs/models.py +++ b/longs/models.py @@ -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, diff --git a/rapids/forms.py b/rapids/forms.py index dafba32e..d37c1b4d 100755 --- a/rapids/forms.py +++ b/rapids/forms.py @@ -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