From df83c523e13f2e6f22abc29a65b62891039027d8 Mon Sep 17 00:00:00 2001 From: Jacob Pierce Date: Wed, 3 Jul 2024 14:48:46 -0700 Subject: [PATCH 1/2] use timezone.now for Exam.date_created, move def from AbstractExam -> DraftExam --- .../0009_alter_exam_date_created.py | 19 +++++++++++++++++++ kolibri/core/exams/models.py | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 kolibri/core/exams/migrations/0009_alter_exam_date_created.py diff --git a/kolibri/core/exams/migrations/0009_alter_exam_date_created.py b/kolibri/core/exams/migrations/0009_alter_exam_date_created.py new file mode 100644 index 00000000000..0761ec5325c --- /dev/null +++ b/kolibri/core/exams/migrations/0009_alter_exam_date_created.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.25 on 2024-07-03 21:46 +import django.utils.timezone +from django.db import migrations +from django.db import models + + +class Migration(migrations.Migration): + + dependencies = [ + ("exams", "0008_draft_exams"), + ] + + operations = [ + migrations.AlterField( + model_name="exam", + name="date_created", + field=models.DateTimeField(default=django.utils.timezone.now), + ), + ] diff --git a/kolibri/core/exams/models.py b/kolibri/core/exams/models.py index 1c72bc9e89c..7b7a191d2be 100644 --- a/kolibri/core/exams/models.py +++ b/kolibri/core/exams/models.py @@ -158,8 +158,6 @@ class Meta: """ data_model_version = models.SmallIntegerField(default=3) - date_created = models.DateTimeField(auto_now_add=True, null=True) - def __str__(self): return self.title @@ -196,6 +194,8 @@ class DraftExam(AbstractExam): assignments = JSONField(default=list, blank=True) learner_ids = JSONField(default=list, blank=True) + date_created = models.DateTimeField(auto_now_add=True, null=True) + def to_exam(self): """ Convert this draft exam to an exam object. @@ -235,7 +235,7 @@ class Exam(AbstractExam, AbstractFacilityDataModel): # Is this exam currently active and visible to students to whom it is assigned? active = models.BooleanField(default=False) - date_created = models.DateTimeField(null=True) + date_created = models.DateTimeField(default=timezone.now) # To be set True when the quiz is first set to active=True date_activated = models.DateTimeField(default=None, null=True, blank=True) From 61416205a8edb22ddf3812de9644c92a2c74551e Mon Sep 17 00:00:00 2001 From: Jacob Pierce Date: Wed, 3 Jul 2024 15:05:12 -0700 Subject: [PATCH 2/2] update sorting lambda to account for possible None value --- kolibri/core/exams/api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kolibri/core/exams/api.py b/kolibri/core/exams/api.py index 7ca4c7c89d4..5c202d85388 100644 --- a/kolibri/core/exams/api.py +++ b/kolibri/core/exams/api.py @@ -1,3 +1,5 @@ +import datetime + from django.http import Http404 from django.utils.timezone import now from django_filters.rest_framework import DjangoFilterBackend @@ -152,9 +154,10 @@ def list(self, request, *args, **kwargs): # Consolidate the exam_queryset and draft_queryset # and sort them by reverse date_created + dt_utc_aware = datetime.datetime.fromtimestamp(0, datetime.timezone.utc) all_objects = sorted( [*exam_objects, *draft_objects], - key=lambda x: x["date_created"], + key=lambda x: x["date_created"] or dt_utc_aware, reverse=True, )