Skip to content

Commit

Permalink
Merge pull request #209 from openedx/pwnage101/ENT-8389
Browse files Browse the repository at this point in the history
feat: supply `parent_content_key` to transaction when redeeming content
  • Loading branch information
pwnage101 authored Feb 26, 2024
2 parents a8df681 + 1934444 commit a383b6a
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 181 deletions.
1 change: 1 addition & 0 deletions enterprise_subsidy/apps/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Meta:
"lms_user_id",
"lms_user_email",
"content_key",
"parent_content_key",
"content_title",
"quantity",
"unit", # Manually fetch from parent ledger via get_unit().
Expand Down
5 changes: 5 additions & 0 deletions enterprise_subsidy/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ class APITestBase(APITestMixin):
subsidy_access_policy_3_uuid = str(uuid.uuid4())
subsidy_4_transaction_1_uuid = str(uuid.uuid4())
subsidy_4_transaction_2_uuid = str(uuid.uuid4())
parent_content_key_1 = "edX+test"
content_key_1 = "course-v1:edX+test+course.1"
content_title_1 = "edx: Test Course 1"
parent_content_key_2 = "edX+test"
content_key_2 = "course-v1:edX+test+course.2"
content_title_2 = "edx: Test Course 2"
lms_user_email = '[email protected]'
Expand All @@ -83,6 +85,7 @@ def setUp(self):
lms_user_email=self.lms_user_email,
subsidy_access_policy_uuid=self.subsidy_access_policy_1_uuid,
content_key=self.content_key_1,
parent_content_key=self.parent_content_key_1,
content_title=self.content_title_1,
)
self.subsidy_1_transaction_2 = TransactionFactory(
Expand All @@ -94,6 +97,7 @@ def setUp(self):
lms_user_email=self.lms_user_email,
subsidy_access_policy_uuid=self.subsidy_access_policy_2_uuid,
content_key=self.content_key_2,
parent_content_key=self.parent_content_key_2,
content_title=self.content_title_2,
)

Expand Down Expand Up @@ -342,6 +346,7 @@ def test_can_redeem_happy_path(self, has_existing_transaction, mock_lms_user_cli
'lms_user_email': self.lms_user_email,
'subsidy_access_policy_uuid': str(self.subsidy_access_policy_1_uuid),
'content_key': self.content_key_1,
'parent_content_key': self.parent_content_key_1,
'content_title': self.content_title_1,
'external_reference': [],
'transaction_status_api_url': f"{self.transaction_status_api_url}/{self.subsidy_1_transaction_1_uuid}/",
Expand Down
36 changes: 28 additions & 8 deletions enterprise_subsidy/apps/subsidy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,27 +294,37 @@ def email_for_learner(self, lms_user_id):
return user_data.get('email')
return None

def title_for_content(self, content_key):
def metadata_summary_for_content(self, content_key):
"""
Best effort return the title of the given content.
Best effort return the metadata summary of the given content.
Returns:
string: Title of content or None.
dict: Metadata summary (or empty dict if there is a bug in our code).
Raises:
ContentNotFoundForCustomerException: If the metadata service returned 404.
"""
content_title = None
content_summary = {}
try:
content_summary = self.content_metadata_api().get_content_summary(
self.enterprise_customer_uuid,
content_key
)
if content_summary:
content_title = content_summary.get('content_title')
except HTTPError as exc:
if exc.response.status_code == status.HTTP_404_NOT_FOUND:
raise ContentNotFoundForCustomerException(
'The given content_key is not in any catalog for this customer.'
) from exc
return content_title
return content_summary

def title_for_content(self, content_key):
"""
Best effort return the title of the given content.
Returns:
string: Title of content or None.
"""
return self.metadata_summary_for_content(content_key).get('content_title')

def price_for_content(self, content_key):
"""
Expand Down Expand Up @@ -346,6 +356,7 @@ def create_transaction(
lms_user_id=None,
lms_user_email=None,
content_key=None,
parent_content_key=None,
content_title=None,
subsidy_access_policy_uuid=None,
**transaction_metadata
Expand All @@ -366,6 +377,7 @@ def create_transaction(
lms_user_id=lms_user_id,
lms_user_email=lms_user_email,
content_key=content_key,
parent_content_key=parent_content_key,
content_title=content_title,
subsidy_access_policy_uuid=subsidy_access_policy_uuid,
**transaction_metadata,
Expand Down Expand Up @@ -442,10 +454,16 @@ def redeem(
return (None, False)
try:
lms_user_email = self.email_for_learner(lms_user_id)
content_title = self.title_for_content(content_key)

# Fetch one or more metadata keys from catalog service, with overall metadata request locally cached.
content_metadata_summary = self.metadata_summary_for_content(content_key)
content_title = content_metadata_summary.get('content_title')
parent_content_key = content_metadata_summary.get('content_key')

transaction = self._create_redemption(
lms_user_id,
content_key,
parent_content_key,
content_price,
subsidy_access_policy_uuid,
lms_user_email=lms_user_email,
Expand Down Expand Up @@ -476,6 +494,7 @@ def _create_redemption(
self,
lms_user_id,
content_key,
parent_content_key,
content_price,
subsidy_access_policy_uuid,
content_title=None,
Expand Down Expand Up @@ -531,6 +550,7 @@ def _create_redemption(
idempotency_key,
quantity,
content_key=content_key,
parent_content_key=parent_content_key,
content_title=content_title,
lms_user_id=lms_user_id,
lms_user_email=lms_user_email,
Expand Down
24 changes: 12 additions & 12 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ charset-normalizer==3.3.2
# via requests
click==8.1.7
# via edx-django-utils
cryptography==42.0.2
cryptography==42.0.4
# via
# pyjwt
# social-auth-core
defusedxml==0.8.0rc2
# via
# python3-openid
# social-auth-core
django==4.2.9
django==4.2.10
# via
# -c requirements/constraints.txt
# -r requirements/base.in
Expand Down Expand Up @@ -78,7 +78,7 @@ django-filter==23.5
# openedx-ledger
django-log-request-id==2.1.0
# via -r requirements/base.in
django-model-utils==4.3.1
django-model-utils==4.4.0
# via edx-rbac
django-object-actions==4.2.0
# via
Expand Down Expand Up @@ -123,8 +123,9 @@ edx-django-utils==5.10.1
# edx-drf-extensions
# edx-rest-api-client
# getsmarter-api-clients
# openedx-events
# openedx-ledger
edx-drf-extensions==10.1.0
edx-drf-extensions==10.2.0
# via
# -r requirements/base.in
# edx-rbac
Expand All @@ -138,7 +139,7 @@ edx-rbac==1.8.0
# openedx-ledger
edx-rest-api-client==5.6.1
# via -r requirements/base.in
fastavro==1.9.3
fastavro==1.9.4
# via openedx-events
getsmarter-api-clients==0.6.1
# via -r requirements/base.in
Expand All @@ -160,7 +161,7 @@ jsonschema==4.21.1
# via drf-spectacular
jsonschema-specifications==2023.12.1
# via jsonschema
mysqlclient==2.2.3
mysqlclient==2.2.4
# via
# -r requirements/base.in
# openedx-ledger
Expand All @@ -171,11 +172,11 @@ oauthlib==3.2.2
# getsmarter-api-clients
# requests-oauthlib
# social-auth-core
openedx-events==9.4.0
openedx-events==9.5.2
# via
# -r requirements/base.in
# openedx-ledger
openedx-ledger==1.3.2
openedx-ledger==1.4.0
# via -r requirements/base.in
packaging==23.2
# via drf-yasg
Expand All @@ -195,7 +196,6 @@ pyjwt[crypto]==2.8.0
# edx-auth-backends
# edx-drf-extensions
# edx-rest-api-client
# pyjwt
# social-auth-core
pymemcache==4.0.0
# via -r requirements/base.in
Expand Down Expand Up @@ -234,7 +234,7 @@ requests-oauthlib==1.3.1
# via
# getsmarter-api-clients
# social-auth-core
rpds-py==0.17.1
rpds-py==0.18.0
# via
# jsonschema
# referencing
Expand All @@ -253,7 +253,7 @@ slumber==0.7.1
# via edx-rest-api-client
social-auth-app-django==5.4.0
# via edx-auth-backends
social-auth-core==4.5.2
social-auth-core==4.5.3
# via
# edx-auth-backends
# social-auth-app-django
Expand All @@ -272,7 +272,7 @@ uritemplate==4.1.1
# via
# drf-spectacular
# drf-yasg
urllib3==2.2.0
urllib3==2.2.1
# via requests
zipp==3.17.0
# via importlib-resources
2 changes: 1 addition & 1 deletion requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tomli==2.0.1
# via
# pyproject-api
# tox
tox==4.12.1
tox==4.13.0
# via -r requirements/ci.in
virtualenv==20.25.0
# via tox
Loading

0 comments on commit a383b6a

Please sign in to comment.