Commit a78be806 by McKenzie Welter

cache basket_switch_data as product model property

parent c6c50375
......@@ -119,42 +119,6 @@ def prepare_basket(request, products, voucher=None):
return basket
def get_basket_switch_data(product):
structure = product.structure
switch_link_text = None
if product.is_enrollment_code_product:
switch_link_text = _('Click here to just purchase an enrollment for yourself')
structure = 'child'
elif product.is_seat_product:
switch_link_text = _('Click here to purchase multiple seats in this course')
structure = 'standalone'
stock_records = StockRecord.objects.filter(
product__course_id=product.course_id,
product__structure=structure
)
# 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
def attribute_cookie_data(basket, request):
try:
with transaction.atomic():
......
......@@ -27,7 +27,7 @@ from ecommerce.extensions.analytics.utils import (
track_segment_event,
translate_basket_line_for_segment
)
from ecommerce.extensions.basket.utils import add_utm_params_to_url, get_basket_switch_data, prepare_basket
from ecommerce.extensions.basket.utils import add_utm_params_to_url, prepare_basket
from ecommerce.extensions.offer.utils import format_benefit_value, render_email_confirmation_if_required
from ecommerce.extensions.order.exceptions import AlreadyPlacedOrderException
from ecommerce.extensions.partner.shortcuts import get_partner_for_site
......@@ -270,7 +270,7 @@ class BasketSummaryView(BasketView):
}
# Get variables for the switch link that toggles from enrollment codes and seat.
switch_link_text, partner_sku = get_basket_switch_data(line.product)
switch_link_text, partner_sku = line.product.basket_switch_data
if line.has_discount:
benefit = self.request.basket.applied_offers().values()[0].benefit
......
from django.db import models
from django.db.models.signals import post_init, post_save
from django.dispatch import receiver
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _
from oscar.apps.catalogue.abstract_models import AbstractProduct
......@@ -12,6 +13,8 @@ from ecommerce.core.constants import (
)
from ecommerce.core.utils import log_message_and_raise_validation_error
StockRecord = get_model('partner', 'StockRecord')
class Product(AbstractProduct):
course = models.ForeignKey(
......@@ -37,6 +40,42 @@ class Product(AbstractProduct):
def is_coupon_product(self):
return self.get_product_class().name == COUPON_PRODUCT_CLASS_NAME
@cached_property
def basket_switch_data(self):
structure = self.structure
switch_link_text = None
if self.is_enrollment_code_product:
switch_link_text = _('Click here to just purchase an enrollment for yourself')
structure = 'child'
elif self.is_seat_product:
switch_link_text = _('Click here to purchase multiple seats in this course')
structure = 'standalone'
stock_records = StockRecord.objects.filter(
product__course_id=self.course_id,
product__structure=structure
)
# 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(self.attr, 'certificate_type', None)
product_seat_type = getattr(self.attr, 'seat_type', None)
for stock_record in stock_records:
stock_record_cert_type = getattr(stock_record.self.attr, 'certificate_type', None)
stock_record_seat_type = getattr(stock_record.self.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
def save(self, *args, **kwargs):
try:
if not isinstance(self.attr.note, basestring) and self.attr.note is not None:
......
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