Permalink
Browse files

Fixed #9200 -- Added new form wizard to formtools based on class base…

…d views. Many thanks to Stephan Jäkel, ddurham and ElliottM for their work.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 1a951fa commit 632dfa233839f8c76bd6b065b8420fe0ab96175a @jezdez jezdez committed Jun 1, 2011
Showing with 2,594 additions and 344 deletions.
  1. +76 −50 django/contrib/formtools/tests/__init__.py
  2. +43 −0 django/contrib/formtools/tests/forms.py
  3. +13 −13 {tests/regressiontests/formwizard → django/contrib/formtools/tests}/templates/forms/wizard.html
  4. +0 −9 django/contrib/formtools/tests/templates/formwizard/wizard.html
  5. +9 −6 django/contrib/formtools/tests/urls.py
  6. +1 −0 django/contrib/formtools/wizard/__init__.py
  7. +7 −0 django/contrib/formtools/wizard/forms.py
  8. +11 −12 django/contrib/formtools/{wizard.py → wizard/legacy.py}
  9. +22 −0 django/contrib/formtools/wizard/storage/__init__.py
  10. +93 −0 django/contrib/formtools/wizard/storage/base.py
  11. +32 −0 django/contrib/formtools/wizard/storage/cookie.py
  12. +10 −0 django/contrib/formtools/wizard/storage/exceptions.py
  13. +20 −0 django/contrib/formtools/wizard/storage/session.py
  14. +17 −0 django/contrib/formtools/wizard/templates/formtools/wizard/wizard_form.html
  15. +6 −0 django/contrib/formtools/wizard/tests/__init__.py
  16. +43 −0 django/contrib/formtools/wizard/tests/cookiestoragetests.py
  17. +182 −0 django/contrib/formtools/wizard/tests/formtests.py
  18. +22 −0 django/contrib/formtools/wizard/tests/loadstoragetests.py
  19. +1 −0 django/contrib/formtools/wizard/tests/namedwizardtests/__init__.py
  20. +42 −0 django/contrib/formtools/wizard/tests/namedwizardtests/forms.py
  21. +355 −0 django/contrib/formtools/wizard/tests/namedwizardtests/tests.py
  22. +24 −0 django/contrib/formtools/wizard/tests/namedwizardtests/urls.py
  23. +8 −0 django/contrib/formtools/wizard/tests/sessionstoragetests.py
  24. +76 −0 django/contrib/formtools/wizard/tests/storagetests.py
  25. +1 −0 django/contrib/formtools/wizard/tests/wizardtests/__init__.py
  26. +57 −0 django/contrib/formtools/wizard/tests/wizardtests/forms.py
  27. +248 −0 django/contrib/formtools/wizard/tests/wizardtests/tests.py
  28. +16 −0 django/contrib/formtools/wizard/tests/wizardtests/urls.py
  29. +684 −0 django/contrib/formtools/wizard/views.py
  30. +21 −0 django/utils/functional.py
  31. +4 −0 docs/internals/deprecation.txt
  32. +416 −170 docs/ref/contrib/formtools/form-wizard.txt
  33. +16 −0 docs/releases/1.4.txt
  34. 0 tests/regressiontests/formwizard/__init__.py
  35. +0 −18 tests/regressiontests/formwizard/forms.py
  36. 0 tests/regressiontests/formwizard/models.py
  37. +0 −59 tests/regressiontests/formwizard/tests.py
  38. +0 −6 tests/regressiontests/formwizard/urls.py
  39. +18 −1 tests/regressiontests/utils/functional.py
