Code

Ticket #6893: form_wizard_patch_2.diff

File form_wizard_patch_2.diff, 3.3 KB (added by wamberg, 6 years ago)

Updated patch to r8172

Line 
1Index: django/contrib/formtools/wizard.py
2===================================================================
3--- django/contrib/formtools/wizard.py  (revision 8172)
4+++ django/contrib/formtools/wizard.py  (working copy)
5@@ -90,7 +90,7 @@
6             # Otherwise, move along to the next step.
7             else:
8                 form = self.get_form(next_step)
9-                current_step = next_step
10+                self.step = current_step = next_step
11 
12         return self.render(form, request, current_step)
13 
14@@ -209,7 +209,7 @@
15         """
16         context = context or {}
17         context.update(self.extra_context)
18-        return render_to_response(self.get_template(self.step), dict(context,
19+        return render_to_response(self.get_template(step), dict(context,
20             step_field=self.step_field_name,
21             step0=step,
22             step=step + 1,
23Index: django/contrib/formtools/tests.py
24===================================================================
25--- django/contrib/formtools/tests.py   (revision 8172)
26+++ django/contrib/formtools/tests.py   (working copy)
27@@ -1,5 +1,5 @@
28 from django import forms
29-from django.contrib.formtools import preview
30+from django.contrib.formtools import preview, wizard
31 from django import http
32 from django.test import TestCase
33 
34@@ -85,3 +85,48 @@
35         response = self.client.post('/test1/', test_data)
36         self.assertEqual(response.content, success_string)
37 
38+class WizardPageOneForm(forms.Form):
39+    field = forms.CharField()
40+   
41+class WizardPageTwoForm(forms.Form):
42+    field = forms.CharField()
43+   
44+class WizardClass(wizard.FormWizard):
45+
46+    def get_template(self, step):
47+        return "formtools/wizard_form.html"
48+   
49+    def done(self, request, cleaned_data):
50+        return http.HttpResponse(success_string)
51+
52+class DummyRequest(object):
53+    """
54+    We'll pass this as request instead of a real request object
55+    """
56+   
57+    def __init__(self, POST=None):
58+        self.method = "GET"
59+        self.POST = None
60+        if POST:
61+            self.method = "POST"
62+            self.POST = POST
63+
64+class WizardTests(TestCase):
65+    def test_step_starts_at_zero(self):
66+        """
67+        step should be zero for the first form
68+        """
69+        wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm])
70+        request = DummyRequest()
71+        wizard(request)
72+        self.assertEquals(0, wizard.step)
73+       
74+    def test_step_increments(self):
75+        """
76+        step should be incremented when we go to the next page
77+        """
78+        wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm])
79+        request = DummyRequest(POST={"0-field":"test", "wizard_step":"0"})
80+        response = wizard(request)
81+        self.assertEquals(1, wizard.step)
82+       
83Index: django/contrib/formtools/templates/formtools/wizard_form.html
84===================================================================
85--- django/contrib/formtools/templates/formtools/wizard_form.html       (revision 0)
86+++ django/contrib/formtools/templates/formtools/wizard_form.html       (revision 0)
87@@ -0,0 +1,13 @@
88+{% extends "base.html" %}
89+
90+{% block content %}
91+<p>Step {{ step }} of {{ step_count }}</p>
92+<form action="." method="post">
93+<table>
94+{{ form }}
95+</table>
96+<input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
97+{{ previous_fields|safe }}
98+<input type="submit">
99+</form>
100+{% endblock %}