2005-12-24 12:41:10 +08:00
|
|
|
"""
|
|
|
|
A set of request processors that return dictionaries to be merged into a
|
|
|
|
template context. Each function takes the request object as its only parameter
|
|
|
|
and returns a dictionary to add to the context.
|
|
|
|
|
|
|
|
These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by
|
2006-05-02 09:31:56 +08:00
|
|
|
RequestContext.
|
2005-12-24 12:41:10 +08:00
|
|
|
"""
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2009-10-27 08:36:34 +08:00
|
|
|
from django.middleware.csrf import get_token
|
2010-02-22 07:40:47 +08:00
|
|
|
from django.utils.functional import lazy
|
2005-12-24 12:41:10 +08:00
|
|
|
|
|
|
|
def auth(request):
|
|
|
|
"""
|
2010-02-22 07:40:47 +08:00
|
|
|
DEPRECATED. This context processor is the old location, and has been moved
|
|
|
|
to `django.contrib.auth.context_processors`.
|
2007-09-16 19:27:40 +08:00
|
|
|
|
2010-02-22 07:40:47 +08:00
|
|
|
This function still exists for backwards-compatibility; it will be removed
|
|
|
|
in Django 1.4.
|
2005-12-24 12:41:10 +08:00
|
|
|
"""
|
2010-02-22 07:40:47 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"The context processor at `django.core.context_processors.auth` is " \
|
|
|
|
"deprecated; use the path `django.contrib.auth.context_processors.auth` " \
|
|
|
|
"instead.",
|
2010-10-11 20:20:07 +08:00
|
|
|
DeprecationWarning
|
2010-02-22 07:40:47 +08:00
|
|
|
)
|
|
|
|
from django.contrib.auth.context_processors import auth as auth_context_processor
|
|
|
|
return auth_context_processor(request)
|
2005-12-24 12:41:10 +08:00
|
|
|
|
2009-10-27 08:36:34 +08:00
|
|
|
def csrf(request):
|
|
|
|
"""
|
|
|
|
Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
|
|
|
|
it has not been provided by either a view decorator or the middleware
|
|
|
|
"""
|
|
|
|
def _get_val():
|
|
|
|
token = get_token(request)
|
|
|
|
if token is None:
|
|
|
|
# In order to be able to provide debugging info in the
|
|
|
|
# case of misconfiguration, we use a sentinel value
|
|
|
|
# instead of returning an empty dict.
|
|
|
|
return 'NOTPROVIDED'
|
|
|
|
else:
|
|
|
|
return token
|
|
|
|
_get_val = lazy(_get_val, str)
|
|
|
|
|
|
|
|
return {'csrf_token': _get_val() }
|
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
def debug(request):
|
|
|
|
"Returns context variables helpful for debugging."
|
|
|
|
context_extras = {}
|
2006-05-02 09:31:56 +08:00
|
|
|
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
|
2005-12-24 12:41:10 +08:00
|
|
|
context_extras['debug'] = True
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import connection
|
|
|
|
context_extras['sql_queries'] = connection.queries
|
2005-12-24 12:41:10 +08:00
|
|
|
return context_extras
|
|
|
|
|
|
|
|
def i18n(request):
|
2008-07-06 13:25:55 +08:00
|
|
|
from django.utils import translation
|
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
context_extras = {}
|
2006-05-02 09:31:56 +08:00
|
|
|
context_extras['LANGUAGES'] = settings.LANGUAGES
|
2008-07-06 13:25:55 +08:00
|
|
|
context_extras['LANGUAGE_CODE'] = translation.get_language()
|
2006-06-06 23:22:53 +08:00
|
|
|
context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi()
|
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
return context_extras
|
|
|
|
|
2007-05-29 19:09:24 +08:00
|
|
|
def media(request):
|
|
|
|
"""
|
|
|
|
Adds media-related context variables to the context.
|
|
|
|
|
|
|
|
"""
|
2010-10-20 09:33:24 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"The context processor at `django.core.context_processors.media` is " \
|
|
|
|
"deprecated; use the path `django.contrib.staticfiles.context_processors.staticfiles` " \
|
|
|
|
"instead.",
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
|
|
|
from django.contrib.staticfiles.context_processors import staticfiles as staticfiles_context_processor
|
|
|
|
return staticfiles_context_processor(request)
|
2007-05-29 19:09:24 +08:00
|
|
|
|
2006-01-27 23:55:04 +08:00
|
|
|
def request(request):
|
|
|
|
return {'request': request}
|
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
# PermWrapper and PermLookupDict proxy the permissions system into objects that
|
|
|
|
# the template system can understand.
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class PermLookupDict(object):
|
2005-12-24 12:41:10 +08:00
|
|
|
def __init__(self, user, module_name):
|
|
|
|
self.user, self.module_name = user, module_name
|
2006-09-23 16:35:10 +08:00
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
def __repr__(self):
|
2006-09-23 16:35:10 +08:00
|
|
|
return str(self.user.get_all_permissions())
|
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
def __getitem__(self, perm_name):
|
|
|
|
return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
|
2006-09-23 16:35:10 +08:00
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
def __nonzero__(self):
|
|
|
|
return self.user.has_module_perms(self.module_name)
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class PermWrapper(object):
|
2005-12-24 12:41:10 +08:00
|
|
|
def __init__(self, user):
|
|
|
|
self.user = user
|
2006-09-23 16:35:10 +08:00
|
|
|
|
2005-12-24 12:41:10 +08:00
|
|
|
def __getitem__(self, module_name):
|
|
|
|
return PermLookupDict(self.user, module_name)
|
2009-10-15 02:09:13 +08:00
|
|
|
|
2008-08-09 23:38:44 +08:00
|
|
|
def __iter__(self):
|
|
|
|
# I am large, I contain multitudes.
|
|
|
|
raise TypeError("PermWrapper is not iterable.")
|