From 349c4c37f8be401592793b47a591ff079c4a7fbb Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 13 Oct 2012 12:21:35 +0200 Subject: [PATCH] Fixed #19015 -- Add ISO input formats to all formats --- django/conf/locale/de/formats.py | 4 ---- django/utils/formats.py | 16 ++++++++++++++++ .../regressiontests/forms/tests/input_formats.py | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/django/conf/locale/de/formats.py b/django/conf/locale/de/formats.py index 79d555b989..97b99c9245 100644 --- a/django/conf/locale/de/formats.py +++ b/django/conf/locale/de/formats.py @@ -17,7 +17,6 @@ FIRST_DAY_OF_WEEK = 1 # Monday # see http://docs.python.org/library/datetime.html#strftime-strptime-behavior DATE_INPUT_FORMATS = ( '%d.%m.%Y', '%d.%m.%y', # '25.10.2006', '25.10.06' - '%Y-%m-%d', '%y-%m-%d', # '2006-10-25', '06-10-25' # '%d. %B %Y', '%d. %b. %Y', # '25. October 2006', '25. Oct. 2006' ) TIME_INPUT_FORMATS = ( @@ -28,9 +27,6 @@ DATETIME_INPUT_FORMATS = ( '%d.%m.%Y %H:%M:%S', # '25.10.2006 14:30:59' '%d.%m.%Y %H:%M', # '25.10.2006 14:30' '%d.%m.%Y', # '25.10.2006' - '%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' - '%Y-%m-%d %H:%M', # '2006-10-25 14:30' - '%Y-%m-%d', # '2006-10-25' ) DECIMAL_SEPARATOR = ',' THOUSAND_SEPARATOR = '.' diff --git a/django/utils/formats.py b/django/utils/formats.py index 555982eede..03b9918edf 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -16,6 +16,17 @@ from django.utils.translation import get_language, to_locale, check_for_language _format_cache = {} _format_modules_cache = {} +ISO_INPUT_FORMATS = { + 'DATE_INPUT_FORMATS': ('%Y-%m-%d',), + 'TIME_INPUT_FORMATS': ('%H:%M:%S', '%H:%M'), + 'DATETIME_INPUT_FORMATS': ( + '%Y-%m-%d %H:%M:%S', + '%Y-%m-%d %H:%M:%S.%f', + '%Y-%m-%d %H:%M', + '%Y-%m-%d' + ), +} + def reset_format_cache(): """Clear any cached formats. @@ -82,6 +93,11 @@ def get_format(format_type, lang=None, use_l10n=None): for module in get_format_modules(lang): try: val = getattr(module, format_type) + for iso_input in ISO_INPUT_FORMATS.get(format_type, ()): + if iso_input not in val: + if isinstance(val, tuple): + val = list(val) + val.append(iso_input) _format_cache[cache_key] = val return val except AttributeError: diff --git a/tests/regressiontests/forms/tests/input_formats.py b/tests/regressiontests/forms/tests/input_formats.py index d34125fa29..7a305dbc2b 100644 --- a/tests/regressiontests/forms/tests/input_formats.py +++ b/tests/regressiontests/forms/tests/input_formats.py @@ -9,7 +9,8 @@ from django.test import SimpleTestCase @override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"], USE_L10N=True) class LocalizedTimeTests(SimpleTestCase): def setUp(self): - activate('de') + # nl/formats.py has customized TIME_INPUT_FORMATS + activate('nl') def tearDown(self): deactivate() @@ -92,7 +93,7 @@ class LocalizedTimeTests(SimpleTestCase): result = f.clean('13.30.05') self.assertEqual(result, time(13,30,5)) - # # Check that the parsed result does a round trip to the same format + # Check that the parsed result does a round trip to the same format text = f.widget._format_value(result) self.assertEqual(text, "13:30:05") @@ -302,6 +303,9 @@ class LocalizedDateTests(SimpleTestCase): # Parse a date in an unaccepted format; get an error self.assertRaises(forms.ValidationError, f.clean, '21/12/2010') + # ISO formats are accepted, even if not specified in formats.py + self.assertEqual(f.clean('2010-12-21'), date(2010,12,21)) + # Parse a date in a valid format, get a parsed result result = f.clean('21.12.2010') self.assertEqual(result, date(2010,12,21)) @@ -585,6 +589,9 @@ class LocalizedDateTimeTests(SimpleTestCase): # Parse a date in an unaccepted format; get an error self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010') + # ISO formats are accepted, even if not specified in formats.py + self.assertEqual(f.clean('2010-12-21 13:30:05'), datetime(2010,12,21,13,30,5)) + # Parse a date in a valid format, get a parsed result result = f.clean('21.12.2010 13:30:05') self.assertEqual(result, datetime(2010,12,21,13,30,5))