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 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import oscar
  2. import re
  3. import platform
  4. import django
  5. from django.utils.six.moves.urllib import parse
  6. from django.conf import settings
  7. from django.utils.safestring import mark_safe
  8. def strip_language_code(request):
  9. """
  10. When using Django's i18n_patterns, we need a language-neutral variant of
  11. the current URL to be able to use set_language to change languages.
  12. This naive approach strips the language code from the beginning of the URL
  13. and will likely fail if using translated URLs.
  14. """
  15. path = request.path
  16. if settings.USE_I18N and hasattr(request, 'LANGUAGE_CODE'):
  17. return re.sub('^/%s/' % request.LANGUAGE_CODE, '/', path)
  18. return path
  19. def usage_statistics_string():
  20. """
  21. For Oscar development, it is helpful to know which versions of Oscar,
  22. Django and Python are in use, and which can be safely deprecated or
  23. removed. If tracking is enabled, this function builds a query string with
  24. that information. It is used in dashboard/layout.html with an invisible
  25. tracker pixel.
  26. If you're developing locally or tracking is disabled, the tracker pixel
  27. does not get rendered and no information is collected.
  28. """
  29. if not settings.DEBUG and getattr(settings, 'OSCAR_TRACKING', True):
  30. params = {
  31. 'django': django.get_version(),
  32. 'python': platform.python_version(),
  33. 'oscar': oscar.get_version(),
  34. }
  35. return mark_safe(parse.urlencode(params))
  36. else:
  37. return None
  38. def metadata(request):
  39. """
  40. Add some generally useful metadata to the template context
  41. """
  42. return {'display_version': getattr(settings, 'DISPLAY_VERSION', False),
  43. 'version': getattr(settings, 'VERSION', 'N/A'),
  44. 'shop_name': settings.OSCAR_SHOP_NAME,
  45. 'shop_tagline': settings.OSCAR_SHOP_TAGLINE,
  46. 'homepage_url': settings.OSCAR_HOMEPAGE,
  47. 'use_less': getattr(settings, 'USE_LESS', False),
  48. 'call_home': usage_statistics_string(),
  49. 'language_neutral_url_path': strip_language_code(request),
  50. 'google_analytics_id': getattr(settings,
  51. 'GOOGLE_ANALYTICS_ID', None)}