Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.5.x] Fixed #19015 -- Add ISO input formats to all formats

Backport of 349c4c3 from master.
  • Loading branch information...
commit 1b93499849f497af405de113355681e3af68d50b 1 parent ae549cc
@claudep claudep authored
View
4 django/conf/locale/de/formats.py
@@ -17,16 +17,12 @@
# 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'
)
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 = '.'
View
16 django/utils/formats.py
@@ -16,6 +16,17 @@
_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:
View
11 tests/regressiontests/forms/tests/input_formats.py
@@ -9,7 +9,8 @@
@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 @@ def test_localized_timeField_with_inputformat(self):
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 @@ def test_dateField(self):
# 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 @@ def test_dateTimeField(self):
# 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))
Please sign in to comment.
Something went wrong with that request. Please try again.