You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wrappers.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from decimal import Decimal as D
  2. from django.utils.translation import ugettext_lazy as _
  3. class DefaultWrapper(object):
  4. """
  5. Default stockrecord wrapper
  6. """
  7. CODE_IN_STOCK = 'instock'
  8. CODE_AVAILABLE = 'available'
  9. CODE_UNAVAILABLE = 'outofstock'
  10. def is_available_to_buy(self, stockrecord):
  11. """
  12. Test whether a product is available to buy.
  13. This is used to determine whether to show the add-to-basket button.
  14. """
  15. if not stockrecord.product.product_class.track_stock:
  16. return True
  17. if stockrecord.num_in_stock is None:
  18. return True
  19. return stockrecord.net_stock_level > 0
  20. def is_purchase_permitted(self, stockrecord, user=None, quantity=1):
  21. """
  22. Test whether a particular purchase is possible (is a user buying a given
  23. quantity of the product)
  24. """
  25. if not self.is_available_to_buy(stockrecord):
  26. return False, _("'%s' is not available to purchase") % stockrecord.product.title
  27. max_qty = self.max_purchase_quantity(stockrecord, user)
  28. if max_qty is None:
  29. return True, None
  30. if max_qty < quantity:
  31. return False, _("'%(title)s' - A maximum of %(max)d can be bought" %
  32. {'title': stockrecord.product.title,
  33. 'max': max_qty})
  34. return True, None
  35. def max_purchase_quantity(self, stockrecord, user=None):
  36. """
  37. Return the maximum available purchase quantity for a given user
  38. """
  39. if not stockrecord.product.product_class.track_stock:
  40. return None
  41. if stockrecord.num_in_stock is None:
  42. return None
  43. return stockrecord.net_stock_level
  44. def availability_code(self, stockrecord):
  45. """
  46. Return a code for the availability of this product.
  47. This is normally used within CSS to add icons to stock messages
  48. :param oscar.apps.partner.models.StockRecord stockrecord: stockrecord instance
  49. """
  50. if stockrecord.net_stock_level > 0:
  51. return self.CODE_IN_STOCK
  52. if self.is_available_to_buy(stockrecord):
  53. return self.CODE_AVAILABLE
  54. return self.CODE_UNAVAILABLE
  55. def availability(self, stockrecord):
  56. """
  57. Return an availability message for the passed stockrecord.
  58. :param oscar.apps.partner.models.StockRecord stockrecord: stockrecord instance
  59. """
  60. if stockrecord.net_stock_level > 0:
  61. return _("In stock (%d available)") % stockrecord.net_stock_level
  62. if self.is_available_to_buy(stockrecord):
  63. return _('Available')
  64. return _("Not available")
  65. def dispatch_date(self, stockrecord):
  66. """
  67. We don't provide a default value as it could be confusing. Subclass
  68. and override this method to provide estimated dispatch dates
  69. """
  70. return None
  71. def lead_time(self, stockrecord):
  72. """
  73. We don't provide a default value as it could be confusing. Subclass
  74. and override this method to provide estimated dispatch dates
  75. """
  76. return None
  77. def calculate_tax(self, stockrecord):
  78. return D('0.00')