Code

Ticket #19189: ticket_19189.diff

File ticket_19189.diff, 5.7 KB (added by kenth, 21 months ago)

Patch with tests

Line 
1diff --git a/django/contrib/formtools/tests/wizard/__init__.py b/django/contrib/formtools/tests/wizard/__init__.py
2index a2a9692..c025a08 100644
3--- a/django/contrib/formtools/tests/wizard/__init__.py
4+++ b/django/contrib/formtools/tests/wizard/__init__.py
5@@ -16,4 +16,5 @@ from django.contrib.formtools.tests.wizard.wizardtests.tests import (
6     WizardTestKwargs,
7     WizardTestGenericViewInterface,
8     WizardFormKwargsOverrideTests,
9+    WizardRevalidationTests,
10 )
11diff --git a/django/contrib/formtools/tests/wizard/wizardtests/forms.py b/django/contrib/formtools/tests/wizard/wizardtests/forms.py
12index 6a81329..1127b29 100644
13--- a/django/contrib/formtools/tests/wizard/wizardtests/forms.py
14+++ b/django/contrib/formtools/tests/wizard/wizardtests/forms.py
15@@ -65,3 +65,18 @@ class SessionContactWizard(ContactWizard):
16 class CookieContactWizard(ContactWizard):
17     storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'
18 
19+class EmptyPage1(forms.Form):
20+    on_page1 = forms.NullBooleanField()
21+
22+class EmptyPage2(forms.Form):
23+    on_page2 = forms.NullBooleanField()
24+
25+class EmptyPage3(forms.Form):
26+    on_page3 = forms.NullBooleanField()
27+
28+class RevalidationWizard(SessionContactWizard):
29+    def done(self, form_list, **kwargs):
30+        step = self.form_list.keys()[1]
31+        form = form_list[1]
32+        raise self.RevalidationError(step, form, **kwargs)
33+
34diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py
35index 586bd59..761d1fd 100644
36--- a/django/contrib/formtools/tests/wizard/wizardtests/tests.py
37+++ b/django/contrib/formtools/tests/wizard/wizardtests/tests.py
38@@ -385,3 +385,41 @@ class WizardFormKwargsOverrideTests(TestCase):
39         self.assertEqual(formset.initial_form_count(), 1)
40         self.assertEqual(['staff@example.com'],
41             list(formset.queryset.values_list('email', flat=True)))
42+
43+class WizardRevalidationTests(TestCase):
44+    urls = 'django.contrib.formtools.tests.wizard.wizardtests.urls'
45+
46+    wizard_url = '/wiz_revalidation/'
47+    wizard_step_1_data = {
48+        'revalidation_wizard-current_step': 'form1',
49+    }
50+    wizard_step_data = (
51+        {
52+            'revalidation_wizard-current_step': 'form1',
53+        },
54+        {
55+            'revalidation_wizard-current_step': 'form2',
56+        },
57+        {
58+            'revalidation_wizard-current_step': 'form3',
59+        },
60+    )
61+
62+    def test_form_revalidate(self):
63+        response = self.client.get(self.wizard_url)
64+        self.assertEqual(response.status_code, 200)
65+        self.assertEqual(response.context['wizard']['steps'].current, 'form1')
66+
67+        response = self.client.post(self.wizard_url, self.wizard_step_data[0])
68+        self.assertEqual(response.status_code, 200)
69+        self.assertEqual(response.context['wizard']['steps'].current, 'form2')
70+
71+        post_data = self.wizard_step_data[1]
72+        response = self.client.post(self.wizard_url, post_data)
73+        self.assertEqual(response.status_code, 200)
74+        self.assertEqual(response.context['wizard']['steps'].current, 'form3')
75+
76+        response = self.client.post(self.wizard_url, self.wizard_step_data[2])
77+        self.assertEqual(response.status_code, 200)
78+        self.assertEqual(response.context['wizard']['steps'].current, 'form2')
79+
80diff --git a/django/contrib/formtools/tests/wizard/wizardtests/urls.py b/django/contrib/formtools/tests/wizard/wizardtests/urls.py
81index dabce53..f15e2dd 100644
82--- a/django/contrib/formtools/tests/wizard/wizardtests/urls.py
83+++ b/django/contrib/formtools/tests/wizard/wizardtests/urls.py
84@@ -2,6 +2,9 @@ from django.conf.urls import patterns, url
85 from django.contrib.formtools.tests.wizard.wizardtests.forms import (
86     SessionContactWizard, CookieContactWizard, Page1, Page2, Page3, Page4)
87 
88+from django.contrib.formtools.tests.wizard.wizardtests.forms import (
89+    RevalidationWizard, EmptyPage1, EmptyPage2, EmptyPage3)
90+
91 urlpatterns = patterns('',
92     url(r'^wiz_session/$', SessionContactWizard.as_view(
93         [('form1', Page1),
94@@ -19,4 +22,8 @@ urlpatterns = patterns('',
95           ('form3', Page3),
96           ('form4', Page4)],
97           template_name='other_wizard_form.html')),
98+    url(r'^wiz_revalidation/$', RevalidationWizard.as_view(
99+        [('form1', EmptyPage1),
100+         ('form2', EmptyPage2),
101+         ('form3', EmptyPage3)])),
102 )
103diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py
104index ea41e86..2119301 100644
105--- a/django/contrib/formtools/wizard/views.py
106+++ b/django/contrib/formtools/wizard/views.py
107@@ -307,6 +307,16 @@ class WizardView(TemplateView):
108         self.storage.current_step = next_step
109         return self.render(new_form, **kwargs)
110 
111+    class RevalidationError(Exception):
112+        def __init__(self, step, form, **kwargs):
113+            self.step = step
114+            self.form = form
115+            self.kwargs = kwargs
116+
117+        def __repr__(self):
118+            return '%s(%s)' % (self.__class__, self.step)
119+        __str__ = __repr__
120+
121     def render_done(self, form, **kwargs):
122         """
123         This method gets called when all forms passed. The method should also
124@@ -327,7 +337,11 @@ class WizardView(TemplateView):
125         # render the done view and reset the wizard before returning the
126         # response. This is needed to prevent from rendering done with the
127         # same data twice.
128-        done_response = self.done(final_form_list, **kwargs)
129+        try:
130+            done_response = self.done(final_form_list, **kwargs)
131+        except self.RevalidationError as e:
132+            return self.render_revalidation_failure(e.step, e.form, **e.kwargs)
133+
134         self.storage.reset()
135         return done_response
136