Skip to content

Instantly share code, notes, and snippets.

@vovanbo
Last active August 29, 2015 14:20
Show Gist options
  • Save vovanbo/e91f1a0e2184486d1e6f to your computer and use it in GitHub Desktop.
Save vovanbo/e91f1a0e2184486d1e6f to your computer and use it in GitHub Desktop.
Oscar's structured strategy with min/max prices
from collections import namedtuple
from operator import attrgetter
from oscar.apps.partner import prices
from oscar.apps.partner.strategy import Structured
# A container for policies
ExtendedPurchaseInfo = namedtuple(
'ExtendedPurchaseInfo', ['price',
'min_price',
'max_price',
'availability',
'stockrecord'])
class StructuredWithMinMax(Structured):
def fetch_for_product(self, product, stockrecord=None):
if stockrecord is None:
stockrecord = self.select_stockrecord(product)
price = self.pricing_policy(product, stockrecord)
min_price = max_price = prices.Unavailable()
return ExtendedPurchaseInfo(
price=price,
min_price=min_price,
max_price=max_price,
availability=self.availability_policy(product, stockrecord),
stockrecord=stockrecord)
def fetch_for_parent(self, product):
children_stock = self.select_children_stockrecords(product)
price = self.parent_pricing_policy(product, children_stock)
stockrecords = [x[1] for x in children_stock if x[1] is not None]
if stockrecords:
cheap_children_stockrecord = min(stockrecords, key=attrgetter('price_excl_tax'))
expensive_children_stockrecord = max(stockrecords, key=attrgetter('price_excl_tax'))
min_price = self.pricing_policy(product, cheap_children_stockrecord)
max_price = self.pricing_policy(product, expensive_children_stockrecord)
else:
min_price = max_price = prices.Unavailable()
return ExtendedPurchaseInfo(
price=price,
min_price=min_price,
max_price=max_price,
availability=self.parent_availability_policy(product, children_stock),
stockrecord=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment