Fixed #4542: added a generic localflavor DateField. Thanks, Nick Lane.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6208 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
55d6aebfec
commit
525af7d580
|
@ -0,0 +1,38 @@
|
||||||
|
from django import newforms as forms
|
||||||
|
|
||||||
|
DEFAULT_DATE_INPUT_FORMATS = (
|
||||||
|
'%Y-%m-%d', '%d/%m/%Y', '%d/%m/%y', # '2006-10-25', '25/10/2006', '25/10/06'
|
||||||
|
'%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006'
|
||||||
|
'%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006'
|
||||||
|
'%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006'
|
||||||
|
'%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006'
|
||||||
|
)
|
||||||
|
|
||||||
|
DEFAULT_DATETIME_INPUT_FORMATS = (
|
||||||
|
'%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'
|
||||||
|
'%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'
|
||||||
|
'%d/%m/%y %H:%M:%S', # '25/10/06 14:30:59'
|
||||||
|
'%d/%m/%y %H:%M', # '25/10/06 14:30'
|
||||||
|
'%d/%m/%y', # '25/10/06'
|
||||||
|
)
|
||||||
|
|
||||||
|
class DateField(forms.DateField):
|
||||||
|
"""
|
||||||
|
A date input field which uses non-US date input formats by default.
|
||||||
|
"""
|
||||||
|
def __init__(self, input_formats=None, *args, **kwargs):
|
||||||
|
input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
|
||||||
|
super(DateField, self).__init__(input_formats=input_formats, *args, **kwargs)
|
||||||
|
|
||||||
|
class DateTimeField(forms.DateTimeField):
|
||||||
|
"""
|
||||||
|
A date and time input field which uses non-US date and time input formats
|
||||||
|
by default.
|
||||||
|
"""
|
||||||
|
def __init__(self, input_formats=None, *args, **kwargs):
|
||||||
|
input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
|
||||||
|
super(DateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs)
|
|
@ -2036,4 +2036,162 @@ Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.']
|
ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.']
|
||||||
|
|
||||||
|
## Generic DateField ##########################################################
|
||||||
|
|
||||||
|
>>> from django.contrib.localflavor.generic.forms import *
|
||||||
|
|
||||||
|
A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
|
||||||
|
appropriate.
|
||||||
|
|
||||||
|
>>> import datetime
|
||||||
|
>>> f = DateField()
|
||||||
|
>>> f.clean(datetime.date(2006, 10, 25))
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('2006-10-25')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('25/10/2006')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('25/10/06')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('Oct 25 2006')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('October 25 2006')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('October 25, 2006')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('25 October 2006')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('25 October, 2006')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('2006-4-31')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date.']
|
||||||
|
>>> f.clean('200a-10-25')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date.']
|
||||||
|
>>> f.clean('10/25/06')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date.']
|
||||||
|
>>> f.clean(None)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'This field is required.']
|
||||||
|
|
||||||
|
>>> f = DateField(required=False)
|
||||||
|
>>> f.clean(None)
|
||||||
|
>>> repr(f.clean(None))
|
||||||
|
'None'
|
||||||
|
>>> f.clean('')
|
||||||
|
>>> repr(f.clean(''))
|
||||||
|
'None'
|
||||||
|
|
||||||
|
DateField accepts an optional input_formats parameter:
|
||||||
|
>>> f = DateField(input_formats=['%Y %m %d'])
|
||||||
|
>>> f.clean(datetime.date(2006, 10, 25))
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
>>> f.clean('2006 10 25')
|
||||||
|
datetime.date(2006, 10, 25)
|
||||||
|
|
||||||
|
The input_formats parameter overrides all default input formats,
|
||||||
|
so the default formats won't work unless you specify them:
|
||||||
|
>>> f.clean('2006-10-25')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date.']
|
||||||
|
>>> f.clean('25/10/2006')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date.']
|
||||||
|
>>> f.clean('25/10/06')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date.']
|
||||||
|
|
||||||
|
## Generic DateTimeField ######################################################
|
||||||
|
|
||||||
|
A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
|
||||||
|
appropriate.
|
||||||
|
|
||||||
|
>>> import datetime
|
||||||
|
>>> f = DateTimeField()
|
||||||
|
>>> f.clean(datetime.date(2006, 10, 25))
|
||||||
|
datetime.datetime(2006, 10, 25, 0, 0)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30, 59)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
|
||||||
|
>>> f.clean('2006-10-25 14:30:45')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30, 45)
|
||||||
|
>>> f.clean('2006-10-25 14:30:00')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean('2006-10-25 14:30')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean('2006-10-25')
|
||||||
|
datetime.datetime(2006, 10, 25, 0, 0)
|
||||||
|
>>> f.clean('25/10/2006 14:30:45')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30, 45)
|
||||||
|
>>> f.clean('25/10/2006 14:30:00')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean('25/10/2006 14:30')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean('25/10/2006')
|
||||||
|
datetime.datetime(2006, 10, 25, 0, 0)
|
||||||
|
>>> f.clean('25/10/06 14:30:45')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30, 45)
|
||||||
|
>>> f.clean('25/10/06 14:30:00')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean('25/10/06 14:30')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean('25/10/06')
|
||||||
|
datetime.datetime(2006, 10, 25, 0, 0)
|
||||||
|
>>> f.clean('hello')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date/time.']
|
||||||
|
>>> f.clean('2006-10-25 4:30 p.m.')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date/time.']
|
||||||
|
|
||||||
|
DateField accepts an optional input_formats parameter:
|
||||||
|
>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
|
||||||
|
>>> f.clean(datetime.date(2006, 10, 25))
|
||||||
|
datetime.datetime(2006, 10, 25, 0, 0)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30, 59)
|
||||||
|
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
|
||||||
|
>>> f.clean('2006 10 25 2:30 PM')
|
||||||
|
datetime.datetime(2006, 10, 25, 14, 30)
|
||||||
|
|
||||||
|
The input_formats parameter overrides all default input formats,
|
||||||
|
so the default formats won't work unless you specify them:
|
||||||
|
>>> f.clean('2006-10-25 14:30:45')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Enter a valid date/time.']
|
||||||
|
|
||||||
|
>>> f = DateTimeField(required=False)
|
||||||
|
>>> f.clean(None)
|
||||||
|
>>> repr(f.clean(None))
|
||||||
|
'None'
|
||||||
|
>>> f.clean('')
|
||||||
|
>>> repr(f.clean(''))
|
||||||
|
'None'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue