Fixed #11385 -- Made forms.DateTimeField accept ISO 8601 date inputs.

Thanks José Padilla for the initial patch, and Carlton Gibson for the
review.
This commit is contained in:
Claude Paroz 2019-10-09 12:08:50 +02:00 committed by Mariusz Felisiak
parent b23fb2c819
commit 1487f16f2d
6 changed files with 66 additions and 18 deletions

View File

@ -25,7 +25,7 @@ from django.forms.widgets import (
URLInput,
)
from django.utils import formats
from django.utils.dateparse import parse_duration
from django.utils.dateparse import parse_datetime, parse_duration
from django.utils.duration import duration_string
from django.utils.ipv6 import clean_ipv6_address
from django.utils.regex_helper import _lazy_re_compile
@ -459,6 +459,11 @@ class DateTimeField(BaseTemporalField):
if isinstance(value, datetime.date):
result = datetime.datetime(value.year, value.month, value.day)
return from_current_timezone(result)
try:
result = parse_datetime(value.strip())
except ValueError:
raise ValidationError(self.error_messages['invalid'], code='invalid')
if not result:
result = super().to_python(value)
return from_current_timezone(result)

View File

@ -490,7 +490,19 @@ For each field, we describe the default widget used if you don't specify
.. attribute:: input_formats
A list of formats used to attempt to convert a string to a valid
``datetime.datetime`` object.
``datetime.datetime`` object, in addition to ISO 8601 formats.
The field always accepts strings in ISO 8601 formatted dates or similar
recognized by :func:`~django.utils.dateparse.parse_datetime`. Some examples
are::
* '2006-10-25 14:30:59'
* '2006-10-25T14:30:59'
* '2006-10-25 14:30'
* '2006-10-25T14:30'
* '2006-10-25T14:30Z'
* '2006-10-25T14:30+02:00'
* '2006-10-25'
If no ``input_formats`` argument is provided, the default input formats are
taken from :setting:`DATETIME_INPUT_FORMATS` if :setting:`USE_L10N` is
@ -498,6 +510,11 @@ For each field, we describe the default widget used if you don't specify
if localization is enabled. See also :doc:`format localization
</topics/i18n/formatting>`.
.. versionchanged:: 3.1
Support for ISO 8601 date string parsing (including optional timezone)
was added.
``DecimalField``
----------------

View File

@ -179,6 +179,10 @@ Forms
to access model instances. See :ref:`iterating-relationship-choices` for
details.
* :class:`django.forms.DateTimeField` now accepts dates in a subset of ISO 8601
datetime formats, including optional timezone (e.g. ``2019-10-10T06:47``,
``2019-10-10T06:47:23+04:00``, or ``2019-10-10T06:47:23Z``).
Generic Views
~~~~~~~~~~~~~

View File

@ -2,6 +2,7 @@ from datetime import date, datetime
from django.forms import DateTimeField, ValidationError
from django.test import SimpleTestCase
from django.utils.timezone import get_fixed_timezone, utc
class DateTimeFieldTest(SimpleTestCase):
@ -31,6 +32,19 @@ class DateTimeFieldTest(SimpleTestCase):
('10/25/06 14:30:00', datetime(2006, 10, 25, 14, 30)),
('10/25/06 14:30', datetime(2006, 10, 25, 14, 30)),
('10/25/06', datetime(2006, 10, 25, 0, 0)),
# ISO 8601 formats.
(
'2014-09-23T22:34:41.614804',
datetime(2014, 9, 23, 22, 34, 41, 614804),
),
('2014-09-23T22:34:41', datetime(2014, 9, 23, 22, 34, 41)),
('2014-09-23T22:34', datetime(2014, 9, 23, 22, 34)),
('2014-09-23', datetime(2014, 9, 23, 0, 0)),
('2014-09-23T22:34Z', datetime(2014, 9, 23, 22, 34, tzinfo=utc)),
(
'2014-09-23T22:34+07:00',
datetime(2014, 9, 23, 22, 34, tzinfo=get_fixed_timezone(420)),
),
# Whitespace stripping.
(' 2006-10-25 14:30:45 ', datetime(2006, 10, 25, 14, 30, 45)),
(' 2006-10-25 ', datetime(2006, 10, 25, 0, 0)),
@ -39,6 +53,11 @@ class DateTimeFieldTest(SimpleTestCase):
(' 10/25/2006 ', datetime(2006, 10, 25, 0, 0)),
(' 10/25/06 14:30:45 ', datetime(2006, 10, 25, 14, 30, 45)),
(' 10/25/06 ', datetime(2006, 10, 25, 0, 0)),
(
' 2014-09-23T22:34:41.614804 ',
datetime(2014, 9, 23, 22, 34, 41, 614804),
),
(' 2014-09-23T22:34Z ', datetime(2014, 9, 23, 22, 34, tzinfo=utc)),
]
f = DateTimeField()
for value, expected_datetime in tests:
@ -54,9 +73,11 @@ class DateTimeFieldTest(SimpleTestCase):
f.clean('2006-10-25 4:30 p.m.')
with self.assertRaisesMessage(ValidationError, msg):
f.clean(' ')
with self.assertRaisesMessage(ValidationError, msg):
f.clean('2014-09-23T28:23')
f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
with self.assertRaisesMessage(ValidationError, msg):
f.clean('2006-10-25 14:30:45')
f.clean('2006.10.25 14:30:45')
def test_datetimefield_clean_input_formats(self):
tests = [
@ -72,6 +93,8 @@ class DateTimeFieldTest(SimpleTestCase):
datetime(2006, 10, 25, 14, 30, 59, 200),
),
('2006 10 25 2:30 PM', datetime(2006, 10, 25, 14, 30)),
# ISO-like formats are always accepted.
('2006-10-25 14:30:45', datetime(2006, 10, 25, 14, 30, 45)),
)),
('%Y.%m.%d %H:%M:%S.%f', (
(

View File

@ -703,7 +703,7 @@ class LocalizedDateTimeTests(SimpleTestCase):
f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"], localize=True)
# Parse a date in an unaccepted format; get an error
with self.assertRaises(forms.ValidationError):
f.clean('2010-12-21 13:30:05')
f.clean('2010/12/21 13:30:05')
with self.assertRaises(forms.ValidationError):
f.clean('1:30:05 PM 21/12/2010')
with self.assertRaises(forms.ValidationError):
@ -711,8 +711,12 @@ class LocalizedDateTimeTests(SimpleTestCase):
# Parse a date in a valid format, get a parsed result
result = f.clean('13.30.05 12.21.2010')
self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))
self.assertEqual(datetime(2010, 12, 21, 13, 30, 5), result)
# ISO format is always valid.
self.assertEqual(
f.clean('2010-12-21 13:30:05'),
datetime(2010, 12, 21, 13, 30, 5),
)
# The parsed result does a round trip to the same format
text = f.widget.format_value(result)
self.assertEqual(text, "21.12.2010 13:30:05")
@ -733,7 +737,7 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase):
f = forms.DateTimeField()
# Parse a date in an unaccepted format; get an error
with self.assertRaises(forms.ValidationError):
f.clean('2010-12-21 13:30:05')
f.clean('2010/12/21 13:30:05')
# Parse a date in a valid format, get a parsed result
result = f.clean('1:30:05 PM 21/12/2010')
@ -756,7 +760,7 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase):
f = forms.DateTimeField(localize=True)
# Parse a date in an unaccepted format; get an error
with self.assertRaises(forms.ValidationError):
f.clean('2010-12-21 13:30:05')
f.clean('2010/12/21 13:30:05')
# Parse a date in a valid format, get a parsed result
result = f.clean('1:30:05 PM 21/12/2010')
@ -781,7 +785,7 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase):
with self.assertRaises(forms.ValidationError):
f.clean('13:30:05 21.12.2010')
with self.assertRaises(forms.ValidationError):
f.clean('2010-12-21 13:30:05')
f.clean('2010/12/21 13:30:05')
# Parse a date in a valid format, get a parsed result
result = f.clean('12.21.2010 13:30:05')
@ -806,7 +810,7 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase):
with self.assertRaises(forms.ValidationError):
f.clean('13:30:05 21.12.2010')
with self.assertRaises(forms.ValidationError):
f.clean('2010-12-21 13:30:05')
f.clean('2010/12/21 13:30:05')
# Parse a date in a valid format, get a parsed result
result = f.clean('12.21.2010 13:30:05')
@ -877,7 +881,7 @@ class SimpleDateTimeFormatTests(SimpleTestCase):
f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"])
# Parse a date in an unaccepted format; get an error
with self.assertRaises(forms.ValidationError):
f.clean('2010-12-21 13:30:05')
f.clean('2010/12/21 13:30:05')
# Parse a date in a valid format, get a parsed result
result = f.clean('1:30:05 PM 21.12.2010')
@ -900,7 +904,7 @@ class SimpleDateTimeFormatTests(SimpleTestCase):
f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"], localize=True)
# Parse a date in an unaccepted format; get an error
with self.assertRaises(forms.ValidationError):
f.clean('2010-12-21 13:30:05')
f.clean('2010/12/21 13:30:05')
# Parse a date in a valid format, get a parsed result
result = f.clean('1:30:05 PM 21.12.2010')

View File

@ -1081,11 +1081,6 @@ class NewFormsTests(TestCase):
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
def test_form_with_explicit_timezone(self):
form = EventForm({'dt': '2011-09-01 17:20:30+07:00'})
# Datetime inputs formats don't allow providing a time zone.
self.assertFalse(form.is_valid())
def test_form_with_non_existent_time(self):
with timezone.override(pytz.timezone('Europe/Paris')):
form = EventForm({'dt': '2011-03-27 02:30:00'})