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

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