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
|
|
|
|
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
|
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
def static(request):
|
|
|
|
"""
|
|
|
|
Adds static-related context variables to the context.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return {'STATIC_URL': settings.STATIC_URL}
|
|
|
|
|
2007-05-29 19:09:24 +08:00
|
|
|
def media(request):
|
|
|
|
"""
|
|
|
|
Adds media-related context variables to the context.
|
|
|
|
|
|
|
|
"""
|
2010-11-12 05:44:33 +08:00
|
|
|
return {'MEDIA_URL': settings.MEDIA_URL}
|
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
|
2011-02-23 21:36:58 +08:00
|
|
|
# the template system can understand. They once lived here -- they have
|
|
|
|
# been moved to django.contrib.auth.context_processors.
|
|
|
|
|
|
|
|
from django.contrib.auth.context_processors import PermLookupDict as RealPermLookupDict
|
|
|
|
from django.contrib.auth.context_processors import PermWrapper as RealPermWrapper
|
|
|
|
|
|
|
|
class PermLookupDict(RealPermLookupDict):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"`django.core.context_processors.PermLookupDict` is " \
|
|
|
|
"deprecated; use `django.contrib.auth.context_processors.PermLookupDict` " \
|
|
|
|
"instead.",
|
2011-04-02 16:37:53 +08:00
|
|
|
DeprecationWarning
|
2011-02-23 21:36:58 +08:00
|
|
|
)
|
|
|
|
super(PermLookupDict, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
class PermWrapper(RealPermWrapper):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"`django.core.context_processors.PermWrapper` is " \
|
|
|
|
"deprecated; use `django.contrib.auth.context_processors.PermWrapper` " \
|
|
|
|
"instead.",
|
2011-04-02 16:37:53 +08:00
|
|
|
DeprecationWarning
|
2011-02-23 21:36:58 +08:00
|
|
|
)
|
|
|
|
super(PermWrapper, self).__init__(*args, **kwargs)
|