Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use "price" in ecommerce data loader (#4450) #4459

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions course_discovery/apps/course_metadata/data_loaders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def update_seats(self, body):
def update_seat(self, course_run, product_body):
stock_record = product_body['stockrecords'][0]
currency_code = stock_record['price_currency']
price = Decimal(stock_record['price_excl_tax'])
price = Decimal(stock_record.get('price_excl_tax', stock_record['price']))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of repeating this code, a util function can be added that return the price based on which argument is available. Furthermore, the docstring should explain why is there a need to check 2 keys as it wont be obvious to first time readers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DawoudSheraz Hi, sorry, I seem to have missed this comment. Ideally we should have only on them here because, the change was introduced by the upgrade of Django Oscar, which has then been rolled back on specific branches. So, in the future it will probably get simplified once there is a consistency in the Django Oscar version used. Extracting out a dedicated utility function for a transient change seems a bit superfluous to me. If you noticed, the PR also updates the test cases to match the updated price instead of price_excl_tax.

I agree that we can enhance the docstring or even an inline comment to make this more obvious.

sku = stock_record['partner_sku']

# For more context see ADR docs/decisions/0025-dont-sync-mobile-skus-on-discovery.rst
Expand Down Expand Up @@ -644,9 +644,11 @@ def update_seat(self, course_run, product_body):
def validate_stockrecord(self, stockrecords, title, product_class):
"""
Argument:
body (dict): product data from ecommerce, either entitlement or enrollment code
sockrecords (list): a list of stock records to validate from ecommerce
title (str): product title
product_class (str): either entitlement or enrollment code
Returns:
product sku if no exceptions, else None
True when all validation checks pass, else None
"""
# Map product_class keys with how they should be displayed in the exception messages.
product_classes = {
Expand Down Expand Up @@ -681,7 +683,7 @@ def validate_stockrecord(self, stockrecords, title, product_class):

try:
currency_code = stock_record['price_currency']
Decimal(stock_record['price_excl_tax'])
Decimal(stock_record.get('price_excl_tax', stock_record['price']))
sku = stock_record['partner_sku']
except (KeyError, ValueError):
msg = 'A necessary stockrecord field is missing or incorrectly set for {product} {title}'.format(
Expand Down Expand Up @@ -720,7 +722,7 @@ def update_entitlement(self, body):

stock_record = stockrecords[0]
currency_code = stock_record['price_currency']
price = Decimal(stock_record['price_excl_tax'])
price = Decimal(stock_record.get('price_excl_tax', stock_record['price']))
sku = stock_record['partner_sku']

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"stockrecords": [
{
"price_currency": "USD",
"price_excl_tax": "0.00",
"price": "0.00",
"partner_sku": "sku001",
}
]
Expand Down Expand Up @@ -225,7 +225,7 @@
"stockrecords": [
{
"price_currency": "EUR",
"price_excl_tax": "0.00",
"price": "0.00",
"partner_sku": "sku002",
}
]
Expand All @@ -242,7 +242,7 @@
"stockrecords": [
{
"price_currency": "EUR",
"price_excl_tax": "25.00",
"price": "25.00",
"partner_sku": "sku003",
}
]
Expand All @@ -260,7 +260,7 @@
"stockrecords": [
{
"price_currency": "EUR",
"price_excl_tax": "250.00",
"price": "250.00",
"partner_sku": "mobile.android.sku003",
}
]
Expand All @@ -277,7 +277,7 @@
"stockrecords": [
{
"price_currency": "EUR",
"price_excl_tax": "25.00",
"price": "25.00",
"partner_sku": "sku004"
}
]
Expand All @@ -304,7 +304,7 @@
"stockrecords": [
{
"price_currency": "USD",
"price_excl_tax": "0.00",
"price": "0.00",
"partner_sku": "sku005",
}
]
Expand All @@ -321,7 +321,7 @@
"stockrecords": [
{
"price_currency": "USD",
"price_excl_tax": "25.00",
"price": "25.00",
"partner_sku": "sku006",
}
]
Expand Down Expand Up @@ -350,7 +350,7 @@
"stockrecords": [
{
"price_currency": "USD",
"price_excl_tax": "250.00",
"price": "250.00",
"partner_sku": "sku007",
}
]
Expand Down Expand Up @@ -379,7 +379,7 @@
"stockrecords": [
{
"price_currency": "USD",
"price_excl_tax": "250.00",
"price": "250.00",
"partner_sku": "sku008",
}
]
Expand All @@ -404,7 +404,7 @@
"stockrecords": [
{
"price_currency": "123",
"price_excl_tax": "0.00",
"price": "0.00",
"partner_sku": "sku009",
}
]
Expand All @@ -429,7 +429,7 @@
"stockrecords": [
{
"price_currency": "USD",
"price_excl_tax": "0.00",
"price": "0.00",
"partner_sku": "sku010",
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def mock_products_api(self, alt_course=None, alt_currency=None, alt_mode=None, h

stockrecord = {
"price_currency": alt_currency if alt_currency else "USD",
"price_excl_tax": "10.00",
"price": "10.00",
}
if valid_stockrecord:
stockrecord.update({"partner_sku": "sku132"})
Expand Down Expand Up @@ -856,7 +856,7 @@ def assert_seats_loaded(self, body, mock_products):
for product in products:
stock_record = product['stockrecords'][0]
price_currency = stock_record['price_currency']
price = Decimal(stock_record['price_excl_tax'])
price = Decimal(stock_record['price'])
sku = stock_record['partner_sku']
certificate_type = Seat.AUDIT
credit_provider = None
Expand Down Expand Up @@ -908,7 +908,7 @@ def assert_entitlements_loaded(self, body):
course = Course.objects.get(uuid=attributes['UUID'])
stock_record = datum['stockrecords'][0]
price_currency = stock_record['price_currency']
price = Decimal(stock_record['price_excl_tax'])
price = Decimal(stock_record['price'])
sku = stock_record['partner_sku']

mode_name = attributes['certificate_type']
Expand Down
Loading