Ticket #9473: fix_9473_2.diff

File fix_9473_2.diff, 4.8 KB (added by Keith Bussell, 16 years ago)

Same fix, new test

  • django/forms/widgets.py

     
    422422
    423423    def value_from_datadict(self, data, files, name):
    424424        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)
    426426
    427427    def _has_changed(self, initial, data):
    428428        # Sometimes data or initial could be None or u'' which should be the
  • tests/regressiontests/formwizard/forms.py

     
     1from django import forms
     2from django.contrib.formtools.wizard import FormWizard
     3from django.http import HttpResponse
     4
     5class Page1(forms.Form):
     6    name = forms.CharField(max_length=100)
     7    thirsty = forms.NullBooleanField()
     8
     9class Page2(forms.Form):
     10    address1 = forms.CharField(max_length=100)
     11    address2 = forms.CharField(max_length=100)
     12   
     13class Page3(forms.Form):
     14    random_crap = forms.CharField(max_length=100)
     15   
     16class ContactWizard(FormWizard):
     17    def done(self, request, form_list):
     18        return HttpResponse("")
  • tests/regressiontests/formwizard/templates/forms/wizard.html

     
     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>
     14 No newline at end of file
  • tests/regressiontests/formwizard/tests.py

     
     1import re
     2from django import forms
     3from django.test import TestCase
     4
     5class 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)
  • tests/regressiontests/formwizard/urls.py

     
     1from django.conf.urls.defaults import *
     2from forms import ContactWizard, Page1, Page2, Page3
     3
     4urlpatterns = patterns('',
     5    url(r'^wiz/$', ContactWizard([Page1, Page2, Page3])),
     6    )
Back to Top