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

Backport of 349c4c37f8 from master.
This commit is contained in:
Claude Paroz 2012-10-13 12:21:35 +02:00
parent ae549cc1dd
commit 1b93499849
3 changed files with 25 additions and 6 deletions

View File

@ -17,16 +17,12 @@ 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'
)
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 File

@ -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:

View File

@ -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))