Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

availability.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from django.utils.translation import ugettext_lazy as _
  2. class Base(object):
  3. """
  4. Base availability policy.
  5. """
  6. #: Availability code. This is used for HTML classes
  7. code = ''
  8. #: A description of the availability of a product. This is shown on the
  9. #: product detail page. Eg "In stock", "Out of stock" etc
  10. message = ''
  11. #: When this item should be dispatched
  12. dispatch_date = None
  13. @property
  14. def short_message(self):
  15. """
  16. A shorter version of the availability message, suitable for showing on
  17. browsing pages.
  18. """
  19. return self.message
  20. @property
  21. def is_available_to_buy(self):
  22. """
  23. Test if this product is available to be bought. This is used for
  24. validation when a product is added to a user's basket.
  25. """
  26. # We test a purchase of a single item
  27. return self.is_purchase_permitted(1)[0]
  28. def is_purchase_permitted(self, quantity):
  29. """
  30. Test whether a proposed purchase is allowed
  31. Should return a boolean and a reason
  32. """
  33. return False, _("unavailable")
  34. # Common availability policies
  35. class Unavailable(Base):
  36. """
  37. Policy for when a product is unavailable
  38. """
  39. code = 'unavailable'
  40. message = _("Unavailable")
  41. class Available(Base):
  42. """
  43. For when a product is always available, irrespective of stock level.
  44. This might be appropriate for digital products where stock doesn't need to
  45. be tracked and the product is always available to buy.
  46. """
  47. code = 'available'
  48. message = _("Available")
  49. def is_purchase_permitted(self, quantity):
  50. return True, ""
  51. class StockRequired(Base):
  52. """
  53. Allow a product to be bought while there is stock. This policy is
  54. instantiated with a stock number (``num_available``). It ensures that the
  55. product is only available to buy while there is stock available.
  56. This is suitable for physical products where back orders (eg allowing
  57. purchases when there isn't stock available) are not permitted.
  58. """
  59. CODE_IN_STOCK = 'instock'
  60. CODE_OUT_OF_STOCK = 'outofstock'
  61. def __init__(self, num_available):
  62. self.num_available = num_available
  63. def is_purchase_permitted(self, quantity):
  64. if self.num_available == 0:
  65. return False, _("no stock available")
  66. if quantity > self.num_available:
  67. msg = _("a maximum of %(max)d can be bought") % {
  68. 'max': self.num_available}
  69. return False, msg
  70. return True, ""
  71. @property
  72. def code(self):
  73. if self.num_available > 0:
  74. return self.CODE_IN_STOCK
  75. return self.CODE_OUT_OF_STOCK
  76. @property
  77. def short_message(self):
  78. if self.num_available > 0:
  79. return _("In stock")
  80. return _("Unavailable")
  81. @property
  82. def message(self):
  83. if self.num_available > 0:
  84. return _("In stock (%d available)") % self.num_available
  85. return _("Unavailable")