PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/RawSalad/site-packages/django/core/context_processors.py

https://github.com/CCLab/websites
Python | 113 lines | 94 code | 3 blank | 16 comment | 0 complexity | 83f7861f85b1d643a2d9267a3335acd4 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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. DeprecationWarning
  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 static(request):
  59. """
  60. Adds static-related context variables to the context.
  61. """
  62. return {'STATIC_URL': settings.STATIC_URL}
  63. def media(request):
  64. """
  65. Adds media-related context variables to the context.
  66. """
  67. return {'MEDIA_URL': settings.MEDIA_URL}
  68. def request(request):
  69. return {'request': request}
  70. # PermWrapper and PermLookupDict proxy the permissions system into objects that
  71. # the template system can understand. They once lived here -- they have
  72. # been moved to django.contrib.auth.context_processors.
  73. from django.contrib.auth.context_processors import PermLookupDict as RealPermLookupDict
  74. from django.contrib.auth.context_processors import PermWrapper as RealPermWrapper
  75. class PermLookupDict(RealPermLookupDict):
  76. def __init__(self, *args, **kwargs):
  77. import warnings
  78. warnings.warn(
  79. "`django.core.context_processors.PermLookupDict` is " \
  80. "deprecated; use `django.contrib.auth.context_processors.PermLookupDict` " \
  81. "instead.",
  82. PendingDeprecationWarning
  83. )
  84. super(PermLookupDict, self).__init__(*args, **kwargs)
  85. class PermWrapper(RealPermWrapper):
  86. def __init__(self, *args, **kwargs):
  87. import warnings
  88. warnings.warn(
  89. "`django.core.context_processors.PermWrapper` is " \
  90. "deprecated; use `django.contrib.auth.context_processors.PermWrapper` " \
  91. "instead.",
  92. PendingDeprecationWarning
  93. )
  94. super(PermWrapper, self).__init__(*args, **kwargs)