選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

strategy.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. from collections import namedtuple
  2. from decimal import Decimal as D
  3. from . import availability, prices
  4. # A container for policies
  5. from oscar.core.decorators import deprecated
  6. PurchaseInfo = namedtuple(
  7. 'PurchaseInfo', ['price', 'availability', 'stockrecord'])
  8. class Selector(object):
  9. """
  10. Responsible for returning the appropriate strategy class for a given
  11. user/session.
  12. This can be called in three ways:
  13. #) Passing a request and user. This is for determining
  14. prices/availability for a normal user browsing the site.
  15. #) Passing just the user. This is for offline processes that don't
  16. have a request instance but do know which user to determine prices for.
  17. #) Passing nothing. This is for offline processes that don't
  18. correspond to a specific user. Eg, determining a price to store in
  19. a search index.
  20. """
  21. def strategy(self, request=None, user=None, **kwargs):
  22. """
  23. Return an instanticated strategy instance
  24. """
  25. # Default to the backwards-compatible strategy of picking the first
  26. # stockrecord but charging zero tax.
  27. return Default(request)
  28. class Base(object):
  29. """
  30. The base strategy class
  31. Given a product, strategies are responsible for returning a
  32. ``PurchaseInfo`` instance which contains:
  33. - The appropriate stockrecord for this customer
  34. - A pricing policy instance
  35. - An availability policy instance
  36. """
  37. def __init__(self, request=None):
  38. self.request = request
  39. self.user = None
  40. if request and request.user.is_authenticated():
  41. self.user = request.user
  42. def fetch_for_product(self, product, stockrecord=None):
  43. """
  44. Given a product, return a ``PurchaseInfo`` instance.
  45. The ``PurchaseInfo`` class is a named tuple with attributes:
  46. - ``price``: a pricing policy object.
  47. - ``availability``: an availability policy object.
  48. - ``stockrecord``: the stockrecord that is being used
  49. If a stockrecord is passed, return the appropriate ``PurchaseInfo``
  50. instance for that product and stockrecord is returned.
  51. """
  52. raise NotImplementedError(
  53. "A strategy class must define a fetch_for_product method "
  54. "for returning the availability and pricing "
  55. "information."
  56. )
  57. def fetch_for_parent(self, product):
  58. """
  59. Given a parent product, fetch a ``StockInfo`` instance
  60. """
  61. raise NotImplementedError(
  62. "A strategy class must define a fetch_for_group method "
  63. "for returning the availability and pricing "
  64. "information."
  65. )
  66. def fetch_for_line(self, line, stockrecord=None):
  67. """
  68. Given a basket line instance, fetch a ``PurchaseInfo`` instance.
  69. This method is provided to allow purchase info to be determined using a
  70. basket line's attributes. For instance, "bundle" products often use
  71. basket line attributes to store SKUs of contained products. For such
  72. products, we need to look at the availability of each contained product
  73. to determine overall availability.
  74. """
  75. # Default to ignoring any basket line options as we don't know what to
  76. # do with them within Oscar - that's up to your project to implement.
  77. return self.fetch_for_product(line.product)
  78. class Structured(Base):
  79. """
  80. A strategy class which provides separate, overridable methods for
  81. determining the 3 things that a ``PurchaseInfo`` instance requires:
  82. #) A stockrecord
  83. #) A pricing policy
  84. #) An availability policy
  85. """
  86. def fetch_for_product(self, product, stockrecord=None):
  87. """
  88. Return the appropriate ``PurchaseInfo`` instance.
  89. This method is not intended to be overridden.
  90. """
  91. if stockrecord is None:
  92. stockrecord = self.select_stockrecord(product)
  93. return PurchaseInfo(
  94. price=self.pricing_policy(product, stockrecord),
  95. availability=self.availability_policy(product, stockrecord),
  96. stockrecord=stockrecord)
  97. def fetch_for_parent(self, product):
  98. # Select children and associated stockrecords
  99. children_stock = self.select_children_stockrecords(product)
  100. return PurchaseInfo(
  101. price=self.parent_pricing_policy(product, children_stock),
  102. availability=self.parent_availability_policy(
  103. product, children_stock),
  104. stockrecord=None)
  105. fetch_for_group = deprecated(fetch_for_parent)
  106. def select_stockrecord(self, product):
  107. """
  108. Select the appropriate stockrecord
  109. """
  110. raise NotImplementedError(
  111. "A structured strategy class must define a "
  112. "'select_stockrecord' method")
  113. def select_children_stockrecords(self, product):
  114. """
  115. Select appropriate stock record for all children of a product
  116. """
  117. records = []
  118. for child in product.children.all():
  119. # Use tuples of (child product, stockrecord)
  120. records.append((child, self.select_stockrecord(child)))
  121. return records
  122. select_variant_stockrecords = deprecated(select_children_stockrecords)
  123. def pricing_policy(self, product, stockrecord):
  124. """
  125. Return the appropriate pricing policy
  126. """
  127. raise NotImplementedError(
  128. "A structured strategy class must define a "
  129. "'pricing_policy' method")
  130. def parent_pricing_policy(self, product, children_stock):
  131. raise NotImplementedError(
  132. "A structured strategy class must define a "
  133. "'parent_pricing_policy' method")
  134. group_pricing_policy = deprecated(parent_pricing_policy)
  135. def availability_policy(self, product, stockrecord):
  136. """
  137. Return the appropriate availability policy
  138. """
  139. raise NotImplementedError(
  140. "A structured strategy class must define a "
  141. "'availability_policy' method")
  142. def parent_availability_policy(self, product, children_stock):
  143. raise NotImplementedError(
  144. "A structured strategy class must define a "
  145. "'parent_availability_policy' method")
  146. group_availability_policy = deprecated(parent_availability_policy)
  147. # Mixins - these can be used to construct the appropriate strategy class
  148. class UseFirstStockRecord(object):
  149. """
  150. Stockrecord selection mixin for use with the ``Structured`` base strategy.
  151. This mixin picks the first (normally only) stockrecord to fulfil a product.
  152. This is backwards compatible with Oscar<0.6 where only one stockrecord per
  153. product was permitted.
  154. """
  155. def select_stockrecord(self, product):
  156. try:
  157. return product.stockrecords.all()[0]
  158. except IndexError:
  159. return None
  160. class StockRequired(object):
  161. """
  162. Availability policy mixin for use with the ``Structured`` base strategy.
  163. This mixin ensures that a product can only be bought if it has stock
  164. available (if stock is being tracked).
  165. """
  166. def availability_policy(self, product, stockrecord):
  167. if not stockrecord:
  168. return availability.Unavailable()
  169. if not product.get_product_class().track_stock:
  170. return availability.Available()
  171. else:
  172. return availability.StockRequired(
  173. stockrecord.net_stock_level)
  174. def parent_availability_policy(self, product, children_stock):
  175. # A parent product is available if one of its children is
  176. for child, stockrecord in children_stock:
  177. policy = self.availability_policy(product, stockrecord)
  178. if policy.is_available_to_buy:
  179. return availability.Available()
  180. return availability.Unavailable()
  181. class NoTax(object):
  182. """
  183. Pricing policy mixin for use with the ``Structured`` base strategy.
  184. This mixin specifies zero tax and uses the ``price_excl_tax`` from the
  185. stockrecord.
  186. """
  187. def pricing_policy(self, product, stockrecord):
  188. # Check stockrecord has the appropriate data
  189. if not stockrecord or stockrecord.price_excl_tax is None:
  190. return prices.Unavailable()
  191. return prices.FixedPrice(
  192. currency=stockrecord.price_currency,
  193. excl_tax=stockrecord.price_excl_tax,
  194. tax=D('0.00'))
  195. def parent_pricing_policy(self, product, children_stock):
  196. stockrecords = [x[1] for x in children_stock if x[1] is not None]
  197. if not stockrecords:
  198. return prices.Unavailable()
  199. # We take price from first record
  200. stockrecord = stockrecords[0]
  201. return prices.FixedPrice(
  202. currency=stockrecord.price_currency,
  203. excl_tax=stockrecord.price_excl_tax,
  204. tax=D('0.00'))
  205. class FixedRateTax(object):
  206. """
  207. Pricing policy mixin for use with the ``Structured`` base strategy. This
  208. mixin applies a fixed rate tax to the base price from the product's
  209. stockrecord. The price_incl_tax is quantized to two decimal places.
  210. Rounding behaviour is Decimal's default
  211. """
  212. rate = D('0') # Subclass and specify the correct rate
  213. exponent = D('0.01') # Default to two decimal places
  214. def pricing_policy(self, product, stockrecord):
  215. if not stockrecord:
  216. return prices.Unavailable()
  217. tax = (stockrecord.price_excl_tax * self.rate).quantize(self.exponent)
  218. return prices.TaxInclusiveFixedPrice(
  219. currency=stockrecord.price_currency,
  220. excl_tax=stockrecord.price_excl_tax,
  221. tax=tax)
  222. def parent_pricing_policy(self, product, children_stock):
  223. stockrecords = [x[1] for x in children_stock if x[1] is not None]
  224. if not stockrecords:
  225. return prices.Unavailable()
  226. # We take price from first record
  227. stockrecord = stockrecords[0]
  228. tax = (stockrecord.price_excl_tax * self.rate).quantize(self.exponent)
  229. return prices.FixedPrice(
  230. currency=stockrecord.price_currency,
  231. excl_tax=stockrecord.price_excl_tax,
  232. tax=tax)
  233. class DeferredTax(object):
  234. """
  235. Pricing policy mixin for use with the ``Structured`` base strategy.
  236. This mixin does not specify the product tax and is suitable to territories
  237. where tax isn't known until late in the checkout process.
  238. """
  239. def pricing_policy(self, product, stockrecord):
  240. if not stockrecord:
  241. return prices.Unavailable()
  242. return prices.FixedPrice(
  243. currency=stockrecord.price_currency,
  244. excl_tax=stockrecord.price_excl_tax)
  245. def parent_pricing_policy(self, product, children_stock):
  246. stockrecords = [x[1] for x in children_stock if x[1] is not None]
  247. if not stockrecords:
  248. return prices.Unavailable()
  249. # We take price from first record
  250. stockrecord = stockrecords[0]
  251. return prices.FixedPrice(
  252. currency=stockrecord.price_currency,
  253. excl_tax=stockrecord.price_excl_tax)
  254. # Example strategy composed of above mixins. For real projects, it's likely
  255. # you'll want to use a different pricing mixin as you'll probably want to
  256. # charge tax!
  257. class Default(UseFirstStockRecord, StockRequired, NoTax, Structured):
  258. """
  259. Default stock/price strategy that uses the first found stockrecord for a
  260. product, ensures that stock is available (unless the product class
  261. indicates that we don't need to track stock) and charges zero tax.
  262. """
  263. class UK(UseFirstStockRecord, StockRequired, FixedRateTax, Structured):
  264. """
  265. Sample strategy for the UK that:
  266. - uses the first stockrecord for each product (effectively assuming
  267. there is only one).
  268. - requires that a product has stock available to be bought
  269. - applies a fixed rate of tax on all products
  270. This is just a sample strategy used for internal development. It is not
  271. recommended to be used in production, especially as the tax rate is
  272. hard-coded.
  273. """
  274. # Use UK VAT rate (as of December 2013)
  275. rate = D('0.20')
  276. class US(UseFirstStockRecord, StockRequired, DeferredTax, Structured):
  277. """
  278. Sample strategy for the US.
  279. - uses the first stockrecord for each product (effectively assuming
  280. there is only one).
  281. - requires that a product has stock available to be bought
  282. - doesn't apply a tax to product prices (normally this will be done
  283. after the shipping address is entered).
  284. This is just a sample one used for internal development. It is not
  285. recommended to be used in production.
  286. """