Code

Ticket #12226: 12226_r13760.diff

File 12226_r13760.diff, 3.4 KB (added by carljm, 4 years ago)
Line 
1diff --git a/django/test/client.py b/django/test/client.py
2index 08e3ff6..0588088 100644
3--- a/django/test/client.py
4+++ b/django/test/client.py
5@@ -4,6 +4,7 @@ import sys
6 import os
7 import re
8 import mimetypes
9+import warnings
10 try:
11     from cStringIO import StringIO
12 except ImportError:
13@@ -93,7 +94,7 @@ def store_rendered_templates(store, signal, sender, template, context, **kwargs)
14     """
15     Stores templates and contexts that are rendered.
16     """
17-    store.setdefault('template', []).append(template)
18+    store.setdefault('templates', []).append(template)
19     store.setdefault('context', ContextList()).append(context)
20 
21 def encode_multipart(boundary, data):
22@@ -260,16 +261,25 @@ class Client(object):
23             response.request = request
24 
25             # Add any rendered template detail to the response.
26-            # If there was only one template rendered (the most likely case),
27-            # flatten the list to a single element.
28-            for detail in ('template', 'context'):
29-                if data.get(detail):
30-                    if len(data[detail]) == 1:
31-                        setattr(response, detail, data[detail][0]);
32-                    else:
33-                        setattr(response, detail, data[detail])
34-                else:
35-                    setattr(response, detail, None)
36+            response.templates = data.get("templates", [])
37+            response.context = data.get("context")
38+
39+            # Flatten a single context. Not really necessary anymore thanks to
40+            # the __getattr__ flattening in ContextList, but has some edge-case
41+            # backwards-compatibility implications.
42+            if response.context and len(response.context) == 1:
43+                response.context = response.context[0]
44+
45+            # Provide a backwards-compatible (but pending deprecation) response.template
46+            def _get_template(self):
47+                warnings.warn("response.template is deprecated; use response.templates instead (which is always a list)",
48+                              PendingDeprecationWarning)
49+                if not self.templates:
50+                    return None
51+                elif len(self.templates) == 1:
52+                    return self.templates[0]
53+                return self.templates
54+            response.__class__.template = property(_get_template)
55 
56             # Update persistent cookie data.
57             if response.cookies:
58diff --git a/django/test/testcases.py b/django/test/testcases.py
59index 10bd6c6..1a34fd9 100644
60--- a/django/test/testcases.py
61+++ b/django/test/testcases.py
62@@ -445,7 +445,7 @@ class TransactionTestCase(unittest.TestCase):
63         if msg_prefix:
64             msg_prefix += ": "
65 
66-        template_names = [t.name for t in to_list(response.template)]
67+        template_names = [t.name for t in response.templates]
68         if not template_names:
69             self.fail(msg_prefix + "No templates used to render the response")
70         self.failUnless(template_name in template_names,
71@@ -461,7 +461,7 @@ class TransactionTestCase(unittest.TestCase):
72         if msg_prefix:
73             msg_prefix += ": "
74 
75-        template_names = [t.name for t in to_list(response.template)]
76+        template_names = [t.name for t in response.templates]
77         self.failIf(template_name in template_names,
78             msg_prefix + "Template '%s' was used unexpectedly in rendering"
79             " the response" % template_name)