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.
6 These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by
10 from django
.conf
import settings
11 from django
.middleware
.csrf
import get_token
12 from django
.utils
.functional
import lazy
16 DEPRECATED. This context processor is the old location, and has been moved
17 to `django.contrib.auth.context_processors`.
19 This function still exists for backwards-compatibility; it will be removed
24 "The context processor at `django.core.context_processors.auth` is " \
25 "deprecated; use the path `django.contrib.auth.context_processors.auth` " \
27 PendingDeprecationWarning
29 from django
.contrib
.auth
.context_processors
import auth
as auth_context_processor
30 return auth_context_processor(request
)
34 Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
35 it has not been provided by either a view decorator or the middleware
38 token
= get_token(request
)
40 # In order to be able to provide debugging info in the
41 # case of misconfiguration, we use a sentinel value
42 # instead of returning an empty dict.
46 _get_val
= lazy(_get_val
, str)
48 return {'csrf_token': _get_val() }
51 "Returns context variables helpful for debugging."
53 if settings
.DEBUG
and request
.META
.get('REMOTE_ADDR') in settings
.INTERNAL_IPS
:
54 context_extras
['debug'] = True
55 from django
.db
import connection
56 context_extras
['sql_queries'] = connection
.queries
60 from django
.utils
import translation
63 context_extras
['LANGUAGES'] = settings
.LANGUAGES
64 context_extras
['LANGUAGE_CODE'] = translation
.get_language()
65 context_extras
['LANGUAGE_BIDI'] = translation
.get_language_bidi()
71 Adds media-related context variables to the context.
74 return {'MEDIA_URL': settings
.MEDIA_URL
}
77 return {'request': request
}
79 # PermWrapper and PermLookupDict proxy the permissions system into objects that
80 # the template system can understand.
82 class PermLookupDict(object):
83 def __init__(self
, user
, module_name
):
84 self
.user
, self
.module_name
= user
, module_name
87 return str(self
.user
.get_all_permissions())
89 def __getitem__(self
, perm_name
):
90 return self
.user
.has_perm("%s.%s" % (self
.module_name
, perm_name
))
92 def __nonzero__(self
):
93 return self
.user
.has_module_perms(self
.module_name
)
95 class PermWrapper(object):
96 def __init__(self
, user
):
99 def __getitem__(self
, module_name
):
100 return PermLookupDict(self
.user
, module_name
)
103 # I am large, I contain multitudes.
104 raise TypeError("PermWrapper is not iterable.")