diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-02 23:23:51 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-28 17:00:07 +0100 |
| commit | 92e8f1f30223d95c0e71ee6ace7bdc476c81abbd (patch) | |
| tree | 19139953edec69f849b0f1ea32821fdc4f38c491 /django | |
| parent | c599f233b17d3890df8f648f8ead25141c21495c (diff) | |
Moved context_processors from django.core to django.template.
Diffstat (limited to 'django')
| -rw-r--r-- | django/conf/global_settings.py | 12 | ||||
| -rw-r--r-- | django/conf/project_template/project_name/settings.py | 10 | ||||
| -rw-r--r-- | django/core/context_processors.py | 89 | ||||
| -rw-r--r-- | django/template/context.py | 2 | ||||
| -rw-r--r-- | django/template/context_processors.py | 85 |
5 files changed, 104 insertions, 94 deletions
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index a950cfd565..d6263b246a 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -218,12 +218,12 @@ TEMPLATE_LOADERS = ( # only parameter and returns a dictionary to add to the context. TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', - 'django.core.context_processors.debug', - 'django.core.context_processors.i18n', - 'django.core.context_processors.media', - 'django.core.context_processors.static', - 'django.core.context_processors.tz', - # 'django.core.context_processors.request', + 'django.template.context_processors.debug', + 'django.template.context_processors.i18n', + 'django.template.context_processors.media', + 'django.template.context_processors.static', + 'django.template.context_processors.tz', + # 'django.template.context_processors.request', 'django.contrib.messages.context_processors.messages', ) diff --git a/django/conf/project_template/project_name/settings.py b/django/conf/project_template/project_name/settings.py index 33b08fa2a9..a782ac4815 100644 --- a/django/conf/project_template/project_name/settings.py +++ b/django/conf/project_template/project_name/settings.py @@ -60,11 +60,11 @@ TEMPLATES = [ 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ - 'django.core.context_processors.debug', - 'django.core.context_processors.i18n', - 'django.core.context_processors.tz', - 'django.core.context_processors.media', - 'django.core.context_processors.static', + 'django.template.context_processors.debug', + 'django.template.context_processors.i18n', + 'django.template.context_processors.tz', + 'django.template.context_processors.media', + 'django.template.context_processors.static', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], diff --git a/django/core/context_processors.py b/django/core/context_processors.py index e5d61fdb16..19cc8b4def 100644 --- a/django/core/context_processors.py +++ b/django/core/context_processors.py @@ -1,85 +1,10 @@ -""" -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. +import warnings -These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by -RequestContext. -""" -from __future__ import unicode_literals +from django.template.context_processors import * # NOQA +from django.utils.deprecation import RemovedInDjango20Warning -from django.conf import settings -from django.middleware.csrf import get_token -from django.utils import six -from django.utils.encoding import smart_text -from django.utils.functional import lazy - -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 smart_text(token) - _get_val = lazy(_get_val, six.text_type) - - return {'csrf_token': _get_val()} - - -def debug(request): - """ - Returns context variables helpful for debugging. - """ - context_extras = {} - if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: - context_extras['debug'] = True - from django.db import connection - # Return a lazy reference that computes connection.queries on access, - # to ensure it contains queries triggered after this function runs. - context_extras['sql_queries'] = lazy(lambda: connection.queries, list) - return context_extras - - -def i18n(request): - from django.utils import translation - - context_extras = {} - context_extras['LANGUAGES'] = settings.LANGUAGES - context_extras['LANGUAGE_CODE'] = translation.get_language() - context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi() - - return context_extras - - -def tz(request): - from django.utils import timezone - - return {'TIME_ZONE': timezone.get_current_timezone_name()} - - -def static(request): - """ - Adds static-related context variables to the context. - - """ - return {'STATIC_URL': settings.STATIC_URL} - - -def media(request): - """ - Adds media-related context variables to the context. - - """ - return {'MEDIA_URL': settings.MEDIA_URL} - - -def request(request): - return {'request': request} +warnings.warn( + "django.core.context_processors is deprecated in favor of " + "django.template.context_processors.", + RemovedInDjango20Warning, stacklevel=2) diff --git a/django/template/context.py b/django/template/context.py index cf59789a6f..fdfeb4df6e 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -2,7 +2,7 @@ from copy import copy # Hard-coded processor for easier use of CSRF protection. -_builtin_context_processors = ('django.core.context_processors.csrf',) +_builtin_context_processors = ('django.template.context_processors.csrf',) class ContextPopException(Exception): diff --git a/django/template/context_processors.py b/django/template/context_processors.py new file mode 100644 index 0000000000..e5d61fdb16 --- /dev/null +++ b/django/template/context_processors.py @@ -0,0 +1,85 @@ +""" +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 +RequestContext. +""" +from __future__ import unicode_literals + +from django.conf import settings +from django.middleware.csrf import get_token +from django.utils import six +from django.utils.encoding import smart_text +from django.utils.functional import lazy + + +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 smart_text(token) + _get_val = lazy(_get_val, six.text_type) + + return {'csrf_token': _get_val()} + + +def debug(request): + """ + Returns context variables helpful for debugging. + """ + context_extras = {} + if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: + context_extras['debug'] = True + from django.db import connection + # Return a lazy reference that computes connection.queries on access, + # to ensure it contains queries triggered after this function runs. + context_extras['sql_queries'] = lazy(lambda: connection.queries, list) + return context_extras + + +def i18n(request): + from django.utils import translation + + context_extras = {} + context_extras['LANGUAGES'] = settings.LANGUAGES + context_extras['LANGUAGE_CODE'] = translation.get_language() + context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi() + + return context_extras + + +def tz(request): + from django.utils import timezone + + return {'TIME_ZONE': timezone.get_current_timezone_name()} + + +def static(request): + """ + Adds static-related context variables to the context. + + """ + return {'STATIC_URL': settings.STATIC_URL} + + +def media(request): + """ + Adds media-related context variables to the context. + + """ + return {'MEDIA_URL': settings.MEDIA_URL} + + +def request(request): + return {'request': request} |
