Files
edx-platform/common/djangoapps/entitlements/utils.py
Kyle McCormick 151bd13666 Use full names for common.djangoapps imports; warn when using old style (#25477)
* Generate common/djangoapps import shims for LMS
* Generate common/djangoapps import shims for Studio
* Stop appending project root to sys.path
* Stop appending common/djangoapps to sys.path
* Import from common.djangoapps.course_action_state instead of course_action_state
* Import from common.djangoapps.course_modes instead of course_modes
* Import from common.djangoapps.database_fixups instead of database_fixups
* Import from common.djangoapps.edxmako instead of edxmako
* Import from common.djangoapps.entitlements instead of entitlements
* Import from common.djangoapps.pipline_mako instead of pipeline_mako
* Import from common.djangoapps.static_replace instead of static_replace
* Import from common.djangoapps.student instead of student
* Import from common.djangoapps.terrain instead of terrain
* Import from common.djangoapps.third_party_auth instead of third_party_auth
* Import from common.djangoapps.track instead of track
* Import from common.djangoapps.util instead of util
* Import from common.djangoapps.xblock_django instead of xblock_django
* Add empty common/djangoapps/__init__.py to fix pytest collection
* Fix pylint formatting violations
* Exclude import_shims/ directory tree from linting
2020-11-10 07:02:01 -05:00

61 lines
2.2 KiB
Python

"""
Utility methods for the entitlement application.
"""
import logging
from django.utils import timezone
from common.djangoapps.course_modes.models import CourseMode
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from common.djangoapps.student.models import CourseEnrollment
log = logging.getLogger("common.entitlements.utils")
def is_course_run_entitlement_fulfillable(
course_run_key,
entitlement,
compare_date=timezone.now(),
):
"""
Checks that the current run meets the following criteria for an entitlement
1) A User can enroll in or is currently enrolled
2) A User can upgrade to the entitlement mode
Arguments:
course_run_key (CourseKey): The id of the Course run that is being checked.
entitlement: The Entitlement that we are checking against.
compare_date: The date and time that we are comparing against. Defaults to timezone.now()
Returns:
bool: True if the Course Run is fullfillable for the CourseEntitlement.
"""
try:
course_overview = CourseOverview.get_from_id(course_run_key)
except CourseOverview.DoesNotExist:
log.error((u'There is no CourseOverview entry available for {course_run_id}, '
u'course run cannot be applied to entitlement').format(
course_run_id=str(course_run_key)
))
return False
# Verify that the course run can currently be enrolled
enrollment_start = course_overview.enrollment_start
enrollment_end = course_overview.enrollment_end
can_enroll = (
(not enrollment_start or enrollment_start < compare_date)
and (not enrollment_end or enrollment_end > compare_date)
)
# Is the user already enrolled in the Course Run
is_enrolled = CourseEnrollment.is_enrolled(entitlement.user, course_run_key)
# 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_key)]
can_upgrade = unexpired_paid_modes and entitlement.mode in unexpired_paid_modes
return course_overview.start and can_upgrade and (is_enrolled or can_enroll)