Skip to content

Commit

Permalink
feat: Remove api calls for content enrollment url
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Jul 18, 2023
1 parent 57e16cf commit 512371b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 229 deletions.
9 changes: 1 addition & 8 deletions enterprise_catalog/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
get_parent_content_key,
localized_utcnow,
)
from enterprise_catalog.apps.catalog.waffle import (
LEARNER_PORTAL_ENROLLMENT_ALL_SUBSIDIES_AND_CONTENT_TYPES_FLAG,
)


@ddt.ddt
Expand Down Expand Up @@ -1476,11 +1473,7 @@ def test_get_content_metadata_no_nested_enrollment_urls_exec_ed_2u(
metadata = course_runs + [course]
self.add_metadata_to_catalog(self.enterprise_catalog, metadata)

with override_waffle_flag(
LEARNER_PORTAL_ENROLLMENT_ALL_SUBSIDIES_AND_CONTENT_TYPES_FLAG,
active=is_learner_portal_enabled,
):
response = self.client.get(self._get_content_metadata_url(self.enterprise_catalog))
response = self.client.get(self._get_content_metadata_url(self.enterprise_catalog))

self.maxDiff = None
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down
40 changes: 0 additions & 40 deletions enterprise_catalog/apps/api_client/enterprise_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ def slug(self):
"""
return self.customer_data.get('slug', '')

@property
def active_catalogs(self):
"""
Return catalogs associated with active coupon code and subscription catalogs for the enterprise customer.
"""
catalogs = self.customer_data.get('coupons_catalogs', []) + self.customer_data.get('subscriptions_catalogs', [])
return list(set(catalogs))

@property
def last_modified_date(self):
"""
Expand Down Expand Up @@ -94,38 +86,6 @@ def _get_enterprise_customer_data(uuid):
logger.warning('Received unexpected customer_data for enterprise customer %s', uuid)
customer_data = {}

try:
ecommerce_client = EcommerceApiClient()
coupons_overview = ecommerce_client.get_coupons_overview(uuid)
coupons_catalogs = [coupon['enterprise_catalog_uuid'] for coupon in coupons_overview]
except requests.exceptions.RequestException as exc:
logger.error(
'Failed to fetch coupons overview for %r because %r',
uuid,
exc,
)
coupons_catalogs = []

try:
license_manager_client = LicenseManagerApiClient()
customer_agreement = license_manager_client.get_customer_agreement(uuid)
if customer_agreement:
subscriptions_catalogs = [
subscription['enterprise_catalog_uuid'] for subscription in customer_agreement['subscriptions']
]
else:
subscriptions_catalogs = []
except requests.exceptions.RequestException as exc:
logger.error(
'Failed to fetch customer agreement for %r because %r',
uuid,
exc,
)
subscriptions_catalogs = []

customer_data['coupons_catalogs'] = coupons_catalogs
customer_data['subscriptions_catalogs'] = subscriptions_catalogs

cache.set(cache_key, customer_data, settings.ENTERPRISE_CUSTOMER_CACHE_TIMEOUT)

return customer_data
60 changes: 7 additions & 53 deletions enterprise_catalog/apps/catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
get_parent_content_key,
localized_utcnow,
)
from enterprise_catalog.apps.catalog.waffle import (
use_learner_portal_for_all_subsidies_content_types,
)


LOGGER = getLogger(__name__)
Expand Down Expand Up @@ -398,11 +395,7 @@ def get_content_enrollment_url(self, content_metadata):
if self.publish_audit_enrollment_urls:
params['audit'] = 'true'

use_learner_portal_for_all = use_learner_portal_for_all_subsidies_content_types()
can_enroll_with_learner_portal = self._can_enroll_via_learner_portal(
content_key,
is_learner_portal_enabled_for_all_subsidies_content_types=use_learner_portal_for_all
)
can_enroll_with_learner_portal = self._can_enroll_via_learner_portal

if content_metadata.is_exec_ed_2u_course:
if not self.catalog_query.include_exec_ed_2u_courses:
Expand All @@ -411,10 +404,9 @@ def get_content_enrollment_url(self, content_metadata):
exec_ed_enroll_url, exec_ed_entitlement_sku = self._get_exec_ed_2u_enrollment_url(
content_metadata,
enterprise_slug=self.enterprise_customer.slug,
use_learner_portal=(can_enroll_with_learner_portal
and use_learner_portal_for_all),
use_learner_portal=can_enroll_with_learner_portal,
)
if can_enroll_with_learner_portal and use_learner_portal_for_all:
if can_enroll_with_learner_portal:
return update_query_parameters(exec_ed_enroll_url, params)

if not exec_ed_entitlement_sku:
Expand Down Expand Up @@ -472,52 +464,14 @@ def _get_exec_ed_2u_enrollment_url(self, content_metadata, enterprise_slug, use_
entitlement_sku,
)

def _can_enroll_via_learner_portal(self, content_key, is_learner_portal_enabled_for_all_subsidies_content_types):
@property
def _can_enroll_via_learner_portal(self):
"""
Check whether the enterprise customer has the learner portal enabled. Note that enterprise
offers were previously only partially supported by the learner portal (i.e., it did not
support per-learner spend or enrollment limits). As a result, we had to avoid the learner
portal as an enrollment url for enterprise customers who had the learner portal enabled
for other subsidy types like a subscription but with at least one enterprise offer with a
per-learner limit.
Now, the learner portal does support all enterprise offer configurations so we no longer
need to check against the specific enterprise customer exclusion list via the setting
`INTEGRATED_CUSTOMERS_WITH_SUBSIDIES_AND_OFFERS`. To bypass the existing behavior during
this transition, the `use_learner_portal_for_all_subsidies_content_types` kwarg may be set
to True (e.g., based on a Waffle flag).
If `use_learner_portal_for_all_subsidies_content_types` is False, falls back to existing behavior
of checking whether the enterprise customer is in `INTEGRATED_CUSTOMERS_WITH_SUBSIDIES_AND_OFFERS`
settings list and, if so, check if the enterprise customer has any non-offer related catalogs (e.g.,
subscription license) containing the specified content key, in which case the learner portal is
supported.
Check whether the enterprise customer has the learner portal enabled.
"""
if not self.enterprise_customer.learner_portal_enabled:
return False

# If the waffle flag is active, we want to allow enrollment via the learner portal.
if is_learner_portal_enabled_for_all_subsidies_content_types:
return True

# Otherwise, the waffle flag is inactive; fall back to current state where we check
# against `settings.INTEGRATED_CUSTOMERS_WITH_SUBSIDIES_AND_OFFERS`.
is_integrated_customer_with_offer_and_nonoffer_subsidies = (
str(self.enterprise_uuid) in
settings.INTEGRATED_CUSTOMERS_WITH_SUBSIDIES_AND_OFFERS
)
if not is_integrated_customer_with_offer_and_nonoffer_subsidies:
return True

# Customers in the special `INTEGRATED_CUSTOMERS_WITH_SUBSIDIES_AND_OFFERS` list
# must have the content_key contained in an active catalog for this method to return True.
active_catalogs = EnterpriseCatalog.objects.filter(uuid__in=self.enterprise_customer.active_catalogs)
for catalog in active_catalogs:
contains_content_items = catalog.contains_content_keys([content_key])
if contains_content_items:
return True

return False
return True

def get_xapi_activity_id(self, content_resource, content_key):
"""
Expand Down
Loading

0 comments on commit 512371b

Please sign in to comment.