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 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import datetime
  2. from decimal import Decimal as D
  3. from django.utils.translation import ugettext_lazy as _
  4. class DefaultWrapper(object):
  5. """
  6. Default stockrecord wrapper
  7. """
  8. def is_available_to_buy(self, stockrecord):
  9. """
  10. Test whether a product is available to buy.
  11. This is used to determine whether to show the add-to-basket button.
  12. """
  13. if stockrecord.num_in_stock is None:
  14. return True
  15. return stockrecord.net_stock_level > 0
  16. def is_purchase_permitted(self, stockrecord, user=None, quantity=1):
  17. """
  18. Test whether a particular purchase is possible (is a user buying a given
  19. quantity of the product)
  20. """
  21. if not self.is_available_to_buy(stockrecord):
  22. return False, _("'%s' is not available to purchase" % stockrecord.product.title)
  23. max_qty = self.max_purchase_quantity(stockrecord, user)
  24. if max_qty is None:
  25. return True, None
  26. if max_qty < quantity:
  27. return False, _("'%(title)s' - A maximum of %(max)d can be bought" %
  28. {'title': stockrecord.product.title,
  29. 'max': max_qty})
  30. return True, None
  31. def max_purchase_quantity(self, stockrecord, user=None):
  32. """
  33. Return the maximum available purchase quantity for a given user
  34. """
  35. if stockrecord.num_in_stock is None:
  36. return None
  37. return stockrecord.net_stock_level
  38. def availability_code(self, stockrecord):
  39. """
  40. Return a code for the availability of this product.
  41. This is normally used within CSS to add icons to stock messages
  42. :param oscar.apps.partner.models.StockRecord stockrecord: stockrecord instance
  43. """
  44. if stockrecord.net_stock_level > 0:
  45. return 'instock'
  46. if self.is_available_to_buy(stockrecord):
  47. return 'available'
  48. return 'outofstock'
  49. def availability(self, stockrecord):
  50. """
  51. Return an availability message for the passed stockrecord.
  52. :param oscar.apps.partner.models.StockRecord stockrecord: stockrecord instance
  53. """
  54. if stockrecord.net_stock_level > 0:
  55. return _("In stock (%d available)" % stockrecord.net_stock_level)
  56. if self.is_available_to_buy(stockrecord):
  57. return _('Available')
  58. return _("Not available")
  59. def dispatch_date(self, stockrecord):
  60. if stockrecord.net_stock_level:
  61. # Assume next day for in-stock items
  62. return datetime.date.today() + datetime.timedelta(days=1)
  63. # Assume one week for out-of-stock items
  64. return datetime.date.today() + datetime.timedelta(days=7)
  65. def lead_time(self, stockrecord):
  66. return 1
  67. def calculate_tax(self, stockrecord):
  68. return D('0.00')