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
14
Returns context variables required by apps that use Django's authentication
17
If there is no 'user' attribute in the request, uses AnonymousUser (from
20
if hasattr(request, 'user'):
23
from django.contrib.auth.models import AnonymousUser
24
user = AnonymousUser()
27
'messages': user.get_and_delete_messages(),
28
'perms': PermWrapper(user),
32
"Returns context variables helpful for debugging."
34
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
35
context_extras['debug'] = True
36
from django.db import connection
37
context_extras['sql_queries'] = connection.queries
41
from django.utils import translation
44
context_extras['LANGUAGES'] = settings.LANGUAGES
45
context_extras['LANGUAGE_CODE'] = translation.get_language()
46
context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi()
52
Adds media-related context variables to the context.
55
return {'MEDIA_URL': settings.MEDIA_URL}
58
return {'request': request}
60
# PermWrapper and PermLookupDict proxy the permissions system into objects that
61
# the template system can understand.
63
class PermLookupDict(object):
64
def __init__(self, user, module_name):
65
self.user, self.module_name = user, module_name
68
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))
73
def __nonzero__(self):
74
return self.user.has_module_perms(self.module_name)
76
class PermWrapper(object):
77
def __init__(self, user):
80
def __getitem__(self, module_name):
81
return PermLookupDict(self.user, module_name)
84
# I am large, I contain multitudes.
85
raise TypeError("PermWrapper is not iterable.")