Fixed #18356 -- Gave the test client signals.template_rendered call a unique dispatch_uid

This prevents the test client context from being lost when the client
is used in a nested fashion.
This commit is contained in:
Bojan Mihelac 2013-02-04 16:50:15 +01:00 committed by Tim Graham
parent 453915bb12
commit 0cac4fbf69
5 changed files with 25 additions and 3 deletions

View File

@ -418,6 +418,7 @@ answer newbie questions, and generally made Django that much better:
Christian Metts
michal@plovarna.cz
Justin Michalicek <jmichalicek@gmail.com>
Bojan Mihelac <bmihelac@mihelac.org>
Slawek Mikula <slawek dot mikula at gmail dot com>
Katie Miller <katie@sub50.com>
Shawn Milochik <shawn@milochik.com>

View File

@ -406,7 +406,8 @@ class Client(RequestFactory):
# callback function.
data = {}
on_template_render = curry(store_rendered_templates, data)
signals.template_rendered.connect(on_template_render, dispatch_uid="template-render")
signal_uid = "template-render-%s" % id(request)
signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
# Capture exceptions created by the handler.
got_request_exception.connect(self.store_exc_info, dispatch_uid="request-exception")
try:
@ -452,7 +453,7 @@ class Client(RequestFactory):
return response
finally:
signals.template_rendered.disconnect(dispatch_uid="template-render")
signals.template_rendered.disconnect(dispatch_uid=signal_uid)
got_request_exception.disconnect(dispatch_uid="request-exception")
def get(self, path, data={}, follow=False, **extra):

View File

@ -925,6 +925,14 @@ class ContextTests(TestCase):
finally:
django.template.context._standard_context_processors = None
def test_nested_requests(self):
"""
response.context is not lost when view call another view.
"""
response = self.client.get("/test_client_regress/nested_view/")
self.assertEqual(response.context.__class__, Context)
self.assertEqual(response.context['nested'], 'yes')
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class SessionTests(TestCase):

View File

@ -11,6 +11,7 @@ urlpatterns = patterns('',
(r'^request_data/$', views.request_data),
(r'^request_data_extended/$', views.request_data, {'template':'extended.html', 'data':'bacon'}),
url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
url(r'^nested_view/$', views.nested_view, name='nested_view'),
(r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
(r'^redirects/$', RedirectView.as_view(url='/test_client_regress/redirects/further/')),
(r'^redirects/further/$', RedirectView.as_view(url='/test_client_regress/redirects/further/more/')),

View File

@ -5,8 +5,10 @@ from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.core.serializers.json import DjangoJSONEncoder
from django.test.client import CONTENT_TYPE_RE
from django.template import RequestContext
from django.test import Client
from django.test.client import CONTENT_TYPE_RE
from django.test.utils import setup_test_environment
class CustomTestException(Exception):
@ -52,6 +54,15 @@ def view_with_argument(request, name):
else:
return HttpResponse('Howdy, %s' % name)
def nested_view(request):
"""
A view that uses test client to call another view.
"""
setup_test_environment()
c = Client()
c.get("/test_client_regress/no_template_view")
return render_to_response('base.html', {'nested':'yes'})
def login_protected_redirect_view(request):
"A view that redirects all requests to the GET view"
return HttpResponseRedirect('/test_client_regress/get_view/')