Commit fdbfc980 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

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.
parent a0141f9e
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -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):
        """
+13 −18
Original line number Diff line number Diff line
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',
    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',
    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):
+4 −4
Original line number Diff line number Diff line
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."
+32 −21
Original line number Diff line number Diff line
@@ -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,33 +14,48 @@ 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 _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:
        # 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 '
@@ -50,10 +63,8 @@ def render(request, template_name, dictionary=_dictionary_undefined,
        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)

+2 −0
Original line number Diff line number Diff line
@@ -93,6 +93,8 @@ details on these changes.
* The ``dictionary`` and ``context_instance`` parameters for the following
  functions will be removed:

  * ``django.shortcuts.render()``
  * ``django.shortcuts.render_to_response()``
  * ``django.template.loader.render_to_string()``

* The ``dirs`` parameter for the following functions will be removed:
Loading