Code

Ticket #11726: ticket11726.diff

File ticket11726.diff, 2.8 KB (added by michalm, 3 years ago)

Alternative fix, patched against trunk, includes failing testcase

Line 
1diff --git a/django/contrib/formtools/tests/__init__.py b/django/contrib/formtools/tests/__init__.py
2index b5037f4..be0372a 100644
3--- a/django/contrib/formtools/tests/__init__.py
4+++ b/django/contrib/formtools/tests/__init__.py
5@@ -322,6 +322,38 @@ class WizardTests(TestCase):
6         response = self.client.post('/wizard/', data)
7         self.assertEqual(2, response.context['step0'])
8 
9+    def test_11726(self):
10+        """
11+        Regression test for ticket #11726.
12+        Wizard should not raise Http404 when steps are added dynamically.
13+        """
14+        reached = [False]
15+        that = self
16+
17+        class WizardWithProcessStep(WizardClass):
18+            def process_step(self, request, form, step):
19+                if step == 0:
20+                    if self.num_steps() < 2:
21+                        self.form_list.append(WizardPageTwoForm)
22+                if step == 1:
23+                    that.assertTrue(isinstance(form, WizardPageTwoForm))
24+                    reached[0] = True
25+
26+        wizard = WizardWithProcessStep([WizardPageOneForm])
27+        data = {"0-field": "test",
28+                "1-field": "test2",
29+                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
30+                "wizard_step": "1"}
31+        wizard(DummyRequest(POST=data))
32+        self.assertTrue(reached[0])
33+
34+        data = {"0-field": "test",
35+                "1-field": "test2",
36+                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
37+                "hash_1": "d5b434e3934cc92fee4bd2964c4ebc06f81d362d",
38+                "wizard_step": "2"}
39+        self.assertRaises(http.Http404, wizard, DummyRequest(POST=data))
40+
41     def test_14498(self):
42         """
43         Regression test for ticket #14498.  All previous steps' forms should be
44diff --git a/django/contrib/formtools/wizard.py b/django/contrib/formtools/wizard.py
45index af6f97b..c19578c 100644
46--- a/django/contrib/formtools/wizard.py
47+++ b/django/contrib/formtools/wizard.py
48@@ -47,6 +47,9 @@ class FormWizard(object):
49 
50     def get_form(self, step, data=None):
51         "Helper method that returns the Form instance for the given step."
52+        # Sanity check.
53+        if step >= self.num_steps():
54+            raise Http404('Step %s does not exist' % step)
55         return self.form_list[step](data, prefix=self.prefix_for_step(step), initial=self.initial.get(step, None))
56 
57     def num_steps(self):
58@@ -71,10 +74,6 @@ class FormWizard(object):
59         current_step = self.determine_step(request, *args, **kwargs)
60         self.parse_params(request, *args, **kwargs)
61 
62-        # Sanity check.
63-        if current_step >= self.num_steps():
64-            raise Http404('Step %s does not exist' % current_step)
65-
66         # Validate and process all the previous forms before instantiating the
67         # current step's form in case self.process_step makes changes to
68         # self.form_list.