Ticket #5714: fix5714.patch
File fix5714.patch, 4.3 KB (added by , 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): 355 355 return value.date() 356 356 if isinstance(value, datetime.date): 357 357 return value 358 if isinstance(value, basestring): 359 value = value.strip() 358 360 for format in self.input_formats or formats.get_format('DATE_INPUT_FORMATS'): 359 361 try: 360 362 return datetime.date(*time.strptime(value, format)[:3]) … … class TimeField(Field): 381 383 return None 382 384 if isinstance(value, datetime.time): 383 385 return value 386 if isinstance(value, basestring): 387 value = value.strip() 384 388 for format in self.input_formats or formats.get_format('TIME_INPUT_FORMATS'): 385 389 try: 386 390 return datetime.time(*time.strptime(value, format)[3:6]) … … class DateTimeField(Field): 417 421 if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES: 418 422 return None 419 423 value = '%s %s' % tuple(value) 424 if isinstance(value, basestring): 425 value = value.strip() 420 426 for format in self.input_formats or formats.get_format('DATETIME_INPUT_FORMATS'): 421 427 try: 422 428 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): 280 280 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06') 281 281 self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) 282 282 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 283 291 def test_datefield_20(self): 284 292 f = DateField(required=False) 285 293 self.assertEqual(None, f.clean(None)) … … class FieldsTests(TestCase): 307 315 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello') 308 316 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.') 309 317 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 310 322 def test_timefield_23(self): 311 323 f = TimeField(input_formats=['%I:%M %p']) 312 324 self.assertEqual(datetime.time(14, 25), f.clean(datetime.time(14, 25))) … … class FieldsTests(TestCase): 338 350 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello') 339 351 self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.') 340 352 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 341 362 def test_datetimefield_25(self): 342 363 f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) 343 364 self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(datetime.date(2006, 10, 25)))