diff --git a/common/djangoapps/entitlements/api/v1/views.py b/common/djangoapps/entitlements/api/v1/views.py index 486fdfd5f5..8755dd7023 100644 --- a/common/djangoapps/entitlements/api/v1/views.py +++ b/common/djangoapps/entitlements/api/v1/views.py @@ -332,11 +332,11 @@ class EntitlementEnrollmentViewSet(viewsets.GenericViewSet): ) # Verify that the run is fullfillable - if not is_course_run_entitlement_fullfillable(course_run_key, datetime.datetime.now(UTC), entitlement): + if not is_course_run_entitlement_fullfillable(course_run_key, entitlement): return Response( status=status.HTTP_400_BAD_REQUEST, data={ - 'message': 'The User is unable to enroll in Course Run {course_id} it is not available.'.format( + 'message': 'The User is unable to enroll in Course Run {course_id}, it is not available.'.format( course_id=course_run_id ) } diff --git a/common/djangoapps/entitlements/utils.py b/common/djangoapps/entitlements/utils.py index 55f5be9ae7..eb2270d9e0 100644 --- a/common/djangoapps/entitlements/utils.py +++ b/common/djangoapps/entitlements/utils.py @@ -1,8 +1,10 @@ from course_modes.models import CourseMode +from django.utils import timezone + from openedx.core.djangoapps.content.course_overviews.models import CourseOverview -def is_course_run_entitlement_fullfillable(course_run_id, compare_date, entitlement): +def is_course_run_entitlement_fullfillable(course_run_id, entitlement, compare_date=timezone.now()): """ Checks that the current run meets the following criteria for an entitlement @@ -16,16 +18,16 @@ def is_course_run_entitlement_fullfillable(course_run_id, compare_date, entitlem entitlement: The Entitlement that we are checking against. Returns: - bool: True is the Course Run is fullfillable for the CourseEntitlement. + bool: True if the Course Run is fullfillable for the CourseEntitlement. """ course_overview = CourseOverview.get_from_id(course_run_id) - # Only courses that have not ended will be displayed + # Verify that the course is still running run_start = course_overview.start run_end = course_overview.end is_running = run_start and (not run_end or (run_end and (run_end > compare_date))) - # Only courses that can currently be enrolled in will be displayed + # Verify that the course run can currently be enrolled enrollment_start = course_overview.enrollment_start enrollment_end = course_overview.enrollment_end can_enroll = ( @@ -33,7 +35,7 @@ def is_course_run_entitlement_fullfillable(course_run_id, compare_date, entitlem and (not enrollment_end or enrollment_end > compare_date) ) - # Ensure the course is upgradeable and the mode matches the entitlement's mode. + # Ensure the course run is upgradeable and the mode matches the entitlement's mode unexpired_paid_modes = [mode.slug for mode in CourseMode.paid_modes_for_course(course_run_id)] can_upgrade = unexpired_paid_modes and entitlement.mode in unexpired_paid_modes diff --git a/openedx/core/djangoapps/catalog/utils.py b/openedx/core/djangoapps/catalog/utils.py index 8b0943fcaa..3743697ed7 100644 --- a/openedx/core/djangoapps/catalog/utils.py +++ b/openedx/core/djangoapps/catalog/utils.py @@ -321,11 +321,11 @@ def get_fulfillable_course_runs_for_entitlement(entitlement, course_runs): enrollable_sessions = [] # Only show published course runs that can still be enrolled and upgraded - now = datetime.datetime.now(UTC) + search_time = datetime.datetime.now(UTC) for course_run in course_runs: course_id = CourseKey.from_string(course_run.get('key')) is_enrolled = CourseEnrollment.is_enrolled(entitlement.user, str(course_id)) - if is_course_run_entitlement_fullfillable(course_id, now, entitlement) and not is_enrolled: + if is_course_run_entitlement_fullfillable(course_id, entitlement, search_time) and not is_enrolled: enrollable_sessions.append(course_run) enrollable_sessions.sort(key=lambda session: session.get('start'))