Source code for oscar.apps.partner.strategy

fromcollectionsimportnamedtuplefromdecimalimportDecimalasDfromoscar.core.loadingimportget_classUnavailable=get_class('partner.availability','Unavailable')Available=get_class('partner.availability','Available')StockRequiredAvailability=get_class('partner.availability','StockRequired')UnavailablePrice=get_class('partner.prices','Unavailable')FixedPrice=get_class('partner.prices','FixedPrice')TaxInclusiveFixedPrice=get_class('partner.prices','TaxInclusiveFixedPrice')# A container for policiesPurchaseInfo=namedtuple('PurchaseInfo',['price','availability','stockrecord'])

[docs]classSelector(object):""" Responsible for returning the appropriate strategy class for a given user/session. This can be called in three ways: #) Passing a request and user. This is for determining prices/availability for a normal user browsing the site. #) Passing just the user. This is for offline processes that don't have a request instance but do know which user to determine prices for. #) Passing nothing. This is for offline processes that don't correspond to a specific user. Eg, determining a price to store in a search index. """

[docs]classBase(object):""" The base strategy class Given a product, strategies are responsible for returning a ``PurchaseInfo`` instance which contains: - The appropriate stockrecord for this customer - A pricing policy instance - An availability policy instance """def__init__(self,request=None):self.request=requestself.user=Noneifrequestandrequest.user.is_authenticated:self.user=request.user

[docs]deffetch_for_product(self,product,stockrecord=None):""" Given a product, return a ``PurchaseInfo`` instance. The ``PurchaseInfo`` class is a named tuple with attributes: - ``price``: a pricing policy object. - ``availability``: an availability policy object. - ``stockrecord``: the stockrecord that is being used If a stockrecord is passed, return the appropriate ``PurchaseInfo`` instance for that product and stockrecord is returned. """raiseNotImplementedError("A strategy class must define a fetch_for_product method ""for returning the availability and pricing ""information.")

[docs]deffetch_for_line(self,line,stockrecord=None):""" Given a basket line instance, fetch a ``PurchaseInfo`` instance. This method is provided to allow purchase info to be determined using a basket line's attributes. For instance, "bundle" products often use basket line attributes to store SKUs of contained products. For such products, we need to look at the availability of each contained product to determine overall availability. """# Default to ignoring any basket line options as we don't know what to# do with them within Oscar - that's up to your project to implement.returnself.fetch_for_product(line.product)

[docs]deffetch_for_product(self,product,stockrecord=None):""" Return the appropriate ``PurchaseInfo`` instance. This method is not intended to be overridden. """ifstockrecordisNone:stockrecord=self.select_stockrecord(product)returnPurchaseInfo(price=self.pricing_policy(product,stockrecord),availability=self.availability_policy(product,stockrecord),stockrecord=stockrecord)

[docs]deffetch_for_parent(self,product):# Select children and associated stockrecordschildren_stock=self.select_children_stockrecords(product)returnPurchaseInfo(price=self.parent_pricing_policy(product,children_stock),availability=self.parent_availability_policy(product,children_stock),stockrecord=None)

[docs]defselect_children_stockrecords(self,product):""" Select appropriate stock record for all children of a product """records=[]forchildinproduct.children.all():# Use tuples of (child product, stockrecord)records.append((child,self.select_stockrecord(child)))returnrecords

# Mixins - these can be used to construct the appropriate strategy class

[docs]classUseFirstStockRecord(object):""" Stockrecord selection mixin for use with the ``Structured`` base strategy. This mixin picks the first (normally only) stockrecord to fulfil a product. This is backwards compatible with Oscar<0.6 where only one stockrecord per product was permitted. """defselect_stockrecord(self,product):try:returnproduct.stockrecords.all()[0]exceptIndexError:returnNone

[docs]classStockRequired(object):""" Availability policy mixin for use with the ``Structured`` base strategy. This mixin ensures that a product can only be bought if it has stock available (if stock is being tracked). """defavailability_policy(self,product,stockrecord):ifnotstockrecord:returnUnavailable()ifnotproduct.get_product_class().track_stock:returnAvailable()else:returnStockRequiredAvailability(stockrecord.net_stock_level)defparent_availability_policy(self,product,children_stock):# A parent product is available if one of its children isforchild,stockrecordinchildren_stock:policy=self.availability_policy(product,stockrecord)ifpolicy.is_available_to_buy:returnAvailable()returnUnavailable()

