Commit 2e666819 by Matt Drayer

mattdrayer/WL-511: Repair invalid SKU publishing/selection workflows

parent f5508179
...@@ -9,6 +9,7 @@ from edx_rest_api_client.exceptions import SlumberHttpBaseException ...@@ -9,6 +9,7 @@ from edx_rest_api_client.exceptions import SlumberHttpBaseException
from oscar.core.loading import get_model from oscar.core.loading import get_model
import requests import requests
from ecommerce.core.constants import ENROLLMENT_CODE_SEAT_TYPES
from ecommerce.core.url_utils import get_lms_url, get_lms_commerce_api_url from ecommerce.core.url_utils import get_lms_url, get_lms_commerce_api_url
from ecommerce.courses.utils import mode_for_seat from ecommerce.courses.utils import mode_for_seat
...@@ -32,11 +33,13 @@ class LMSPublisher(object): ...@@ -32,11 +33,13 @@ class LMSPublisher(object):
def serialize_seat_for_commerce_api(self, seat): def serialize_seat_for_commerce_api(self, seat):
""" Serializes a course seat product to a dict that can be further serialized to JSON. """ """ Serializes a course seat product to a dict that can be further serialized to JSON. """
stock_record = seat.stockrecords.first() stock_record = seat.stockrecords.first()
try: bulk_sku = None
enrollment_code = seat.course.enrollment_code_product if seat.attr.certificate_type in ENROLLMENT_CODE_SEAT_TYPES:
bulk_sku = enrollment_code.stockrecords.first().partner_sku try:
except Product.DoesNotExist: enrollment_code = seat.course.enrollment_code_product
bulk_sku = None bulk_sku = enrollment_code.stockrecords.first().partner_sku
except Product.DoesNotExist:
pass
return { return {
'name': mode_for_seat(seat), 'name': mode_for_seat(seat),
'currency': stock_record.price_currency, 'currency': stock_record.price_currency,
......
...@@ -23,6 +23,7 @@ from ecommerce.core.url_utils import get_lms_enrollment_api_url ...@@ -23,6 +23,7 @@ from ecommerce.core.url_utils import get_lms_enrollment_api_url
from ecommerce.core.url_utils import get_lms_url from ecommerce.core.url_utils import get_lms_url
from ecommerce.coupons.tests.mixins import CouponMixin from ecommerce.coupons.tests.mixins import CouponMixin
from ecommerce.courses.tests.factories import CourseFactory from ecommerce.courses.tests.factories import CourseFactory
from ecommerce.extensions.basket.utils import get_basket_switch_data
from ecommerce.extensions.catalogue.tests.mixins import CourseCatalogTestMixin from ecommerce.extensions.catalogue.tests.mixins import CourseCatalogTestMixin
from ecommerce.extensions.offer.utils import format_benefit_value from ecommerce.extensions.offer.utils import format_benefit_value
from ecommerce.extensions.payment.tests.processors import DummyProcessor from ecommerce.extensions.payment.tests.processors import DummyProcessor
...@@ -291,6 +292,20 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix ...@@ -291,6 +292,20 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
line_data = response.context['formset_lines_data'][0][1] line_data = response.context['formset_lines_data'][0][1]
self.assertEqual(line_data['seat_type'], _(enrollment_code.attr.seat_type.capitalize())) self.assertEqual(line_data['seat_type'], _(enrollment_code.attr.seat_type.capitalize()))
def test_basket_switch_data(self):
"""Verify the correct basket switch data (single vs. multi quantity) is retrieved."""
course = CourseFactory()
toggle_switch(ENROLLMENT_CODE_SWITCH, True)
course.create_or_update_seat('invalid', False, 10, self.partner)
seat = course.create_or_update_seat('verified', False, 10, self.partner)
seat_sku = StockRecord.objects.get(product=seat).partner_sku
enrollment_code = Product.objects.get(product_class__name=ENROLLMENT_CODE_PRODUCT_CLASS_NAME)
ec_sku = StockRecord.objects.get(product=enrollment_code).partner_sku
__, partner_sku = get_basket_switch_data(seat)
self.assertEqual(partner_sku, ec_sku)
__, partner_sku = get_basket_switch_data(enrollment_code)
self.assertEqual(partner_sku, seat_sku)
@ddt.data( @ddt.data(
(Benefit.PERCENTAGE, 100), (Benefit.PERCENTAGE, 100),
(Benefit.PERCENTAGE, 50), (Benefit.PERCENTAGE, 50),
......
...@@ -77,11 +77,26 @@ def get_basket_switch_data(product): ...@@ -77,11 +77,26 @@ def get_basket_switch_data(product):
switch_link_text = _('Click here to purchase multiple seats in this course') switch_link_text = _('Click here to purchase multiple seats in this course')
structure = 'standalone' structure = 'standalone'
try: stock_records = StockRecord.objects.filter(
partner_sku = StockRecord.objects.get( product__course_id=product.course_id,
product__course_id=product.course_id, product__structure=structure
product__structure=structure).partner_sku )
except StockRecord.DoesNotExist:
partner_sku = None # Determine the proper partner SKU to embed in the single/multiple basket switch link
# The logic here is a little confusing. "Seat" products have "certificate_type" attributes, and
# "Enrollment Code" products have "seat_type" attributes. If the basket is in single-purchase
# mode, we are working with a Seat product and must present the 'buy multiple' switch link and
# SKU from the corresponding Enrollment Code product. If the basket is in multi-purchase mode,
# we are working with an Enrollment Code product and must present the 'buy single' switch link
# and SKU from the corresponding Seat product.
partner_sku = None
product_cert_type = getattr(product.attr, 'certificate_type', None)
product_seat_type = getattr(product.attr, 'seat_type', None)
for stock_record in stock_records:
stock_record_cert_type = getattr(stock_record.product.attr, 'certificate_type', None)
stock_record_seat_type = getattr(stock_record.product.attr, 'seat_type', None)
if (product_seat_type and product_seat_type == stock_record_cert_type) or \
(product_cert_type and product_cert_type == stock_record_seat_type):
partner_sku = stock_record.partner_sku
break
return switch_link_text, partner_sku return switch_link_text, partner_sku
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment