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

/google/appengine/_internal/django/core/context_processors.py

https://github.com/theosp/google_appengine
Python | 102 lines | 83 code | 3 blank | 16 comment | 0 complexity | c52b18bd5f8ac5740c6186364bbd4f15 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 google.appengine._internal.django.conf import settings
  9. from google.appengine._internal.django.middleware.csrf import get_token
  10. from google.appengine._internal.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 " "deprecated; use the path `django.contrib.auth.context_processors.auth` " "instead.",
  21. PendingDeprecationWarning
  22. )
  23. from google.appengine._internal.django.contrib.auth.context_processors import auth as auth_context_processor
  24. return auth_context_processor(request)
  25. def csrf(request):
  26. """
  27. Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
  28. it has not been provided by either a view decorator or the middleware
  29. """
  30. def _get_val():
  31. token = get_token(request)
  32. if token is None:
  33. # In order to be able to provide debugging info in the
  34. # case of misconfiguration, we use a sentinel value
  35. # instead of returning an empty dict.
  36. return 'NOTPROVIDED'
  37. else:
  38. return token
  39. _get_val = lazy(_get_val, str)
  40. return {'csrf_token': _get_val() }
  41. def debug(request):
  42. "Returns context variables helpful for debugging."
  43. context_extras = {}
  44. if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
  45. context_extras['debug'] = True
  46. from google.appengine._internal.django.db import connection
  47. context_extras['sql_queries'] = connection.queries
  48. return context_extras
  49. def i18n(request):
  50. from google.appengine._internal.django.utils import translation
  51. context_extras = {}
  52. context_extras['LANGUAGES'] = settings.LANGUAGES
  53. context_extras['LANGUAGE_CODE'] = translation.get_language()
  54. context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi()
  55. return context_extras
  56. def media(request):
  57. """
  58. Adds media-related context variables to the context.
  59. """
  60. return {'MEDIA_URL': settings.MEDIA_URL}
  61. def request(request):
  62. return {'request': request}
  63. # PermWrapper and PermLookupDict proxy the permissions system into objects that
  64. # the template system can understand.
  65. class PermLookupDict(object):
  66. def __init__(self, user, module_name):
  67. self.user, self.module_name = user, module_name
  68. def __repr__(self):
  69. return str(self.user.get_all_permissions())
  70. def __getitem__(self, perm_name):
  71. return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
  72. def __nonzero__(self):
  73. return self.user.has_module_perms(self.module_name)
  74. class PermWrapper(object):
  75. def __init__(self, user):
  76. self.user = user
  77. def __getitem__(self, module_name):
  78. return PermLookupDict(self.user, module_name)
  79. def __iter__(self):
  80. # I am large, I contain multitudes.
  81. raise TypeError("PermWrapper is not iterable.")