[docs]classNoTax(object):""" Pricing policy mixin for use with the ``Structured`` base strategy. This mixin specifies zero tax and uses the ``price_excl_tax`` from the stockrecord. """defpricing_policy(self,product,stockrecord):# Check stockrecord has the appropriate dataifnotstockrecordorstockrecord.price_excl_taxisNone:returnUnavailablePrice()returnFixedPrice(currency=stockrecord.price_currency,excl_tax=stockrecord.price_excl_tax,tax=D('0.00'))defparent_pricing_policy(self,product,children_stock):stockrecords=[x[1]forxinchildren_stockifx[1]isnotNone]ifnotstockrecords:returnUnavailablePrice()# We take price from first recordstockrecord=stockrecords[0]returnFixedPrice(currency=stockrecord.price_currency,excl_tax=stockrecord.price_excl_tax,tax=D('0.00'))

[docs]classFixedRateTax(object):""" Pricing policy mixin for use with the ``Structured`` base strategy. This mixin applies a fixed rate tax to the base price from the product's stockrecord. The price_incl_tax is quantized to two decimal places. Rounding behaviour is Decimal's default """rate=D('0')# Subclass and specify the correct rateexponent=D('0.01')# Default to two decimal placesdefpricing_policy(self,product,stockrecord):ifnotstockrecordorstockrecord.price_excl_taxisNone:returnUnavailablePrice()rate=self.get_rate(product,stockrecord)exponent=self.get_exponent(stockrecord)tax=(stockrecord.price_excl_tax*rate).quantize(exponent)returnTaxInclusiveFixedPrice(currency=stockrecord.price_currency,excl_tax=stockrecord.price_excl_tax,tax=tax)defparent_pricing_policy(self,product,children_stock):stockrecords=[x[1]forxinchildren_stockifx[1]isnotNone]ifnotstockrecords:returnUnavailablePrice()# We take price from first recordstockrecord=stockrecords[0]rate=self.get_rate(product,stockrecord)exponent=self.get_exponent(stockrecord)tax=(stockrecord.price_excl_tax*rate).quantize(exponent)returnFixedPrice(currency=stockrecord.price_currency,excl_tax=stockrecord.price_excl_tax,tax=tax)

[docs]defget_rate(self,product,stockrecord):""" This method serves as hook to be able to plug in support for varying tax rates based on the product. TODO: Needs tests. """returnself.rate

[docs]defget_exponent(self,stockrecord):""" This method serves as hook to be able to plug in support for a varying exponent based on the currency. TODO: Needs tests. """returnself.exponent

[docs]classDeferredTax(object):""" Pricing policy mixin for use with the ``Structured`` base strategy. This mixin does not specify the product tax and is suitable to territories where tax isn't known until late in the checkout process. """defpricing_policy(self,product,stockrecord):ifnotstockrecordorstockrecord.price_excl_taxisNone:returnUnavailablePrice()returnFixedPrice(currency=stockrecord.price_currency,excl_tax=stockrecord.price_excl_tax)defparent_pricing_policy(self,product,children_stock):stockrecords=[x[1]forxinchildren_stockifx[1]isnotNone]ifnotstockrecords:returnUnavailablePrice()# We take price from first recordstockrecord=stockrecords[0]returnFixedPrice(currency=stockrecord.price_currency,excl_tax=stockrecord.price_excl_tax)

[docs]classDefault(UseFirstStockRecord,StockRequired,NoTax,Structured):""" Default stock/price strategy that uses the first found stockrecord for a product, ensures that stock is available (unless the product class indicates that we don't need to track stock) and charges zero tax. """

[docs]classUK(UseFirstStockRecord,StockRequired,FixedRateTax,Structured):""" Sample strategy for the UK that: - uses the first stockrecord for each product (effectively assuming there is only one). - requires that a product has stock available to be bought - applies a fixed rate of tax on all products This is just a sample strategy used for internal development. It is not recommended to be used in production, especially as the tax rate is hard-coded. """# Use UK VAT rate (as of December 2013)rate=D('0.20')

[docs]classUS(UseFirstStockRecord,StockRequired,DeferredTax,Structured):""" Sample strategy for the US. - uses the first stockrecord for each product (effectively assuming there is only one). - requires that a product has stock available to be bought - doesn't apply a tax to product prices (normally this will be done after the shipping address is entered). This is just a sample one used for internal development. It is not recommended to be used in production. """