summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2014-12-14 10:17:18 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2014-12-28 17:02:29 +0100
commitfdbfc98003f0ba2d3a12def63a75560791f3602d (patch)
tree0238fba169970595f1a8c44d53550fada6236b4f /django
parenta0141f9eac03f0fef722e757253bbc939c018f77 (diff)
Deprecated some arguments of django.shortcuts.render(_to_response).
dictionary and context_instance and superseded by context. Refactored tests that relied context_instance with more modern idioms.
Diffstat (limited to 'django')
-rw-r--r--django/contrib/auth/tests/test_context_processors.py10
-rw-r--r--django/contrib/auth/tests/urls.py31
-rw-r--r--django/contrib/gis/admin/widgets.py8
-rw-r--r--django/shortcuts.py53
4 files changed, 53 insertions, 49 deletions
diff --git a/django/contrib/auth/tests/test_context_processors.py b/django/contrib/auth/tests/test_context_processors.py
index e0e75a947e..cb96bf5d6d 100644
--- a/django/contrib/auth/tests/test_context_processors.py
+++ b/django/contrib/auth/tests/test_context_processors.py
@@ -65,6 +65,10 @@ class PermWrapperTests(TestCase):
TEMPLATE_DIRS=(
os.path.join(os.path.dirname(upath(__file__)), 'templates'),
),
+ TEMPLATE_CONTEXT_PROCESSORS=(
+ 'django.contrib.auth.context_processors.auth',
+ 'django.contrib.messages.context_processors.messages'
+ ),
ROOT_URLCONF='django.contrib.auth.tests.urls',
USE_TZ=False, # required for loading the fixture
PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
@@ -80,9 +84,6 @@ class AuthContextProcessorTests(TestCase):
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
),
- TEMPLATE_CONTEXT_PROCESSORS=(
- 'django.contrib.auth.context_processors.auth',
- ),
)
def test_session_not_accessed(self):
"""
@@ -97,9 +98,6 @@ class AuthContextProcessorTests(TestCase):
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
),
- TEMPLATE_CONTEXT_PROCESSORS=(
- 'django.contrib.auth.context_processors.auth',
- ),
)
def test_session_is_accessed(self):
"""
diff --git a/django/contrib/auth/tests/urls.py b/django/contrib/auth/tests/urls.py
index 8145f2f69c..41e742a93b 100644
--- a/django/contrib/auth/tests/urls.py
+++ b/django/contrib/auth/tests/urls.py
@@ -1,13 +1,12 @@
from django.conf.urls import url, include
from django.contrib import admin
-from django.contrib.auth import context_processors
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.urls import urlpatterns
from django.contrib.auth import views
from django.contrib.auth.decorators import login_required
from django.contrib.messages.api import info
from django.http import HttpResponse, HttpRequest
-from django.shortcuts import render_to_response
+from django.shortcuts import render
from django.template import Template, RequestContext
from django.views.decorators.cache import never_cache
@@ -27,39 +26,35 @@ def remote_user_auth_view(request):
def auth_processor_no_attr_access(request):
- render_to_response('context_processors/auth_attrs_no_access.html',
- context_instance=RequestContext(request, {}, processors=[context_processors.auth]))
+ render(request, 'context_processors/auth_attrs_no_access.html')
# *After* rendering, we check whether the session was accessed
- return render_to_response('context_processors/auth_attrs_test_access.html',
- {'session_accessed': request.session.accessed})
+ return render(request,
+ 'context_processors/auth_attrs_test_access.html',
+ {'session_accessed': request.session.accessed})
def auth_processor_attr_access(request):
- render_to_response('context_processors/auth_attrs_access.html',
- context_instance=RequestContext(request, {}, processors=[context_processors.auth]))
- return render_to_response('context_processors/auth_attrs_test_access.html',
- {'session_accessed': request.session.accessed})
+ render(request, 'context_processors/auth_attrs_access.html')
+ return render(request,
+ 'context_processors/auth_attrs_test_access.html',
+ {'session_accessed': request.session.accessed})
def auth_processor_user(request):
- return render_to_response('context_processors/auth_attrs_user.html',
- context_instance=RequestContext(request, {}, processors=[context_processors.auth]))
+ return render(request, 'context_processors/auth_attrs_user.html')
def auth_processor_perms(request):
- return render_to_response('context_processors/auth_attrs_perms.html',
- context_instance=RequestContext(request, {}, processors=[context_processors.auth]))
+ return render(request, 'context_processors/auth_attrs_perms.html')
def auth_processor_perm_in_perms(request):
- return render_to_response('context_processors/auth_attrs_perm_in_perms.html',
- context_instance=RequestContext(request, {}, processors=[context_processors.auth]))
+ return render(request, 'context_processors/auth_attrs_perm_in_perms.html')
def auth_processor_messages(request):
info(request, "Message 1")
- return render_to_response('context_processors/auth_attrs_messages.html',
- context_instance=RequestContext(request, {}, processors=[context_processors.auth]))
+ return render(request, 'context_processors/auth_attrs_messages.html')
def userpage(request):
diff --git a/django/contrib/gis/admin/widgets.py b/django/contrib/gis/admin/widgets.py
index 716327cf0a..c85e7a6125 100644
--- a/django/contrib/gis/admin/widgets.py
+++ b/django/contrib/gis/admin/widgets.py
@@ -1,7 +1,7 @@
import logging
from django.forms.widgets import Textarea
-from django.template import loader, Context
+from django.template import loader
from django.utils import six
from django.utils import translation
@@ -10,7 +10,7 @@ from django.contrib.gis.geos import GEOSGeometry, GEOSException
# Creating a template context that contains Django settings
# values needed by admin map templates.
-geo_context = Context({'LANGUAGE_BIDI': translation.get_language_bidi()})
+geo_context = {'LANGUAGE_BIDI': translation.get_language_bidi()}
logger = logging.getLogger('django.contrib.gis')
@@ -81,8 +81,8 @@ class OpenLayersWidget(Textarea):
# geometry.
self.params['wkt'] = wkt
- return loader.render_to_string(self.template, self.params,
- context_instance=geo_context)
+ self.params.update(geo_context)
+ return loader.render_to_string(self.template, self.params)
def map_options(self):
"Builds the map options hash for the OpenLayers template."
diff --git a/django/shortcuts.py b/django/shortcuts.py
index f3c07a6972..780f89a6b5 100644
--- a/django/shortcuts.py
+++ b/django/shortcuts.py
@@ -3,8 +3,6 @@ This module collects helper functions and classes that "span" multiple levels
of MVC. In other words, these functions/classes introduce controlled coupling
for convenience's sake.
"""
-import warnings
-
from django.template import loader, RequestContext
from django.template.context import _current_app_undefined
from django.template.engine import (
@@ -16,44 +14,57 @@ from django.db.models.manager import Manager
from django.db.models.query import QuerySet
from django.core import urlresolvers
from django.utils import six
-from django.utils.deprecation import RemovedInDjango20Warning
-def render_to_response(template_name, dictionary=_dictionary_undefined,
+def render_to_response(template_name, context=None,
context_instance=_context_instance_undefined,
- content_type=None, dirs=_dirs_undefined):
+ content_type=None, status=None, dirs=_dirs_undefined,
+ dictionary=_dictionary_undefined):
"""
Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
- # TODO: refactor to avoid the deprecated code path.
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
- content = loader.render_to_string(template_name, dictionary, context_instance, dirs)
+ if (context_instance is _context_instance_undefined
+ and dirs is _dirs_undefined
+ and dictionary is _dictionary_undefined):
+ # No deprecated arguments were passed - use the new code path
+ content = loader.get_template(template_name).render(context)
+
+ else:
+ # Some deprecated arguments were passed - use the legacy code path
+ content = loader.render_to_string(
+ template_name, context, context_instance, dirs, dictionary)
- return HttpResponse(content, content_type)
+ return HttpResponse(content, content_type, status)
-def render(request, template_name, dictionary=_dictionary_undefined,
+def render(request, template_name, context=None,
context_instance=_context_instance_undefined,
content_type=None, status=None, current_app=_current_app_undefined,
- dirs=_dirs_undefined):
+ dirs=_dirs_undefined, dictionary=_dictionary_undefined):
"""
Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
Uses a RequestContext by default.
"""
- if context_instance is not _context_instance_undefined:
- if current_app is not _current_app_undefined:
- raise ValueError('If you provide a context_instance you must '
- 'set its current_app before calling render()')
+ if (context_instance is _context_instance_undefined
+ and current_app is _current_app_undefined
+ and dirs is _dirs_undefined
+ and dictionary is _dictionary_undefined):
+ # No deprecated arguments were passed - use the new code path
+ content = loader.get_template(template_name).render(context, request)
+
else:
- context_instance = RequestContext(request, current_app=current_app)
+ # Some deprecated arguments were passed - use the legacy code path
+ if context_instance is not _context_instance_undefined:
+ if current_app is not _current_app_undefined:
+ raise ValueError('If you provide a context_instance you must '
+ 'set its current_app before calling render()')
+ else:
+ context_instance = RequestContext(request, current_app=current_app)
- # TODO: refactor to avoid the deprecated code path.
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
- content = loader.render_to_string(template_name, dictionary, context_instance, dirs)
+ content = loader.render_to_string(
+ template_name, context, context_instance, dirs, dictionary)
return HttpResponse(content, content_type, status)