diff --git a/lms/djangoapps/courseware/toggles.py b/lms/djangoapps/courseware/toggles.py new file mode 100644 index 0000000000..7e7a5a52ba --- /dev/null +++ b/lms/djangoapps/courseware/toggles.py @@ -0,0 +1,30 @@ +""" +Toggles for courseware in-course experience. +""" + +from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers +from openedx.core.djangoapps.waffle_utils import CourseWaffleFlag, WaffleFlagNamespace + +# Namespace for courseware waffle flags. +WAFFLE_FLAG_NAMESPACE = WaffleFlagNamespace(name='courseware') + +# Waffle flag to redirect to another learner profile experience. +# .. toggle_name: courseware.redirect_to_microfrontend +# .. toggle_implementation: CourseWaffleFlag +# .. toggle_default: False +# .. toggle_description: Supports staged rollout of a new micro-frontend-based implementation of the courseware page. +# .. toggle_category: micro-frontend +# .. toggle_use_cases: incremental_release, open_edx +# .. toggle_creation_date: 2020-01-29 +# .. toggle_expiration_date: 2020-12-31 +# .. toggle_warnings: Also set settings.LEARNING_MICROFRONTEND_URL and ENABLE_COURSEWARE_MICROFRONTEND. +# .. toggle_tickets: +# .. toggle_status: supported +REDIRECT_TO_COURSEWARE_MICROFRONTEND = CourseWaffleFlag(WAFFLE_FLAG_NAMESPACE, 'redirect_to_microfrontend') + + +def should_redirect_to_courseware_microfrontend(course_key): + return ( + configuration_helpers.get_value('ENABLE_COURSEWARE_MICROFRONTEND') and + REDIRECT_TO_COURSEWARE_MICROFRONTEND.is_enabled(course_key) + ) diff --git a/lms/djangoapps/courseware/url_helpers.py b/lms/djangoapps/courseware/url_helpers.py index 525af9721c..e8738163e8 100644 --- a/lms/djangoapps/courseware/url_helpers.py +++ b/lms/djangoapps/courseware/url_helpers.py @@ -2,14 +2,14 @@ Module to define url helpers functions """ - import six +from django.conf import settings from django.urls import reverse from six.moves.urllib.parse import urlencode # pylint: disable=import-error from xmodule.modulestore.django import modulestore from xmodule.modulestore.search import navigation_index, path_to_location - +from lms.djangoapps.courseware.toggles import should_redirect_to_courseware_microfrontend def get_redirect_url(course_key, usage_key, request=None): """ Returns the redirect url back to courseware @@ -25,6 +25,10 @@ def get_redirect_url(course_key, usage_key, request=None): Redirect url string """ + if should_redirect_to_courseware_microfrontend(course_key): + path = path_to_location(modulestore(), usage_key, request, full_path=True) + return get_microfrontend_redirect_url(course_key, path) + ( course_key, chapter, section, vertical_unused, position, final_target_id @@ -52,3 +56,39 @@ def get_redirect_url(course_key, usage_key, request=None): ) redirect_url += "?{}".format(urlencode({'activate_block_id': six.text_type(final_target_id)})) return redirect_url + +def get_microfrontend_redirect_url(course_key, path): + """ + The micro-frontend determines the user's position in the vertical via + a separate API call, so all we need here is the course_key, section, and vertical + IDs to format it's URL. + + It is also capable of determining our section and vertical if they're not present. Fully specifying it all is preferable, though, as the micro-frontend can save itself some work, resulting in a better user experience. + + We're building a URL like this: + + http://localhost:2000/course-v1:edX+DemoX+Demo_Course/block-v1:edX+DemoX+Demo_Course+type@sequential+block@19a30717eff543078a5d94ae9d6c18a5/block-v1:edX+DemoX+Demo_Course+type@vertical+block@4a1bba2a403f40bca5ec245e945b0d76 + """ + + redirect_url = '{base_url}/{prefix}/{course_key}'.format( + base_url=settings.LEARNING_MICROFRONTEND_URL, + prefix='course/', + course_key=course_key + ) + + # The first four elements of the path list are the ones we care about here: + # - course + # - chapter + # - sequence + # - vertical + # We skip course because we already have it from our argument above, and we skip chapter because the micro-frontend URL doesn't include it. + if len(path) > 2: + redirect_url += '/{sequence_key}'.format( + sequence_key=path[2] + ) + if len(path) > 3: + redirect_url += '/{vertical_key}'.format( + vertical_key=path[3] + ) + + return redirect_url diff --git a/lms/envs/devstack.py b/lms/envs/devstack.py index 300de1229d..936c1fe981 100644 --- a/lms/envs/devstack.py +++ b/lms/envs/devstack.py @@ -295,4 +295,4 @@ EDXNOTES_INTERNAL_API = 'http://edx.devstack.edxnotesapi:18120/api/v1' EDXNOTES_CLIENT_NAME = 'edx_notes_api-backend-service' ############## Settings for Microfrontends ######################### -LEARNING_MICROFRONTEND_URL = 'http://localhost:2000/' +LEARNING_MICROFRONTEND_URL = 'http://localhost:2000'