From dd53edc6e035eb9656b1b276a40d51efcf1480c1 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 29 Sep 2017 14:22:58 -0400 Subject: [PATCH] Cache upgrade_deadline on CourseEnrollment objects --- common/djangoapps/student/models.py | 2 +- common/djangoapps/student/tests/test_models.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 459342a493..934c250f2b 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -1696,7 +1696,7 @@ class CourseEnrollment(models.Model): def verified_mode(self): return CourseMode.verified_mode_for_course(self.course_id) - @property + @cached_property def upgrade_deadline(self): """ Returns the upgrade deadline for this enrollment, if it is upgradeable. diff --git a/common/djangoapps/student/tests/test_models.py b/common/djangoapps/student/tests/test_models.py index e8c73458d1..cbee80d12e 100644 --- a/common/djangoapps/student/tests/test_models.py +++ b/common/djangoapps/student/tests/test_models.py @@ -132,6 +132,21 @@ class CourseEnrollmentTests(SharedModuleStoreTestCase): self.assertEqual(Schedule.objects.all().count(), 0) self.assertEqual(enrollment.upgrade_deadline, course_mode.expiration_datetime) + + @skip_unless_lms + # NOTE: We mute the post_save signal to prevent Schedules from being created for new enrollments + @factory.django.mute_signals(signals.post_save) + def test_upgrade_deadline_with_schedule(self): + """ The property should use either the CourseMode or related Schedule to determine the deadline. """ + course = CourseFactory(self_paced=True) + CourseModeFactory( + course_id=course.id, + mode_slug=CourseMode.VERIFIED, + # This must be in the future to ensure it is returned by downstream code. + expiration_datetime=datetime.datetime.now(pytz.UTC) + datetime.timedelta(days=1), + ) + enrollment = CourseEnrollmentFactory(course_id=course.id, mode=CourseMode.AUDIT) + # The schedule's upgrade deadline should be used if a schedule exists DynamicUpgradeDeadlineConfiguration.objects.create(enabled=True) schedule = ScheduleFactory(enrollment=enrollment)