Files
edx-platform/openedx/features/course_experience/views/course_dates.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

85 lines
3.5 KiB
Python

"""
Fragment for rendering the course dates sidebar.
"""
from django.db import transaction
from django.http import Http404
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.translation import get_language_bidi
from opaque_keys.edx.keys import CourseKey
from web_fragments.fragment import Fragment
from lms.djangoapps.courseware.access import has_access
from lms.djangoapps.courseware.courses import get_course_date_blocks, get_course_with_access
from lms.djangoapps.courseware.tabs import DatesTab
from lms.djangoapps.course_home_api.toggles import course_home_mfe_dates_tab_is_active
from lms.djangoapps.course_home_api.utils import get_microfrontend_url
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView
class CourseDatesFragmentView(EdxFragmentView):
"""
A fragment to important dates within a course.
"""
template_name = 'course_experience/course-dates-fragment.html'
def render_to_fragment(self, request, course_id=None, **kwargs):
"""
Render the course dates fragment.
"""
course_key = CourseKey.from_string(course_id)
course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=False)
course_date_blocks = get_course_date_blocks(course, request.user, request, num_assignments=1)
dates_tab_enabled = DatesTab.is_enabled(course, request.user)
if course_home_mfe_dates_tab_is_active(course_key):
dates_tab_link = get_microfrontend_url(course_key=course.id, view_name='dates')
else:
dates_tab_link = reverse('dates', args=[course.id])
context = {
'course_date_blocks': [block for block in course_date_blocks if block.title != 'current_datetime'],
'dates_tab_link': dates_tab_link,
'dates_tab_enabled': dates_tab_enabled,
}
html = render_to_string(self.template_name, context)
dates_fragment = Fragment(html)
self.add_fragment_resource_urls(dates_fragment)
return dates_fragment
@method_decorator(transaction.non_atomic_requests, name='dispatch')
class CourseDatesFragmentMobileView(CourseDatesFragmentView):
"""
A course dates fragment to show dates on mobile apps.
Mobile apps uses WebKit mobile client to create and maintain a session with
the server for authenticated requests, and it hasn't exposed any way to find
out either session was created with the server or not so mobile app uses a
mechanism to automatically create/recreate session with the server for all
authenticated requests if the server returns 404.
"""
template_name = 'course_experience/mobile/course-dates-fragment.html'
def get(self, request, *args, **kwargs):
if not request.user.is_authenticated:
raise Http404
return super(CourseDatesFragmentMobileView, self).get(request, *args, **kwargs)
def css_dependencies(self):
"""
Returns list of CSS files that this view depends on.
The helper function that it uses to obtain the list of CSS files
works in conjunction with the Django pipeline to ensure that in development mode
the files are loaded individually, but in production just the single bundle is loaded.
"""
if get_language_bidi():
return self.get_css_dependencies('style-mobile-rtl')
else:
return self.get_css_dependencies('style-mobile')