From ea7d8c7c4658a60c0a3108adf95ff8b17f8b8896 Mon Sep 17 00:00:00 2001 From: hamzawaleed01 Date: Tue, 28 Jan 2025 13:38:44 +0500 Subject: [PATCH] refactor: and remove duplicate function --- enterprise_catalog/apps/api/v1/utils.py | 25 +++---------------- .../apps/catalog/content_metadata_utils.py | 12 ++++++--- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/enterprise_catalog/apps/api/v1/utils.py b/enterprise_catalog/apps/api/v1/utils.py index 71afe313..f1b45eee 100644 --- a/enterprise_catalog/apps/api/v1/utils.py +++ b/enterprise_catalog/apps/api/v1/utils.py @@ -10,6 +10,10 @@ urlunsplit, ) +from enterprise_catalog.apps.catalog.content_metadata_utils import ( + is_course_run_active, +) + logger = logging.getLogger(__name__) @@ -59,27 +63,6 @@ def get_enterprise_utm_context(enterprise_name): return utm_context -def is_course_run_active(course_run): - """ - Checks whether a course run is active. That is, whether the course run is published, - enrollable, and marketable. - - Arguments: - course_run (dict): The metadata about a course run. - - Returns: - bool: True if course run is "active" - """ - is_enrollable = course_run.get('is_enrollable', False) - if course_run.get("is_marketable_external") and is_enrollable: - return True - course_run_status = course_run.get('status') or '' - is_published = course_run_status.lower() == 'published' - is_marketable = course_run.get('is_marketable', False) - - return is_published and is_enrollable and is_marketable - - def is_any_course_run_active(course_runs): """ Iterates over all course runs to check if there any course run that is available for enrollment. diff --git a/enterprise_catalog/apps/catalog/content_metadata_utils.py b/enterprise_catalog/apps/catalog/content_metadata_utils.py index 5cead3f2..046dc801 100644 --- a/enterprise_catalog/apps/catalog/content_metadata_utils.py +++ b/enterprise_catalog/apps/catalog/content_metadata_utils.py @@ -66,19 +66,23 @@ def is_course_run_active(course_run): """ Checks whether a course run is active. That is, whether the course run is published, enrollable, and marketable. + Checking is_published in case of is_marketable_external is redundant because + the Discovery service already handles the status behind is_marketable_external + property. Arguments: course_run (dict): The metadata about a course run. Returns: bool: True if course run is "active" """ - is_enrollable = course_run.get('is_enrollable', False) - if course_run.get("is_marketable_external") and is_enrollable: - return True course_run_status = course_run.get('status') or '' is_published = course_run_status.lower() == 'published' is_marketable = course_run.get('is_marketable', False) + is_enrollable = course_run.get('is_enrollable', False) + + is_marketable_internal = is_published and is_marketable + is_marketable_external = course_run.get("is_marketable_external", False) - return is_published and is_enrollable and is_marketable + return is_enrollable and (is_marketable_internal or is_marketable_external) def get_course_first_paid_enrollable_seat_price(course):