From 7cbbd782e1b6c5c05be382b0c478e81a77a13013 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 1 Apr 2009 15:07:31 +0000 Subject: [PATCH] [1.0.X] Fixed #9473: FormWizard now works with NullBooleanFields. As a bonus, we now have the beginnings of a test suite for FormWizard. Thanks, Keith Bussell. Backport of r10316 and r10319 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10321 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/widgets.py | 7 ++- tests/regressiontests/formwizard/__init__.py | 0 tests/regressiontests/formwizard/forms.py | 18 ++++++ tests/regressiontests/formwizard/models.py | 0 .../formwizard/templates/forms/wizard.html | 13 ++++ tests/regressiontests/formwizard/tests.py | 59 +++++++++++++++++++ tests/regressiontests/formwizard/urls.py | 6 ++ 7 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tests/regressiontests/formwizard/__init__.py create mode 100644 tests/regressiontests/formwizard/forms.py create mode 100644 tests/regressiontests/formwizard/models.py create mode 100644 tests/regressiontests/formwizard/templates/forms/wizard.html create mode 100644 tests/regressiontests/formwizard/tests.py create mode 100644 tests/regressiontests/formwizard/urls.py diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 738da20d1c..ab1142c180 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -400,7 +400,12 @@ class NullBooleanSelect(Select): def value_from_datadict(self, data, files, name): value = data.get(name, None) - return {u'2': True, u'3': False, True: True, False: False}.get(value, None) + return {u'2': True, + True: True, + 'True': True, + u'3': False, + 'False': False, + False: False}.get(value, None) def _has_changed(self, initial, data): # Sometimes data or initial could be None or u'' which should be the diff --git a/tests/regressiontests/formwizard/__init__.py b/tests/regressiontests/formwizard/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/formwizard/forms.py b/tests/regressiontests/formwizard/forms.py new file mode 100644 index 0000000000..f458eda4ac --- /dev/null +++ b/tests/regressiontests/formwizard/forms.py @@ -0,0 +1,18 @@ +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("") diff --git a/tests/regressiontests/formwizard/models.py b/tests/regressiontests/formwizard/models.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/formwizard/templates/forms/wizard.html b/tests/regressiontests/formwizard/templates/forms/wizard.html new file mode 100644 index 0000000000..a31378fc26 --- /dev/null +++ b/tests/regressiontests/formwizard/templates/forms/wizard.html @@ -0,0 +1,13 @@ + + +

Step {{ step }} of {{ step_count }}

+
+ + {{ form }} +
+ + {{ previous_fields|safe }} + +
+ + \ No newline at end of file diff --git a/tests/regressiontests/formwizard/tests.py b/tests/regressiontests/formwizard/tests.py new file mode 100644 index 0000000000..0c94d2e276 --- /dev/null +++ b/tests/regressiontests/formwizard/tests.py @@ -0,0 +1,59 @@ +import re +from django import forms +from django.test import TestCase + +class FormWizardWithNullBooleanField(TestCase): + urls = 'regressiontests.formwizard.urls' + + input_re = re.compile('name="([^"]+)" value="([^"]+)"') + + wizard_url = '/wiz/' + wizard_step_data = ( + { + '0-name': 'Pony', + '0-thirsty': '2', + }, + { + '1-address1': '123 Main St', + '1-address2': 'Djangoland', + }, + { + '2-random_crap': 'blah blah', + } + ) + + def grabFieldData(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 checkWizardStep(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.grabFieldData(response) + data.update(self.wizard_step_data[step_no - 1]) + + return self.client.post(self.wizard_url, data) + + def testWizard(self): + response = self.client.get(self.wizard_url) + for step_no in range(1, len(self.wizard_step_data) + 1): + response = self.checkWizardStep(response, step_no) diff --git a/tests/regressiontests/formwizard/urls.py b/tests/regressiontests/formwizard/urls.py new file mode 100644 index 0000000000..d964bc6505 --- /dev/null +++ b/tests/regressiontests/formwizard/urls.py @@ -0,0 +1,6 @@ +from django.conf.urls.defaults import * +from forms import ContactWizard, Page1, Page2, Page3 + +urlpatterns = patterns('', + url(r'^wiz/$', ContactWizard([Page1, Page2, Page3])), + )