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
|
|
|
"""
|
2012-08-13 17:34:40 +08:00
|
|
|
from __future__ import unicode_literals
|
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
|
2012-08-13 17:34:40 +08:00
|
|
|
from django.utils import six
|
|
|
|
from django.utils.encoding import smart_text
|
2010-02-22 07:40:47 +08:00
|
|
|
from django.utils.functional import lazy
|
2005-12-24 12:41:10 +08:00
|
|
|
|
2012-08-13 17:34:40 +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.
|
2012-08-13 17:34:40 +08:00
|
|
|
return 'NOTPROVIDED'
|
2009-10-27 08:36:34 +08:00
|
|
|
else:
|
2012-08-13 17:34:40 +08:00
|
|
|
return smart_text(token)
|
|
|
|
_get_val = lazy(_get_val, six.text_type)
|
2009-10-27 08:36:34 +08:00
|
|
|
|
2013-10-15 03:13:14 +08:00
|
|
|
return {'csrf_token': _get_val()}
|
2009-10-27 08:36:34 +08:00
|
|
|
|
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
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def tz(request):
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
return {'TIME_ZONE': timezone.get_current_timezone_name()}
|
|
|
|
|
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}
|