Files
edx-platform/lms/djangoapps/courseware/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

80 lines
3.0 KiB
Python

"""Utility functions that have to do with the courseware."""
import datetime
from django.conf import settings
from lms.djangoapps.commerce.utils import EcommerceService
from pytz import utc
from common.djangoapps.course_modes.models import CourseMode
from xmodule.partitions.partitions import ENROLLMENT_TRACK_PARTITION_ID
from xmodule.partitions.partitions_service import PartitionService
def verified_upgrade_deadline_link(user, course=None, course_id=None):
"""
Format the correct verified upgrade link for the specified ``user``
in a course.
One of ``course`` or ``course_id`` must be supplied. If both are specified,
``course`` will take priority.
Arguments:
user (:class:`~django.contrib.auth.models.User`): The user to display
the link for.
course (:class:`.CourseOverview`): The course to render a link for.
course_id (:class:`.CourseKey`): The course_id of the course to render for.
Returns:
The formatted link that will allow the user to upgrade to verified
in this course.
"""
if course is not None:
course_id = course.id
return EcommerceService().upgrade_url(user, course_id)
def can_show_verified_upgrade(user, enrollment, course=None):
"""
Return whether this user can be shown upgrade message.
Arguments:
user (:class:`.AuthUser`): The user from the request.user property
enrollment (:class:`.CourseEnrollment`): The enrollment under consideration.
If None, then the enrollment is not considered to be upgradeable.
course (:class:`.ModulestoreCourse`): Optional passed in modulestore course.
If provided, it is expected to correspond to `enrollment.course.id`.
If not provided, the course will be loaded from the modulestore.
We use the course to retrieve user partitions when calculating whether
the upgrade link will be shown.
"""
if enrollment is None:
return False # this got accidentally flipped in 2017 (commit 8468357), but leaving alone to not switch again
partition_service = PartitionService(enrollment.course.id, course=course)
enrollment_track_partition = partition_service.get_user_partition(ENROLLMENT_TRACK_PARTITION_ID)
group = partition_service.get_group(user, enrollment_track_partition)
current_mode = None
if group:
try:
current_mode = [
mode.get('slug') for mode in settings.COURSE_ENROLLMENT_MODES.values() if mode['id'] == group.id
].pop()
except IndexError:
pass
upgradable_mode = not current_mode or current_mode in CourseMode.UPSELL_TO_VERIFIED_MODES
if not upgradable_mode:
return False
upgrade_deadline = enrollment.upgrade_deadline
if upgrade_deadline is None:
return False
if datetime.datetime.now(utc).date() > upgrade_deadline.date():
return False
# Show the summary if user enrollment is in which allow user to upsell
return enrollment.is_active and enrollment.mode in CourseMode.UPSELL_TO_VERIFIED_MODES