/packages/Django/django/core/context_processors.py

https://github.com/lmorchard/home-snippets-server-lib · Python · 104 lines · 85 code · 3 blank · 16 comment · 0 complexity · 1116a68f7a8f5d8a6cfcf6774f8c6489 MD5 · raw file

  1. """
  2. A set of request processors that return dictionaries to be merged into a
  3. template context. Each function takes the request object as its only parameter
  4. and returns a dictionary to add to the context.
  5. These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by
  6. RequestContext.
  7. """
  8. from django.conf import settings
  9. from django.middleware.csrf import get_token
  10. from django.utils.functional import lazy
  11. def auth(request):
  12. """
  13. DEPRECATED. This context processor is the old location, and has been moved
  14. to `django.contrib.auth.context_processors`.
  15. This function still exists for backwards-compatibility; it will be removed
  16. in Django 1.4.
  17. """
  18. import warnings
  19. warnings.warn(
  20. "The context processor at `django.core.context_processors.auth` is " \
  21. "deprecated; use the path `django.contrib.auth.context_processors.auth` " \
  22. "instead.",
  23. PendingDeprecationWarning
  24. )
  25. from django.contrib.auth.context_processors import auth as auth_context_processor
  26. return auth_context_processor(request)
  27. def csrf(request):
  28. """
  29. Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
  30. it has not been provided by either a view decorator or the middleware
  31. """
  32. def _get_val():
  33. token = get_token(request)
  34. if token is None:
  35. # In order to be able to provide debugging info in the
  36. # case of misconfiguration, we use a sentinel value
  37. # instead of returning an empty dict.
  38. return 'NOTPROVIDED'
  39. else:
  40. return token
  41. _get_val = lazy(_get_val, str)
  42. return {'csrf_token': _get_val() }
  43. def debug(request):
  44. "Returns context variables helpful for debugging."
  45. context_extras = {}
  46. if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
  47. context_extras['debug'] = True
  48. from django.db import connection
  49. context_extras['sql_queries'] = connection.queries
  50. return context_extras
  51. def i18n(request):
  52. from django.utils import translation
  53. context_extras = {}
  54. context_extras['LANGUAGES'] = settings.LANGUAGES
  55. context_extras['LANGUAGE_CODE'] = translation.get_language()
  56. context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi()
  57. return context_extras
  58. def media(request):
  59. """
  60. Adds media-related context variables to the context.
  61. """
  62. return {'MEDIA_URL': settings.MEDIA_URL}
  63. def request(request):
  64. return {'request': request}
  65. # PermWrapper and PermLookupDict proxy the permissions system into objects that
  66. # the template system can understand.
  67. class PermLookupDict(object):
  68. def __init__(self, user, module_name):
  69. self.user, self.module_name = user, module_name
  70. def __repr__(self):
  71. return str(self.user.get_all_permissions())
  72. def __getitem__(self, perm_name):
  73. return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
  74. def __nonzero__(self):
  75. return self.user.has_module_perms(self.module_name)
  76. class PermWrapper(object):
  77. def __init__(self, user):
  78. self.user = user
  79. def __getitem__(self, module_name):
  80. return PermLookupDict(self.user, module_name)
  81. def __iter__(self):
  82. # I am large, I contain multitudes.
  83. raise TypeError("PermWrapper is not iterable.")