Code

Ticket #17148: trac-17148.diff

File trac-17148.diff, 6.2 KB (added by steph, 2 years ago)

Updated patch and extended tests

Line 
1diff --git a/django/contrib/formtools/tests/wizard/__init__.py b/django/contrib/formtools/tests/wizard/__init__.py
2index dce2fed..7b15fb5 100644
3--- a/django/contrib/formtools/tests/wizard/__init__.py
4+++ b/django/contrib/formtools/tests/wizard/__init__.py
5@@ -14,4 +14,5 @@ from django.contrib.formtools.tests.wizard.wizardtests.tests import (
6     SessionWizardTests,
7     CookieWizardTests,
8     WizardTestKwargs,
9+    WizardTestGenericViewInterface,
10 )
11diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py
12index 9868721..3eb77cb 100644
13--- a/django/contrib/formtools/tests/wizard/wizardtests/tests.py
14+++ b/django/contrib/formtools/tests/wizard/wizardtests/tests.py
15@@ -1,9 +1,12 @@
16 from __future__ import with_statement
17 import os
18 
19+from django import forms
20 from django.test import TestCase
21+from django.test.client import RequestFactory
22 from django.conf import settings
23 from django.contrib.auth.models import User
24+from django.contrib.formtools.wizard.views import CookieWizardView
25 
26 
27 class WizardTests(object):
28@@ -280,3 +283,51 @@ class WizardTestKwargs(TestCase):
29                 TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]):
30             response = self.client.get(self.wizard_url)
31             self.assertTemplateUsed(response, 'other_wizard_form.html')
32+
33+
34+class WizardTestGenericViewInterface(TestCase):
35+    def test_get_context_data_inheritance(self):
36+        class TestWizard(CookieWizardView):
37+            """
38+            A subclass that implements ``get_context_data`` using the standard
39+            protocol for generic views (accept only **kwargs).
40+
41+            See ticket #17148.
42+            """
43+            def get_context_data(self, **kwargs):
44+                context = super(TestWizard, self).get_context_data(**kwargs)
45+                context['test_key'] = 'test_value'
46+                return context
47+
48+        factory = RequestFactory()
49+        view = TestWizard.as_view([forms.Form])
50+
51+        response = view(factory.get('/'))
52+        self.assertEquals(response.context_data['test_key'], 'test_value')
53+
54+    def test_get_context_data_with_mixin(self):
55+        class AnotherMixin(object):
56+            def get_context_data(self, **kwargs):
57+                context = super(AnotherMixin, self).get_context_data(**kwargs)
58+                context['another_key'] = 'another_value'
59+                return context
60+
61+        class TestWizard(AnotherMixin, CookieWizardView):
62+            """
63+            A subclass that implements ``get_context_data`` using the standard
64+            protocol for generic views (accept only **kwargs).
65+
66+            See ticket #17148.
67+            """
68+            def get_context_data(self, **kwargs):
69+                context = super(TestWizard, self).get_context_data(**kwargs)
70+                context['test_key'] = 'test_value'
71+                return context
72+
73+        factory = RequestFactory()
74+
75+        view = TestWizard.as_view([forms.Form])
76+
77+        response = view(factory.get('/'))
78+        self.assertEquals(response.context_data['test_key'], 'test_value')
79+        self.assertEquals(response.context_data['another_key'], 'another_value')
80diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py
81index 15ba146..0e1486c 100644
82--- a/django/contrib/formtools/wizard/views.py
83+++ b/django/contrib/formtools/wizard/views.py
84@@ -497,7 +497,7 @@ class WizardView(TemplateView):
85             step = self.steps.current
86         return self.get_form_list().keyOrder.index(step)
87 
88-    def get_context_data(self, form, *args, **kwargs):
89+    def get_context_data(self, form, **kwargs):
90         """
91         Returns the template context for a step. You can overwrite this method
92         to add more data for all or some steps. This method returns a
93@@ -514,12 +514,12 @@ class WizardView(TemplateView):
94 
95             class MyWizard(FormWizard):
96                 def get_context_data(self, form, **kwargs):
97-                    context = super(MyWizard, self).get_context_data(form, **kwargs)
98+                    context = super(MyWizard, self).get_context_data(form=form, **kwargs)
99                     if self.steps.current == 'my_step_name':
100                         context.update({'another_var': True})
101                     return context
102         """
103-        context = super(WizardView, self).get_context_data(*args, **kwargs)
104+        context = super(WizardView, self).get_context_data(**kwargs)
105         context.update(self.storage.extra_data)
106         context['wizard'] = {
107             'form': form,
108@@ -535,7 +535,7 @@ class WizardView(TemplateView):
109         Returns a ``HttpResponse`` containing all needed context data.
110         """
111         form = form or self.get_form()
112-        context = self.get_context_data(form, **kwargs)
113+        context = self.get_context_data(form=form, **kwargs)
114         return self.render_to_response(context)
115 
116     def done(self, form_list, **kwargs):
117@@ -683,4 +683,3 @@ class NamedUrlCookieWizardView(NamedUrlWizardView):
118     A NamedUrlFormWizard with pre-configured CookieStorageBackend.
119     """
120     storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'
121-
122diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt
123index aadd7d2..4c071c1 100644
124--- a/docs/ref/contrib/formtools/form-wizard.txt
125+++ b/docs/ref/contrib/formtools/form-wizard.txt
126@@ -309,7 +309,7 @@ Advanced ``WizardView`` methods
127     Example to add extra variables for a specific step::
128 
129         def get_context_data(self, form, **kwargs):
130-            context = super(MyWizard, self).get_context_data(form, **kwargs)
131+            context = super(MyWizard, self).get_context_data(form=form, **kwargs)
132             if self.steps.current == 'my_step_name':
133                 context.update({'another_var': True})
134             return context
135@@ -436,7 +436,7 @@ Advanced ``WizardView`` methods
136 
137         def render(self, form=None, **kwargs):
138             form = form or self.get_form()
139-            context = self.get_context_data(form, **kwargs)
140+            context = self.get_context_data(form=form, **kwargs)
141             return self.render_to_response(context)
142 
143 Providing initial data for the forms