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

152 lines
4.6 KiB
Python

"""
Platform plugins to support a verified upgrade tool.
"""
import datetime
import pytz
from crum import get_current_request
from django.conf import settings
from django.utils.translation import ugettext as _
from django.urls import reverse
from common.djangoapps.course_modes.models import CourseMode
from lms.djangoapps.courseware.utils import verified_upgrade_deadline_link
from openedx.features.course_experience.course_tools import CourseTool
from common.djangoapps.student.models import CourseEnrollment
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
class VerifiedUpgradeTool(CourseTool):
"""
The verified upgrade tool.
"""
@classmethod
def analytics_id(cls):
"""
Returns an id to uniquely identify this tool in analytics events.
"""
return 'edx.tool.verified_upgrade'
@classmethod
def is_enabled(cls, request, course_key):
"""
Show this tool to all learners who are eligible to upgrade.
"""
enrollment = CourseEnrollment.get_enrollment(request.user, course_key)
if enrollment is None:
return False
if enrollment.dynamic_upgrade_deadline is None:
return False
if not enrollment.is_active:
return False
if enrollment.mode not in CourseMode.UPSELL_TO_VERIFIED_MODES:
return False
if enrollment.course_upgrade_deadline is None:
return False
if datetime.datetime.now(pytz.UTC) >= enrollment.course_upgrade_deadline:
return False
return True
@classmethod
def title(cls):
"""
Returns the title of this tool.
"""
return _('Upgrade to Verified')
@classmethod
def icon_classes(cls):
"""
Returns the icon classes needed to represent this tool.
"""
return 'fa fa-certificate'
@classmethod
def url(cls, course_key):
"""
Returns the URL for this tool for the specified course key.
"""
request = get_current_request()
return verified_upgrade_deadline_link(request.user, course_id=course_key)
class FinancialAssistanceTool(CourseTool):
"""
The financial assistance tool.
"""
@classmethod
def analytics_id(cls):
"""
Returns an id to uniquely identify this tool in analytics events.
"""
return 'edx.tool.financial_assistance'
@classmethod
def is_enabled(cls, request, course_key):
"""
Show this link for active courses where financial assistance is available, unless upgrade deadline has passed
"""
now = datetime.datetime.now(pytz.UTC)
feature_flags = None
try:
course_overview = CourseOverview.objects.get(id=course_key)
except CourseOverview.DoesNotExist:
course_overview = None
# hide link if there's no ENABLE_FINANCIAL_ASSISTANCE_FORM setting (ex: Edge) or if it's False
subset_name = 'FEATURES'
feature_flags = getattr(settings, subset_name)
if feature_flags is None or not feature_flags.get('ENABLE_FINANCIAL_ASSISTANCE_FORM'):
return False
# hide link for archived courses
if course_overview is not None and course_overview.end_date is not None and now > course_overview.end_date:
return False
# hide link if not logged in or user not enrolled in the course
if not request.user or not CourseEnrollment.is_enrolled(request.user, course_key):
return False
enrollment = CourseEnrollment.get_enrollment(request.user, course_key)
# hide if we're no longer in an upsell mode (already upgraded)
if enrollment.mode not in CourseMode.UPSELL_TO_VERIFIED_MODES:
return False
# hide if there's no course_upgrade_deadline, or one with a value in the past
if enrollment.course_upgrade_deadline:
if now > enrollment.course_upgrade_deadline:
return False
else:
return False
return bool(course_overview.eligible_for_financial_aid)
@classmethod
def title(cls, course_key=None):
"""
Returns the title of this tool.
"""
return _('Financial Assistance')
@classmethod
def icon_classes(cls, course_key=None):
"""
Returns the icon classes needed to represent this tool.
"""
return 'fa fa-info'
@classmethod
def url(cls, course_key):
"""
Returns the URL for this tool for the specified course key.
"""
return reverse('financial_assistance')