Fixed -- Fixed crash with empty DurationField form field.

This commit is contained in:
Michael Angeletti 2015-02-13 17:12:23 -05:00 committed by Tim Graham
parent 1791a7e75a
commit 8a21d25033
2 changed files with 12 additions and 2 deletions
django/forms
tests/forms_tests/tests

View File

@ -514,6 +514,7 @@ class DurationField(Field):
} }
def prepare_value(self, value): def prepare_value(self, value):
if isinstance(value, datetime.timedelta):
return duration_string(value) return duration_string(value)
def to_python(self, value): def to_python(self, value):

View File

@ -48,6 +48,7 @@ from django.test import SimpleTestCase, ignore_warnings
from django.utils import formats, six, translation from django.utils import formats, six, translation
from django.utils._os import upath from django.utils._os import upath
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.duration import duration_string
try: try:
from PIL import Image from PIL import Image
@ -614,7 +615,7 @@ class FieldsTests(SimpleTestCase):
d = datetime.datetime(2006, 9, 17, 14, 30, 0) d = datetime.datetime(2006, 9, 17, 14, 30, 0)
self.assertFalse(f.has_changed(d, '2006 09 17 2:30 PM')) self.assertFalse(f.has_changed(d, '2006 09 17 2:30 PM'))
# RegexField ################################################################## # DurationField ###########################################################
def test_durationfield_1(self): def test_durationfield_1(self):
f = DurationField() f = DurationField()
@ -642,6 +643,14 @@ class FieldsTests(SimpleTestCase):
str(f['duration']) str(f['duration'])
) )
def test_durationfield_prepare_value(self):
field = DurationField()
td = datetime.timedelta(minutes=15, seconds=30)
self.assertEqual(field.prepare_value(td), duration_string(td))
self.assertIsNone(field.prepare_value(None))
# RegexField ##################################################################
def test_regexfield_1(self): def test_regexfield_1(self):
f = RegexField('^[0-9][A-F][0-9]$') f = RegexField('^[0-9][A-F][0-9]$')
self.assertEqual('2A2', f.clean('2A2')) self.assertEqual('2A2', f.clean('2A2'))