Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

fix: Embargo API fixed with new product type. #4034

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion ecommerce/extensions/basket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def prepare_basket(request, products, voucher=None):
_set_basket_bundle_status(bundle, basket)

if request.site.siteconfiguration.enable_embargo_check:
if not embargo_check(request.user, request.site, products):
if not embargo_check(request.user, request.site, products, headers=request.headers):
messages.error(
request,
_('Due to export controls, we cannot allow you to access this course at this time.')
Expand Down
18 changes: 12 additions & 6 deletions ecommerce/extensions/payment/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils.translation import ugettext_lazy as _
from oscar.core.loading import get_model

from ecommerce.core.constants import SEAT_PRODUCT_CLASS_NAME
from ecommerce.core.constants import SEAT_PRODUCT_CLASS_NAME, COURSE_ENTITLEMENT_PRODUCT_CLASS_NAME
from ecommerce.extensions.analytics.utils import parse_tracking_context

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -101,7 +101,7 @@ def clean_field_value(value):
return re.sub(r'[\^:"\']', '', value)


def embargo_check(user, site, products, ip=None):
def embargo_check(user, site, products, ip=None, headers={}):
""" Checks if the user has access to purchase products by calling the LMS embargo API.

Args:
Expand All @@ -118,21 +118,27 @@ def embargo_check(user, site, products, ip=None):
_, _, ip = parse_tracking_context(user, usage='embargo')

for product in products:
# We only are checking Seats
if product.get_product_class().name == SEAT_PRODUCT_CLASS_NAME:
product_class_name = product.get_product_class().name

# We are checking Seats & Course Entitlement
if product_class_name == SEAT_PRODUCT_CLASS_NAME or product_class_name == COURSE_ENTITLEMENT_PRODUCT_CLASS_NAME:
Copy link
Contributor

Choose a reason for hiding this comment

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

The last time when I reported this issue I tried using the same fix but it didn't work since for course entitlement products we didn't have course attribute set on them and hence the code would raise attribute errors as far as I remember. I would recommend double checking the code by debugging it locally and testing all the scenarios.

courses.append(product.course.id)

if courses:
params = {
'user': user,
'user': user.username,
'ip_address': ip,
'course_ids': courses
}

try:
api_client = site.siteconfiguration.oauth_api_client
api_url = urljoin(f"{site.siteconfiguration.embargo_api_url}/", "course_access/")
response = api_client.get(api_url, params=params).json()
response = api_client.get(
api_url,
params=params,
headers={**headers, 'accept': 'application/json'}
).json()
return response.get('access', True)
except: # pylint: disable=bare-except
# We are going to allow purchase if the API is un-reachable.
Expand Down
Loading