Skip to content

Commit

Permalink
fix: set enrollment url course id to advertised course run key
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Jan 6, 2025
1 parent 1ded448 commit 1f4c027
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
9 changes: 5 additions & 4 deletions enterprise_catalog/apps/catalog/algolia_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions enterprise_catalog/apps/catalog/content_metadata_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion enterprise_catalog/apps/catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion enterprise_catalog/apps/catalog/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
40 changes: 40 additions & 0 deletions enterprise_catalog/apps/catalog/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
4 changes: 3 additions & 1 deletion enterprise_catalog/apps/catalog/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 0 additions & 18 deletions enterprise_catalog/apps/catalog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1f4c027

Please sign in to comment.