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.

utils.py 3.9KB

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