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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from itertools import chain
  2. from django.core.exceptions import ObjectDoesNotExist
  3. from oscar.apps.promotions.models import PagePromotion
  4. def promotions(request):
  5. """
  6. For adding bindings for banners and pods to the template
  7. context.
  8. """
  9. # @todo need caching here / and order bt
  10. promotions = PagePromotion._default_manager.select_related().filter(page_url=request.path)
  11. # if 'q' in request.GET:
  12. # keyword_promotions = KeywordPromotion._default_manager.select_related().filter(keyword=request.GET['q'])
  13. # if keyword_promotions.count() > 0:
  14. # promotions = list(chain(promotions, keyword_promotions))
  15. context = {
  16. 'url_path': request.path
  17. }
  18. # Split the promotions into separate lists for each position, and
  19. # add them to the template bindings
  20. _split_by_position(promotions, context)
  21. return context
  22. def _split_by_position(linked_promotions, context):
  23. """
  24. Split the list of promotions into separate lists, grouping
  25. by position, and write these lists to the context dict.
  26. """
  27. for linked_promotion in linked_promotions:
  28. key = 'promotions_%s' % linked_promotion.position.lower()
  29. if key not in context:
  30. context[key] = []
  31. context[key].append(linked_promotion.content_object)
  32. linked_promotion.content_object.set_proxy_link(linked_promotion.get_link())