diff --git a/enterprise_catalog/apps/catalog/algolia_utils.py b/enterprise_catalog/apps/catalog/algolia_utils.py index 80e9cc23..fe55566d 100644 --- a/enterprise_catalog/apps/catalog/algolia_utils.py +++ b/enterprise_catalog/apps/catalog/algolia_utils.py @@ -30,6 +30,8 @@ RESTRICTION_FOR_B2B, VIDEO, ) +from enterprise_catalog.apps.catalog.content_metadata_utils import \ + get_advertised_course_run as imported_get_advertised_course_run from enterprise_catalog.apps.catalog.content_metadata_utils import ( get_course_first_paid_enrollable_seat_price, get_course_run_by_uuid, @@ -1179,10 +1181,9 @@ def get_advertised_course_run(course): dict: containing key, pacing_type, start, end, and upgrade deadline for the course_run, or None """ - full_course_run = get_course_run_by_uuid(course, course.get('advertised_course_run_uuid')) - if full_course_run is None: - return None - return _get_course_run(course, full_course_run) + if advertised_course_run := imported_get_advertised_course_run(course): + return _get_course_run(course, advertised_course_run) + return advertised_course_run def get_course_runs(course): diff --git a/enterprise_catalog/apps/catalog/content_metadata_utils.py b/enterprise_catalog/apps/catalog/content_metadata_utils.py index da894034..088d0fdd 100644 --- a/enterprise_catalog/apps/catalog/content_metadata_utils.py +++ b/enterprise_catalog/apps/catalog/content_metadata_utils.py @@ -102,3 +102,20 @@ def get_course_first_paid_enrollable_seat_price(course): if 'first_enrollable_paid_seat_price' in course_run: return course_run['first_enrollable_paid_seat_price'] return None + + +def get_advertised_course_run(course): + """ + Get part of the advertised course_run as per advertised_course_run_uuid + + Argument: + course (dict) + + Returns: + dict: containing key, pacing_type, start, end, and upgrade deadline + for the course_run, or None + """ + full_course_run = get_course_run_by_uuid(course, course.get('advertised_course_run_uuid')) + if full_course_run is None: + return None + return full_course_run diff --git a/enterprise_catalog/apps/catalog/models.py b/enterprise_catalog/apps/catalog/models.py index aaaf7999..b397150c 100644 --- a/enterprise_catalog/apps/catalog/models.py +++ b/enterprise_catalog/apps/catalog/models.py @@ -45,6 +45,7 @@ json_serialized_course_modes, ) from enterprise_catalog.apps.catalog.content_metadata_utils import ( + get_advertised_course_run, get_course_first_paid_enrollable_seat_price, ) from enterprise_catalog.apps.catalog.utils import ( @@ -555,10 +556,15 @@ def get_content_enrollment_url(self, content_metadata): else: # Catalog param only needed for legacy (non-learner-portal) enrollment URLs params['catalog'] = self.uuid + + course_run_key = content_key + if not parent_content_key: + if advertised_course_run := get_advertised_course_run(content_metadata.json_metadata): + course_run_key = advertised_course_run['key'] url = '{}/enterprise/{}/course/{}/enroll/'.format( settings.LMS_BASE_URL, self.enterprise_uuid, - content_key, + course_run_key, ) return update_query_parameters(url, params) diff --git a/enterprise_catalog/apps/catalog/tests/factories.py b/enterprise_catalog/apps/catalog/tests/factories.py index 45fab4b4..93ef6394 100644 --- a/enterprise_catalog/apps/catalog/tests/factories.py +++ b/enterprise_catalog/apps/catalog/tests/factories.py @@ -99,7 +99,7 @@ def _json_metadata(self): 'logo_image_url': fake.image_url() + '.jpg', }] course_runs = [{ - 'key': 'course-v1:edX+DemoX', + 'key': 'course-v1:edX+DemoX+2T2024', 'uuid': str(FAKE_ADVERTISED_COURSE_RUN_UUID), 'content_language': 'en-us', 'status': 'published', diff --git a/enterprise_catalog/apps/catalog/tests/test_models.py b/enterprise_catalog/apps/catalog/tests/test_models.py index 03b7367f..47f97e32 100644 --- a/enterprise_catalog/apps/catalog/tests/test_models.py +++ b/enterprise_catalog/apps/catalog/tests/test_models.py @@ -623,6 +623,46 @@ def test_get_content_enrollment_url( else: assert settings.LMS_BASE_URL in content_enrollment_url + @ddt.data( + { + 'content_key': 'course-v1:edX+DemoX+2T2024', + 'parent_content_key': 'edX+DemoX', + }, + { + 'content_key': 'edX+DemoX', + 'parent_content_key': None + }, + ) + @ddt.unpack + def test_get_content_enrollment_url_disabled_learner_portal( + self, + content_key, + parent_content_key, + ): + enterprise_uuid = uuid4() + enterprise_slug = 'sluggy' + + enterprise_catalog = factories.EnterpriseCatalogFactory(enterprise_uuid=enterprise_uuid) + content_metadata = factories.ContentMetadataFactory( + content_key=content_key, + parent_content_key=parent_content_key, + content_type=COURSE, + ) + + enterprise_catalog.catalog_query.contentmetadata_set.add(*[content_metadata]) + + mock_enterprise_customer_return_value = { + 'slug': enterprise_slug, + 'enable_learner_portal': False, + } + + with self._mock_enterprise_customer_cache( + mock_enterprise_customer_return_value, + ): + content_enrollment_url = enterprise_catalog.get_content_enrollment_url(content_metadata) + + assert "/course/course-v1:edX+DemoX+2T2024/enroll/" in content_enrollment_url + @mock.patch('enterprise_catalog.apps.api_client.enterprise_cache.EnterpriseApiClient') @ddt.data( { diff --git a/enterprise_catalog/apps/catalog/tests/test_serializers.py b/enterprise_catalog/apps/catalog/tests/test_serializers.py index 78ada547..82430e96 100644 --- a/enterprise_catalog/apps/catalog/tests/test_serializers.py +++ b/enterprise_catalog/apps/catalog/tests/test_serializers.py @@ -8,11 +8,13 @@ COURSE, EXEC_ED_2U_COURSE_TYPE, ) +from enterprise_catalog.apps.catalog.content_metadata_utils import ( + get_course_run_by_uuid, +) from enterprise_catalog.apps.catalog.serializers import ( NormalizedContentMetadataSerializer, ) from enterprise_catalog.apps.catalog.tests import factories -from enterprise_catalog.apps.catalog.utils import get_course_run_by_uuid @ddt.ddt diff --git a/enterprise_catalog/apps/catalog/utils.py b/enterprise_catalog/apps/catalog/utils.py index 7a046424..ada81ea9 100644 --- a/enterprise_catalog/apps/catalog/utils.py +++ b/enterprise_catalog/apps/catalog/utils.py @@ -158,21 +158,3 @@ def to_timestamp(datetime_str): except (ValueError, TypeError) as exc: LOGGER.error(f"[to_timestamp][{exc}] Could not parse date string: {datetime_str}") return None - - -def get_course_run_by_uuid(course, course_run_uuid): - """ - Find a course_run based on uuid - - Arguments: - course (dict): course dict - course_run_uuid (str): uuid to lookup - - Returns: - dict: a course_run or None - """ - try: - course_run = [run for run in course.get('course_runs', []) if run.get('uuid') == course_run_uuid][0] - except IndexError: - return None - return course_run