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.

context_processors.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from itertools import chain
  2. from oscar.apps.promotions.models import PagePromotion, KeywordPromotion
  3. def promotions(request):
  4. """
  5. For adding bindings for banners and pods to the template
  6. context.
  7. """
  8. promotions = get_request_promotions(request)
  9. # Split the promotions into separate lists for each position, and
  10. # add them to the template bindings
  11. context = {
  12. 'url_path': request.path
  13. }
  14. split_by_position(promotions, context)
  15. return context
  16. def get_request_promotions(request):
  17. """
  18. Return promotions relevant to this request
  19. """
  20. promotions = PagePromotion._default_manager.select_related() \
  21. .prefetch_related('content_object') \
  22. .filter(page_url=request.path) \
  23. .order_by('display_order')
  24. if 'q' in request.GET:
  25. keyword_promotions = KeywordPromotion._default_manager.select_related().filter(keyword=request.GET['q'])
  26. if keyword_promotions.count() > 0:
  27. promotions = list(chain(promotions, keyword_promotions))
  28. return promotions
  29. def split_by_position(linked_promotions, context):
  30. """
  31. Split the list of promotions into separate lists, grouping
  32. by position, and write these lists to the context dict.
  33. """
  34. for linked_promotion in linked_promotions:
  35. promotion = linked_promotion.content_object
  36. if not promotion:
  37. continue
  38. key = 'promotions_%s' % linked_promotion.position.lower()
  39. if key not in context:
  40. context[key] = []
  41. context[key].append(promotion)