Last active
August 29, 2015 14:20
-
-
Save vovanbo/e91f1a0e2184486d1e6f to your computer and use it in GitHub Desktop.
Oscar's structured strategy with min/max prices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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