Skip to content

Commit

Permalink
feat: retrieve content_price from normalized_metadata_by_run (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 authored Oct 8, 2024
1 parent e40e53f commit d4f448b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
12 changes: 11 additions & 1 deletion enterprise_subsidy/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,11 @@ class ContentMetadataViewSetTests(APITestBase):
],
"advertised_course_run_uuid": course_run_uuid,
"product_source": None,
"normalized_metadata_by_run": {
"edX+DemoX3+20230101": {
"content_price": 100.0
}
}
}
executive_education_course_metadata = {
"key": content_key_2,
Expand Down Expand Up @@ -1740,6 +1745,11 @@ class ContentMetadataViewSetTests(APITestBase):
}
],
"advertised_course_run_uuid": course_run_uuid,
"normalized_metadata_by_run": {
"edX+DemoX3+20230101": {
"content_price": 599.49
}
}
}
mock_http_error_reason = 'Something Went Wrong'
mock_http_error_url = 'foobar.com'
Expand All @@ -1751,7 +1761,7 @@ class ContentMetadataViewSetTests(APITestBase):
'expected_content_key': content_key_3,
'expected_course_run_uuid': str(course_run_uuid),
'expected_course_run_key': course_run_key,
'expected_content_price': 10000.0,
'expected_content_price': 10000,
'mock_metadata': edx_course_metadata_with_runs,
'expected_source': 'edX',
'expected_mode': 'verified',
Expand Down
28 changes: 23 additions & 5 deletions enterprise_subsidy/apps/content_metadata/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ def catalog_client(self):
"""
return EnterpriseCatalogApiClient()

def price_for_content(self, content_data, course_run_data):
def price_for_content_fallback(self, content_data, course_run_data):
"""
Helper to return the "official" price for content.
The endpoint at ``self.content_metadata_url`` will always return price fields
as USD (dollars), possibly as a string or a float. This method converts
those values to USD cents as an integer.
Fallback logic for `price_for_content` logic if the `normalized_metadata_by_run` field is None.
The fallback logic is the original logic for determining the `content_price` before
using normalized metadata as the first source of truth for `content_price`.
"""
content_price = None

Expand All @@ -79,6 +78,25 @@ def price_for_content(self, content_data, course_run_data):
)
content_price = DEFAULT_CONTENT_PRICE

return content_price

def price_for_content(self, content_data, course_run_data):
"""
Helper to return the "official" price for content.
The endpoint at ``self.content_metadata_url`` will always return price fields
as USD (dollars), possibly as a string or a float. This method converts
those values to USD cents as an integer.
"""
content_price = None
course_run_key = course_run_data.get('key')

if course_run_key in content_data.get('normalized_metadata_by_run', {}):
if normalized_price := content_data['normalized_metadata_by_run'][course_run_key].get('content_price'):
content_price = normalized_price

if not content_price:
content_price = self.price_for_content_fallback(content_data, course_run_data)

return int(Decimal(content_price) * CENTS_PER_DOLLAR)

def mode_for_content(self, content_data):
Expand Down
13 changes: 13 additions & 0 deletions enterprise_subsidy/apps/content_metadata/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def setUpTestData(cls):
'normalized_metadata': {
'enroll_by_date': '2023-05-26T15:45:32.494051Z',
},
"normalized_metadata_by_run": {
"course-v1:edX+DemoX+Demo_Course.1": {
"content_price": 149.0
}
}
}

cls.executive_education_course_metadata = {
Expand Down Expand Up @@ -139,6 +144,14 @@ def setUpTestData(cls):
"additional_metadata": {
"variant_id": cls.variant_id_2,
},
"normalized_metadata_by_run": {
"course-v1:edX+DemoX+Demo_Course.1": {
"content_price": 599.49
},
"course-v1:edX+DemoX+Demo_Course.2": {
"content_price": 599.49
}
}
}

@ddt.data(
Expand Down

0 comments on commit d4f448b

Please sign in to comment.