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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. # Flush any existing discounts to make sure they are recalculated
  17. # correctly
  18. basket.reset_offer_applications()
  19. offers = self.get_offers(request, basket)
  20. self.apply_offers(basket, offers)
  21. def apply_offers(self, basket, offers):
  22. applications = results.OfferApplications()
  23. for offer in offers:
  24. num_applications = 0
  25. # Keep applying the offer until either
  26. # (a) We reach the max number of applications for the offer.
  27. # (b) The benefit can't be applied successfully.
  28. while num_applications < offer.get_max_applications(basket.owner):
  29. result = offer.apply_benefit(basket)
  30. num_applications += 1
  31. if not result.is_successful:
  32. break
  33. applications.add(offer, result)
  34. if result.is_final:
  35. break
  36. # Store this list of discounts with the basket so it can be
  37. # rendered in templates
  38. basket.offer_applications = applications
  39. def get_offers(self, request, basket):
  40. """
  41. Return all offers to apply to the basket.
  42. This method should be subclassed and extended to provide more
  43. sophisticated behaviour. For instance, you could load extra offers
  44. based on the session or the user type.
  45. """
  46. site_offers = self.get_site_offers()
  47. basket_offers = self.get_basket_offers(basket, request.user)
  48. user_offers = self.get_user_offers(request.user)
  49. session_offers = self.get_session_offers(request)
  50. return list(chain(
  51. session_offers, basket_offers, user_offers, site_offers))
  52. def get_site_offers(self):
  53. """
  54. Return site offers that are available to all users
  55. """
  56. date_based_offers = ConditionalOffer.active.filter(
  57. offer_type=ConditionalOffer.SITE,
  58. status=ConditionalOffer.OPEN)
  59. nondate_based_offers = ConditionalOffer.objects.filter(
  60. offer_type=ConditionalOffer.SITE,
  61. status=ConditionalOffer.OPEN,
  62. start_datetime=None, end_datetime=None)
  63. return list(chain(date_based_offers, nondate_based_offers))
  64. def get_basket_offers(self, basket, user):
  65. """
  66. Return basket-linked offers such as those associated with a voucher
  67. code
  68. """
  69. offers = []
  70. if not basket.id:
  71. return offers
  72. for voucher in basket.vouchers.all():
  73. if voucher.is_active() and voucher.is_available_to_user(user):
  74. basket_offers = voucher.offers.all()
  75. for offer in basket_offers:
  76. offer.set_voucher(voucher)
  77. offers = list(chain(offers, basket_offers))
  78. return offers
  79. def get_user_offers(self, user):
  80. """
  81. Returns offers linked to this particular user.
  82. Eg: student users might get 25% off
  83. """
  84. return []
  85. def get_session_offers(self, request):
  86. """
  87. Returns temporary offers linked to the current session.
  88. Eg: visitors coming from an affiliate site get a 10% discount
  89. """
  90. return []