2014-12-03 06:23:51 +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.
|
|
|
|
|
2014-12-18 06:36:32 +08:00
|
|
|
These are referenced from the 'context_processors' option of the configuration
|
|
|
|
of a DjangoTemplates backend and used by RequestContext.
|
2014-12-03 06:23:51 +08:00
|
|
|
"""
|
2014-12-18 06:36:32 +08:00
|
|
|
|
2016-04-09 20:09:08 +08:00
|
|
|
import itertools
|
|
|
|
|
2014-12-03 06:23:51 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.middleware.csrf import get_token
|
2015-02-09 02:24:11 +08:00
|
|
|
from django.utils.functional import SimpleLazyObject, lazy
|
2014-12-03 06:23:51 +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:
|
2017-01-29 21:58:20 +08:00
|
|
|
return token
|
2014-12-03 06:23:51 +08:00
|
|
|
|
2015-02-09 02:24:11 +08:00
|
|
|
return {'csrf_token': SimpleLazyObject(_get_val)}
|
2014-12-03 06:23:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def debug(request):
|
|
|
|
"""
|
2017-01-25 04:36:36 +08:00
|
|
|
Return context variables helpful for debugging.
|
2014-12-03 06:23:51 +08:00
|
|
|
"""
|
|
|
|
context_extras = {}
|
|
|
|
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
|
|
|
|
context_extras['debug'] = True
|
2016-04-09 20:09:08 +08:00
|
|
|
from django.db import connections
|
2014-12-03 06:23:51 +08:00
|
|
|
# Return a lazy reference that computes connection.queries on access,
|
|
|
|
# to ensure it contains queries triggered after this function runs.
|
2016-04-09 20:09:08 +08:00
|
|
|
context_extras['sql_queries'] = lazy(
|
|
|
|
lambda: list(itertools.chain(*[connections[x].queries for x in connections])),
|
|
|
|
list
|
|
|
|
)
|
2014-12-03 06:23:51 +08:00
|
|
|
return context_extras
|
|
|
|
|
|
|
|
|
|
|
|
def i18n(request):
|
|
|
|
from django.utils import translation
|
2015-11-07 18:46:22 +08:00
|
|
|
return {
|
|
|
|
'LANGUAGES': settings.LANGUAGES,
|
|
|
|
'LANGUAGE_CODE': translation.get_language(),
|
|
|
|
'LANGUAGE_BIDI': translation.get_language_bidi(),
|
|
|
|
}
|
2014-12-03 06:23:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def tz(request):
|
|
|
|
from django.utils import timezone
|
|
|
|
return {'TIME_ZONE': timezone.get_current_timezone_name()}
|
|
|
|
|
|
|
|
|
|
|
|
def static(request):
|
|
|
|
"""
|
2017-01-25 04:36:36 +08:00
|
|
|
Add static-related context variables to the context.
|
2014-12-03 06:23:51 +08:00
|
|
|
"""
|
|
|
|
return {'STATIC_URL': settings.STATIC_URL}
|
|
|
|
|
|
|
|
|
|
|
|
def media(request):
|
|
|
|
"""
|
2017-01-25 04:36:36 +08:00
|
|
|
Add media-related context variables to the context.
|
2014-12-03 06:23:51 +08:00
|
|
|
"""
|
|
|
|
return {'MEDIA_URL': settings.MEDIA_URL}
|
|
|
|
|
|
|
|
|
|
|
|
def request(request):
|
|
|
|
return {'request': request}
|