mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #12226 -- Deprecated test client Response.template attribute in favor of templates attribute, which is always a list. Thanks Russell for patch review.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14106 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -4,6 +4,7 @@ import sys
|
||||
import os
|
||||
import re
|
||||
import mimetypes
|
||||
import warnings
|
||||
try:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
@@ -93,7 +94,7 @@ def store_rendered_templates(store, signal, sender, template, context, **kwargs)
|
||||
"""
|
||||
Stores templates and contexts that are rendered.
|
||||
"""
|
||||
store.setdefault('template', []).append(template)
|
||||
store.setdefault('templates', []).append(template)
|
||||
store.setdefault('context', ContextList()).append(context)
|
||||
|
||||
def encode_multipart(boundary, data):
|
||||
@@ -260,16 +261,25 @@ class Client(object):
|
||||
response.request = request
|
||||
|
||||
# Add any rendered template detail to the response.
|
||||
# If there was only one template rendered (the most likely case),
|
||||
# flatten the list to a single element.
|
||||
for detail in ('template', 'context'):
|
||||
if data.get(detail):
|
||||
if len(data[detail]) == 1:
|
||||
setattr(response, detail, data[detail][0]);
|
||||
else:
|
||||
setattr(response, detail, data[detail])
|
||||
else:
|
||||
setattr(response, detail, None)
|
||||
response.templates = data.get("templates", [])
|
||||
response.context = data.get("context")
|
||||
|
||||
# Flatten a single context. Not really necessary anymore thanks to
|
||||
# the __getattr__ flattening in ContextList, but has some edge-case
|
||||
# backwards-compatibility implications.
|
||||
if response.context and len(response.context) == 1:
|
||||
response.context = response.context[0]
|
||||
|
||||
# Provide a backwards-compatible (but pending deprecation) response.template
|
||||
def _get_template(self):
|
||||
warnings.warn("response.template is deprecated; use response.templates instead (which is always a list)",
|
||||
PendingDeprecationWarning)
|
||||
if not self.templates:
|
||||
return None
|
||||
elif len(self.templates) == 1:
|
||||
return self.templates[0]
|
||||
return self.templates
|
||||
response.__class__.template = property(_get_template)
|
||||
|
||||
# Update persistent cookie data.
|
||||
if response.cookies:
|
||||
|
||||
@@ -443,7 +443,7 @@ class TransactionTestCase(unittest.TestCase):
|
||||
if msg_prefix:
|
||||
msg_prefix += ": "
|
||||
|
||||
template_names = [t.name for t in to_list(response.template)]
|
||||
template_names = [t.name for t in response.templates]
|
||||
if not template_names:
|
||||
self.fail(msg_prefix + "No templates used to render the response")
|
||||
self.failUnless(template_name in template_names,
|
||||
@@ -459,7 +459,7 @@ class TransactionTestCase(unittest.TestCase):
|
||||
if msg_prefix:
|
||||
msg_prefix += ": "
|
||||
|
||||
template_names = [t.name for t in to_list(response.template)]
|
||||
template_names = [t.name for t in response.templates]
|
||||
self.failIf(template_name in template_names,
|
||||
msg_prefix + "Template '%s' was used unexpectedly in rendering"
|
||||
" the response" % template_name)
|
||||
|
||||
Reference in New Issue
Block a user