@@ -1,13 +1,19 @@
import os
+import re
import warnings
-from django import forms, http
+from django import http
from django.conf import settings
from django.contrib.formtools import preview, wizard, utils
from django.test import TestCase
from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils import unittest
+from django.contrib.formtools.wizard.tests import *
+from django.contrib.formtools.tests.forms import *
+
+warnings.filterwarnings('ignore', category=PendingDeprecationWarning,
+ module='django.contrib.formtools.wizard')
success_string = "Done was called!"
@@ -24,12 +30,6 @@ def done(self, request, cleaned_data):
return http.HttpResponse(success_string)
-class TestForm(forms.Form):
- field1 = forms.CharField()
- field1_ = forms.CharField()
- bool1 = forms.BooleanField(required=False)
-
-
class PreviewTests(TestCase):
urls = 'django.contrib.formtools.tests.urls'
@@ -63,7 +63,7 @@ def test_form_get(self):
is created to manage the stage.
"""
- response = self.client.get('/test1/')
+ response = self.client.get('/preview/')
stage = self.input % 1
self.assertContains(response, stage, 1)
self.assertEqual(response.context['custom_context'], True)
@@ -81,7 +81,7 @@ def test_form_preview(self):
# Pass strings for form submittal and add stage variable to
# show we previously saw first stage of the form.
self.test_data.update({'stage': 1})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/preview/', self.test_data)
# Check to confirm stage is set to 2 in output form.
stage = self.input % 2
self.assertContains(response, stage, 1)
@@ -99,11 +99,11 @@ def test_form_submit(self):
# Pass strings for form submittal and add stage variable to
# show we previously saw first stage of the form.
self.test_data.update({'stage':2})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/preview/', self.test_data)
self.assertNotEqual(response.content, success_string)
hash = self.preview.security_hash(None, TestForm(self.test_data))
self.test_data.update({'hash': hash})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/preview/', self.test_data)
self.assertEqual(response.content, success_string)
def test_bool_submit(self):
@@ -122,7 +122,7 @@ def test_bool_submit(self):
self.test_data.update({'stage':2})
hash = self.preview.security_hash(None, TestForm(self.test_data))
self.test_data.update({'hash':hash, 'bool1':u'False'})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/preview/', self.test_data)
self.assertEqual(response.content, success_string)
def test_form_submit_good_hash(self):
@@ -133,11 +133,11 @@ def test_form_submit_good_hash(self):
# Pass strings for form submittal and add stage variable to
# show we previously saw first stage of the form.
self.test_data.update({'stage':2})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/preview/', self.test_data)
self.assertNotEqual(response.content, success_string)
hash = utils.form_hmac(TestForm(self.test_data))
self.test_data.update({'hash': hash})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/preview/', self.test_data)
self.assertEqual(response.content, success_string)
@@ -149,12 +149,12 @@ def test_form_submit_bad_hash(self):
# Pass strings for form submittal and add stage variable to
# show we previously saw first stage of the form.
self.test_data.update({'stage':2})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/preview/', self.test_data)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.content, success_string)
hash = utils.form_hmac(TestForm(self.test_data)) + "bad"
self.test_data.update({'hash': hash})
- response = self.client.post('/test1/', self.test_data)
+ response = self.client.post('/previewpreview/', self.test_data)
self.assertNotEqual(response.content, success_string)
@@ -220,38 +220,14 @@ def test_empty_permitted(self):
self.assertEqual(hash1, hash2)
-class HashTestForm(forms.Form):
- name = forms.CharField()
- bio = forms.CharField()
-
-
-class HashTestBlankForm(forms.Form):
- name = forms.CharField(required=False)
- bio = forms.CharField(required=False)
-
#
# FormWizard tests
#
-
-class WizardPageOneForm(forms.Form):
- field = forms.CharField()
-
-
-class WizardPageTwoForm(forms.Form):
- field = forms.CharField()
-
-class WizardPageTwoAlternativeForm(forms.Form):
- field = forms.CharField()
-
-class WizardPageThreeForm(forms.Form):
- field = forms.CharField()
-
-
-class WizardClass(wizard.FormWizard):
+class TestWizardClass(wizard.FormWizard):
def get_template(self, step):
- return 'formwizard/wizard.html'
+ return 'forms/wizard.html'
def done(self, request, cleaned_data):
return http.HttpResponse(success_string)
@@ -269,6 +245,20 @@ def __init__(self, POST=None):
class WizardTests(TestCase):
urls = 'django.contrib.formtools.tests.urls'
+ input_re = re.compile('name="([^"]+)" value="([^"]+)"')
+ wizard_step_data = (
+ {
+ '0-name': 'Pony',
+ '0-thirsty': '2',
+ },
+ {
+ '1-address1': '123 Main St',
+ '1-address2': 'Djangoland',
+ },
+ {
+ '2-random_crap': 'blah blah',
+ }
+ )
def setUp(self):
self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
@@ -290,21 +280,21 @@ def test_step_starts_at_zero(self):
"""
step should be zero for the first form
"""
- response = self.client.get('/wizard/')
+ response = self.client.get('/wizard1/')
self.assertEqual(0, response.context['step0'])
def test_step_increments(self):
"""
step should be incremented when we go to the next page
"""
- response = self.client.post('/wizard/', {"0-field":"test", "wizard_step":"0"})
+ response = self.client.post('/wizard1/', {"0-field":"test", "wizard_step":"0"})
self.assertEqual(1, response.context['step0'])
def test_bad_hash(self):
"""
Form should not advance if the hash is missing or bad
"""
- response = self.client.post('/wizard/',
+ response = self.client.post('/wizard1/',
{"0-field":"test",
"1-field":"test2",
"wizard_step": "1"})
@@ -319,7 +309,7 @@ def test_good_hash(self):
"1-field": "test2",
"hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
"wizard_step": "1"}
- response = self.client.post('/wizard/', data)
+ response = self.client.post('/wizard1/', data)
self.assertEqual(2, response.context['step0'])
def test_11726(self):
@@ -330,7 +320,7 @@ def test_11726(self):
reached = [False]
that = self
- class WizardWithProcessStep(WizardClass):
+ class WizardWithProcessStep(TestWizardClass):
def process_step(self, request, form, step):
if step == 0:
if self.num_steps() < 2:
@@ -362,7 +352,7 @@ def test_14498(self):
reached = [False]
that = self
- class WizardWithProcessStep(WizardClass):
+ class WizardWithProcessStep(TestWizardClass):
def process_step(self, request, form, step):
that.assertTrue(hasattr(form, 'cleaned_data'))
reached[0] = True
@@ -386,7 +376,7 @@ def test_14576(self):
reached = [False]
that = self
- class Wizard(WizardClass):
+ class Wizard(TestWizardClass):
def done(self, request, form_list):
reached[0] = True
that.assertTrue(len(form_list) == 2)
@@ -409,7 +399,7 @@ def test_15075(self):
reached = [False]
that = self
- class WizardWithProcessStep(WizardClass):
+ class WizardWithProcessStep(TestWizardClass):
def process_step(self, request, form, step):
if step == 0:
self.form_list[1] = WizardPageTwoAlternativeForm
@@ -426,3 +416,39 @@ def process_step(self, request, form, step):
"wizard_step": "1"}
wizard(DummyRequest(POST=data))
self.assertTrue(reached[0])
+
+ def grab_field_data(self, response):
+ """
+ Pull the appropriate field data from the context to pass to the next wizard step
+ """
+ previous_fields = response.context['previous_fields']
+ fields = {'wizard_step': response.context['step0']}
+
+ def grab(m):
+ fields[m.group(1)] = m.group(2)
+ return ''
+
+ self.input_re.sub(grab, previous_fields)
+ return fields
+
+ def check_wizard_step(self, response, step_no):
+ """
+ Helper function to test each step of the wizard
+ - Make sure the call succeeded
+ - Make sure response is the proper step number
+ - return the result from the post for the next step
+ """
+ step_count = len(self.wizard_step_data)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, 'Step %d of %d' % (step_no, step_count))
+
+ data = self.grab_field_data(response)
+ data.update(self.wizard_step_data[step_no - 1])
+
+ return self.client.post('/wizard2/', data)
+
+ def test_9473(self):
+ response = self.client.get('/wizard2/')
+ for step_no in range(1, len(self.wizard_step_data) + 1):
+ response = self.check_wizard_step(response, step_no)
@@ -0,0 +1,43 @@
+from django import forms
+from django.contrib.formtools.wizard import FormWizard
+from django.http import HttpResponse
+
+class Page1(forms.Form):
+ name = forms.CharField(max_length=100)
+ thirsty = forms.NullBooleanField()
+
+class Page2(forms.Form):
+ address1 = forms.CharField(max_length=100)
+ address2 = forms.CharField(max_length=100)
+
+class Page3(forms.Form):
+ random_crap = forms.CharField(max_length=100)
+
+class ContactWizard(FormWizard):
+ def done(self, request, form_list):
+ return HttpResponse("")
+
+class TestForm(forms.Form):
+ field1 = forms.CharField()
+ field1_ = forms.CharField()
+ bool1 = forms.BooleanField(required=False)
+
+class HashTestForm(forms.Form):
+ name = forms.CharField()
+ bio = forms.CharField()
+
+class HashTestBlankForm(forms.Form):
+ name = forms.CharField(required=False)
+ bio = forms.CharField(required=False)
+
+class WizardPageOneForm(forms.Form):
+ field = forms.CharField()
+
+class WizardPageTwoForm(forms.Form):
+ field = forms.CharField()
+
+class WizardPageTwoAlternativeForm(forms.Form):
+ field = forms.CharField()
+
+class WizardPageThreeForm(forms.Form):
+ field = forms.CharField()
@@ -1,13 +1,13 @@
-<html>
- <body>
- <p>Step {{ step }} of {{ step_count }}</p>
- <form action="." method="post">
- <table>
- {{ form }}
- </table>
- <input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
- {{ previous_fields|safe }}
- <input type="submit">
- </form>
- </body>
-</html>
+<html>
+ <body>
+ <p>Step {{ step }} of {{ step_count }}</p>
+ <form action="." method="post">
+ <table>
+ {{ form }}
+ </table>
+ <input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
+ {{ previous_fields|safe }}
+ <input type="submit">
+ </form>
+ </body>
+</html>
@@ -1,9 +0,0 @@
-<p>Step {{ step }} of {{ step_count }}</p>
-<form action="." method="post">{% csrf_token %}
-<table>
-{{ form }}
-</table>
-<input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
-{{ previous_fields|safe }}
-<input type="submit">
-</form>
@@ -3,11 +3,14 @@
"""
from django.conf.urls.defaults import *
-from django.contrib.formtools.tests import *
+from django.contrib.formtools.tests import TestFormPreview, TestWizardClass
+
+from forms import (ContactWizard, Page1, Page2, Page3, TestForm,
+ WizardPageOneForm, WizardPageTwoForm, WizardPageThreeForm)
urlpatterns = patterns('',
- (r'^test1/', TestFormPreview(TestForm)),
- (r'^wizard/$', WizardClass([WizardPageOneForm,
- WizardPageTwoForm,
- WizardPageThreeForm])),
- )
+ url(r'^preview/', TestFormPreview(TestForm)),
+ url(r'^wizard1/$', TestWizardClass(
+ [WizardPageOneForm, WizardPageTwoForm, WizardPageThreeForm])),
+ url(r'^wizard2/$', ContactWizard([Page1, Page2, Page3])),
+)
@@ -0,0 +1 @@
+from django.contrib.formtools.wizard.legacy import FormWizard
@@ -0,0 +1,7 @@
+from django import forms
+
+class ManagementForm(forms.Form):
+ """
+ ``ManagementForm`` is used to keep track of the current wizard step.
+ """
+ current_step = forms.CharField(widget=forms.HiddenInput)
Oops, something went wrong. Retry.

0 comments on commit 632dfa2

Please sign in to comment.