Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9473: FormWizard now works with NullBooleanFields. As a bonus,…

… we now have the beginnings of a test suite for FormWizard. Thanks, Keith Bussell.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10316 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit da0c6908e8b55af37862eb643365ac6fb83e948b 1 parent a274ad9
Jacob Kaplan-Moss authored April 01, 2009
2  django/forms/widgets.py
@@ -422,7 +422,7 @@ def render(self, name, value, attrs=None, choices=()):
422 422
 
423 423
     def value_from_datadict(self, data, files, name):
424 424
         value = data.get(name, None)
425  
-        return {u'2': True, u'3': False, True: True, False: False}.get(value, None)
  425
+        return {u'2': True, u'3': False, 'True': True, 'False': False}.get(value, None)
426 426
 
427 427
     def _has_changed(self, initial, data):
428 428
         # Sometimes data or initial could be None or u'' which should be the
0  tests/regressiontests/formwizard/__init__.py
No changes.
18  tests/regressiontests/formwizard/forms.py
... ...
@@ -0,0 +1,18 @@
  1
+from django import forms
  2
+from django.contrib.formtools.wizard import FormWizard
  3
+from django.http import HttpResponse
  4
+
  5
+class Page1(forms.Form):
  6
+    name = forms.CharField(max_length=100)
  7
+    thirsty = forms.NullBooleanField()
  8
+
  9
+class Page2(forms.Form):
  10
+    address1 = forms.CharField(max_length=100)
  11
+    address2 = forms.CharField(max_length=100)
  12
+    
  13
+class Page3(forms.Form):
  14
+    random_crap = forms.CharField(max_length=100)
  15
+    
  16
+class ContactWizard(FormWizard):
  17
+    def done(self, request, form_list):
  18
+        return HttpResponse("")
0  tests/regressiontests/formwizard/models.py
No changes.
13  tests/regressiontests/formwizard/templates/forms/wizard.html
... ...
@@ -0,0 +1,13 @@
  1
+<html>
  2
+  <body>
  3
+    <p>Step {{ step }} of {{ step_count }}</p>
  4
+    <form action="." method="post">
  5
+    <table>
  6
+    {{ form }}
  7
+    </table>
  8
+    <input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
  9
+    {{ previous_fields|safe }}
  10
+    <input type="submit">
  11
+    </form>
  12
+  </body>
  13
+</html>
59  tests/regressiontests/formwizard/tests.py
... ...
@@ -0,0 +1,59 @@
  1
+import re
  2
+from django import forms
  3
+from django.test import TestCase
  4
+
  5
+class FormWizardWithNullBooleanField(TestCase):
  6
+    urls = 'regressiontests.formwizard.urls'
  7
+
  8
+    input_re = re.compile('name="([^"]+)" value="([^"]+)"')
  9
+
  10
+    wizard_url = '/wiz/'
  11
+    wizard_step_data = (
  12
+        {
  13
+            '0-name': 'Pony',
  14
+            '0-thirsty': '2',
  15
+        },
  16
+        {
  17
+            '1-address1': '123 Main St',
  18
+            '1-address2': 'Djangoland',
  19
+        },
  20
+        {
  21
+            '2-random_crap': 'blah blah',
  22
+        }
  23
+    )
  24
+
  25
+    def grabFieldData(self, response):
  26
+        """
  27
+        Pull the appropriate field data from the context to pass to the next wizard step
  28
+        """
  29
+        previous_fields = response.context['previous_fields']
  30
+        fields = {'wizard_step': response.context['step0']}
  31
+        
  32
+        def grab(m):
  33
+            fields[m.group(1)] = m.group(2)
  34
+            return ''
  35
+        
  36
+        self.input_re.sub(grab, previous_fields)
  37
+        return fields
  38
+
  39
+    def checkWizardStep(self, response, step_no):
  40
+        """
  41
+        Helper function to test each step of the wizard
  42
+        - Make sure the call succeeded
  43
+        - Make sure response is the proper step number
  44
+        - return the result from the post for the next step
  45
+        """
  46
+        step_count = len(self.wizard_step_data)
  47
+
  48
+        self.assertEqual(response.status_code, 200)
  49
+        self.assertContains(response, 'Step %d of %d' % (step_no, step_count))
  50
+
  51
+        data = self.grabFieldData(response)
  52
+        data.update(self.wizard_step_data[step_no - 1])
  53
+
  54
+        return self.client.post(self.wizard_url, data)
  55
+        
  56
+    def testWizard(self):
  57
+        response = self.client.get(self.wizard_url)
  58
+        for step_no in range(1, len(self.wizard_step_data) + 1):
  59
+            response = self.checkWizardStep(response, step_no)
6  tests/regressiontests/formwizard/urls.py
... ...
@@ -0,0 +1,6 @@
  1
+from django.conf.urls.defaults import *
  2
+from forms import ContactWizard, Page1, Page2, Page3
  3
+
  4
+urlpatterns = patterns('',
  5
+    url(r'^wiz/$', ContactWizard([Page1, Page2, Page3])),
  6
+    )

0 notes on commit da0c690

Please sign in to comment.
Something went wrong with that request. Please try again.