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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from itertools import chain
  2. import logging
  3. from django.db.models import get_model
  4. from oscar.apps.offer import results
  5. ConditionalOffer = get_model('offer', 'ConditionalOffer')
  6. logger = logging.getLogger('oscar.offers')
  7. class OfferApplicationError(Exception):
  8. pass
  9. class Applicator(object):
  10. def apply(self, request, basket):
  11. """
  12. Apply all relevant offers to the given basket.
  13. The request is passed too as sometimes the available offers
  14. are dependent on the user (eg session-based offers).
  15. """
  16. offers = self.get_offers(request, basket)
  17. self.apply_offers(basket, offers)
  18. def apply_offers(self, basket, offers):
  19. applications = results.OfferApplications()
  20. for offer in offers:
  21. num_applications = 0
  22. # Keep applying the offer until either
  23. # (a) We reach the max number of applications for the offer.
  24. # (b) The benefit can't be applied successfully.
  25. while num_applications < offer.get_max_applications(basket.owner):
  26. result = offer.apply_benefit(basket)
  27. num_applications += 1
  28. if not result.is_successful:
  29. break
  30. applications.add(offer, result)
  31. if result.is_final:
  32. break
  33. # Store this list of discounts with the basket so it can be
  34. # rendered in templates
  35. basket.offer_applications = applications
  36. def get_offers(self, request, basket):
  37. """
  38. Return all offers to apply to the basket.
  39. This method should be subclassed and extended to provide more
  40. sophisticated behaviour. For instance, you could load extra offers
  41. based on the session or the user type.
  42. """
  43. site_offers = self.get_site_offers()
  44. basket_offers = self.get_basket_offers(basket, request.user)
  45. user_offers = self.get_user_offers(request.user)
  46. session_offers = self.get_session_offers(request)
  47. return list(chain(
  48. session_offers, basket_offers, user_offers, site_offers))
  49. def get_site_offers(self):
  50. """
  51. Return site offers that are available to all users
  52. """
  53. # Using select_related with the condition/benefit ranges doesn't seem
  54. # to work. I think this is because both the related objects have the
  55. # FK to range with the same name.
  56. date_based_offers = ConditionalOffer.active.select_related(
  57. 'condition', 'condition__range', 'benefit').filter(
  58. offer_type=ConditionalOffer.SITE,
  59. status=ConditionalOffer.OPEN)
  60. nondate_based_offers = ConditionalOffer.objects.select_related(
  61. 'condition', 'condition__range', 'benefit').filter(
  62. offer_type=ConditionalOffer.SITE,
  63. status=ConditionalOffer.OPEN,
  64. start_datetime=None, end_datetime=None)
  65. return list(chain(date_based_offers, nondate_based_offers))
  66. def get_basket_offers(self, basket, user):
  67. """
  68. Return basket-linked offers such as those associated with a voucher
  69. code
  70. """
  71. offers = []
  72. if not basket.id:
  73. return offers
  74. for voucher in basket.vouchers.all():
  75. if voucher.is_active() and voucher.is_available_to_user(user):
  76. basket_offers = voucher.offers.all()
  77. for offer in basket_offers:
  78. offer.set_voucher(voucher)
  79. offers = list(chain(offers, basket_offers))
  80. return offers
  81. def get_user_offers(self, user):
  82. """
  83. Returns offers linked to this particular user.
  84. Eg: student users might get 25% off
  85. """
  86. return []
  87. def get_session_offers(self, request):
  88. """
  89. Returns temporary offers linked to the current session.
  90. Eg: visitors coming from an affiliate site get a 10% discount
  91. """
  92. return []