Ticket #5714: fix5714.patch

File fix5714.patch, 4.3 KB (added by Matthias Kestenholz, 14 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 03455e0..8d40cb5 100644
    a b class DateField(Field):  
    355355            return value.date()
    356356        if isinstance(value, datetime.date):
    357357            return value
     358        if isinstance(value, basestring):
     359            value = value.strip()
    358360        for format in self.input_formats or formats.get_format('DATE_INPUT_FORMATS'):
    359361            try:
    360362                return datetime.date(*time.strptime(value, format)[:3])
    class TimeField(Field):  
    381383            return None
    382384        if isinstance(value, datetime.time):
    383385            return value
     386        if isinstance(value, basestring):
     387            value = value.strip()
    384388        for format in self.input_formats or formats.get_format('TIME_INPUT_FORMATS'):
    385389            try:
    386390                return datetime.time(*time.strptime(value, format)[3:6])
    class DateTimeField(Field):  
    417421            if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES:
    418422                return None
    419423            value = '%s %s' % tuple(value)
     424        if isinstance(value, basestring):
     425            value = value.strip()
    420426        for format in self.input_formats or formats.get_format('DATETIME_INPUT_FORMATS'):
    421427            try:
    422428                return datetime.datetime(*time.strptime(value, format)[:6])
  • tests/regressiontests/forms/fields.py

    diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
    index d29e2e8..f94a8c3 100644
    a b class FieldsTests(TestCase):  
    280280        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06')
    281281        self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)
    282282
     283        # Test whitespace stripping behavior (#5714)
     284        self.assertEqual(datetime.date(2006, 10, 25), f.clean(' 10/25/2006 '))
     285        self.assertEqual(datetime.date(2006, 10, 25), f.clean(' 10/25/06 '))
     286        self.assertEqual(datetime.date(2006, 10, 25), f.clean(' Oct 25   2006 '))
     287        self.assertEqual(datetime.date(2006, 10, 25), f.clean(' October  25 2006 '))
     288        self.assertEqual(datetime.date(2006, 10, 25), f.clean(' October 25, 2006 '))
     289        self.assertEqual(datetime.date(2006, 10, 25), f.clean(' 25 October 2006 '))
     290
    283291    def test_datefield_20(self):
    284292        f = DateField(required=False)
    285293        self.assertEqual(None, f.clean(None))
    class FieldsTests(TestCase):  
    307315        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello')
    308316        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.')
    309317
     318        # Test whitespace stripping behavior (#5714)
     319        self.assertEqual(datetime.time(14, 25), f.clean(' 14:25 '))
     320        self.assertEqual(datetime.time(14, 25, 59), f.clean(' 14:25:59 '))
     321
    310322    def test_timefield_23(self):
    311323        f = TimeField(input_formats=['%I:%M %p'])
    312324        self.assertEqual(datetime.time(14, 25), f.clean(datetime.time(14, 25)))
    class FieldsTests(TestCase):  
    338350        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello')
    339351        self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.')
    340352
     353        # Test whitespace stripping behavior (#5714)
     354        self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean(' 2006-10-25   14:30:45 '))
     355        self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 2006-10-25 '))
     356        self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean(' 10/25/2006 14:30:45 '))
     357        self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean(' 10/25/2006 14:30 '))
     358        self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 10/25/2006 '))
     359        self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean(' 10/25/06 14:30:45 '))
     360        self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 10/25/06 '))
     361
    341362    def test_datetimefield_25(self):
    342363        f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
    343364        self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(datetime.date(2006, 10, 25)))
Back to Top