diff --git a/lms/djangoapps/courseware/toggles.py b/lms/djangoapps/courseware/toggles.py new file mode 100644 index 000000000000..0058f7631f3b --- /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: TNL-6982 +# .. 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 525af9721c7a..52bea116a8d8 100644 --- a/lms/djangoapps/courseware/url_helpers.py +++ b/lms/djangoapps/courseware/url_helpers.py @@ -4,6 +4,7 @@ import six +from django.conf import settings from django.urls import reverse from six.moves.urllib.parse import urlencode # pylint: disable=import-error @@ -52,3 +53,46 @@ 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=None): + """ + 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 + ) + + if path is None: + return redirect_url + + # 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 300de1229d50..936c1fe981a7 100644 --- a/lms/envs/devstack.py +++ b/lms/envs/devstack.py @@ -295,4 +295,4 @@ def should_show_debug_toolbar(request): EDXNOTES_CLIENT_NAME = 'edx_notes_api-backend-service' ############## Settings for Microfrontends ######################### -LEARNING_MICROFRONTEND_URL = 'http://localhost:2000/' +LEARNING_MICROFRONTEND_URL = 'http://localhost:2000' diff --git a/lms/envs/test.py b/lms/envs/test.py index af51f1c4e981..ff3cd29a94a5 100644 --- a/lms/envs/test.py +++ b/lms/envs/test.py @@ -593,6 +593,7 @@ PROFILE_MICROFRONTEND_URL = "http://profile-mfe/abc/" ORDER_HISTORY_MICROFRONTEND_URL = "http://order-history-mfe/" ACCOUNT_MICROFRONTEND_URL = "http://account-mfe/" +LEARNING_MICROFRONTEND_URL = "http://learning-mfe" ########################## limiting dashboard courses ######################