Ticket #16820: 16820-2.diff

File 16820-2.diff, 2.0 KB (added by Claude Paroz, 12 years ago)

Updated patch and simplified tests

  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 6b1be37..260b9a5 100644
    a b class CheckboxInput(Widget):  
    528528        values =  {'true': True, 'false': False}
    529529        if isinstance(value, six.string_types):
    530530            value = values.get(value.lower(), value)
    531         return value
     531        return bool(value)
    532532
    533533    def _has_changed(self, initial, data):
    534534        # Sometimes data or initial could be None or '' which should be the
  • tests/regressiontests/forms/tests/forms.py

    diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py
    index a8a28ba..1c83ed0 100644
    a b class FormsTestCase(TestCase):  
    269269        f = SignupForm({'email': 'test@example.com', 'get_spam': 'false'}, auto_id=False)
    270270        self.assertHTMLEqual(str(f['get_spam']), '<input type="checkbox" name="get_spam" />')
    271271
     272        # A value of '0' should be interpreted as a True value (#16820)
     273        f = SignupForm({'email': 'test@example.com', 'get_spam': '0'})
     274        self.assertTrue(f.is_valid())
     275        self.assertTrue(f.cleaned_data.get('get_spam'))
     276
    272277    def test_widget_output(self):
    273278        # Any Field can have a Widget class passed to its constructor:
    274279        class ContactForm(Form):
  • tests/regressiontests/forms/tests/widgets.py

    diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py
    index 3ea42cf..9924f3b 100644
    a b class FormsWidgetTestCase(TestCase):  
    228228        # checkboxes).
    229229        self.assertFalse(w.value_from_datadict({}, {}, 'testing'))
    230230
     231        value = w.value_from_datadict({'testing': '0'}, {}, 'testing')
     232        self.assertTrue(isinstance(value, bool))
     233        self.assertTrue(value)
     234
    231235        self.assertFalse(w._has_changed(None, None))
    232236        self.assertFalse(w._has_changed(None, ''))
    233237        self.assertFalse(w._has_changed('', None))
Back to Top