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

repository.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from decimal import Decimal as D
  2. from django.core.exceptions import ImproperlyConfigured
  3. from django.utils.translation import ugettext_lazy as _
  4. from oscar.apps.shipping import methods
  5. class Repository(object):
  6. """
  7. Repository class responsible for returning ShippingMethod
  8. objects for a given user, basket etc
  9. """
  10. methods = (methods.Free(),)
  11. def get_shipping_methods(self, user, basket, shipping_addr=None,
  12. request=None, **kwargs):
  13. """
  14. Return a list of all applicable shipping method objects
  15. for a given basket, address etc.
  16. We default to returning the ``Method`` models that have been defined
  17. but this behaviour can easily be overridden by subclassing this class
  18. and overriding this method.
  19. """
  20. return self.prime_methods(basket, self.methods)
  21. def get_default_shipping_method(self, user, basket, shipping_addr=None,
  22. request=None, **kwargs):
  23. """
  24. Return a 'default' shipping method to show on the basket page to give
  25. the customer an indication of what their order will cost.
  26. """
  27. shipping_methods = self.get_shipping_methods(
  28. user, basket, shipping_addr, request, **kwargs)
  29. if len(shipping_methods) == 0:
  30. raise ImproperlyConfigured(
  31. _("You need to define some shipping methods"))
  32. # Choose the cheapest method by default
  33. return min(shipping_methods, key=lambda method: method.charge_excl_tax)
  34. def prime_methods(self, basket, methods):
  35. """
  36. Prime a list of shipping method instances
  37. This involves injecting the basket instance into each and adding any
  38. discount wrappers.
  39. """
  40. return [self.prime_method(basket, method) for
  41. method in methods]
  42. def prime_method(self, basket, method):
  43. """
  44. Prime an individual method instance
  45. """
  46. method.set_basket(basket)
  47. # If the basket has a shipping offer, wrap the shipping method with a
  48. # decorating class that applies the offer discount to the shipping
  49. # charge.
  50. if basket.offer_applications.shipping_discounts:
  51. # We assume there is only one shipping discount available
  52. discount = basket.offer_applications.shipping_discounts[0]
  53. if method.charge_incl_tax > D('0.00'):
  54. return methods.OfferDiscount(method, discount['offer'])
  55. return method
  56. def find_by_code(self, code, basket):
  57. """
  58. Return the appropriate Method object for the given code
  59. """
  60. for method in self.methods:
  61. if method.code == code:
  62. return self.prime_method(basket, method)
  63. # Check for NoShippingRequired as that is a special case
  64. if code == methods.NoShippingRequired.code:
  65. return self.prime_method(basket, methods.NoShippingRequired())