Ticket #4542: 4542.diff

File 4542.diff, 7.4 KB (added by nick.lane.au@…, 17 years ago)

Patch against r5625

  • django/contrib/localflavor/generic/forms.py

    === added directory 'django/contrib/localflavor/generic'
    === added file 'django/contrib/localflavor/generic/__init__.py'
    === added file 'django/contrib/localflavor/generic/forms.py'
     
     1from django import newforms as forms
     2
     3DEFAULT_DATE_INPUT_FORMATS = (
     4    '%Y-%m-%d', '%d/%m/%Y', '%d/%m/%y', # '2006-10-25', '25/10/2006', '25/10/06'
     5    '%b %d %Y', '%b %d, %Y',            # 'Oct 25 2006', 'Oct 25, 2006'
     6    '%d %b %Y', '%d %b, %Y',            # '25 Oct 2006', '25 Oct, 2006'
     7    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006'
     8    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006'
     9)
     10
     11DEFAULT_DATETIME_INPUT_FORMATS = (
     12    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
     13    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
     14    '%Y-%m-%d',              # '2006-10-25'
     15    '%d/%m/%Y %H:%M:%S',     # '25/10/2006 14:30:59'
     16    '%d/%m/%Y %H:%M',        # '25/10/2006 14:30'
     17    '%d/%m/%Y',              # '25/10/2006'
     18    '%d/%m/%y %H:%M:%S',     # '25/10/06 14:30:59'
     19    '%d/%m/%y %H:%M',        # '25/10/06 14:30'
     20    '%d/%m/%y',              # '25/10/06'
     21)
     22
     23class DateField(forms.DateField):
     24    """
     25    A date input field which uses non-US date input formats by default.
     26    """
     27    def __init__(self, input_formats=None, *args, **kwargs):
     28        input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
     29        super(DateField, self).__init__(input_formats=input_formats, *args, **kwargs)
     30
     31class DateTimeField(forms.DateTimeField):
     32    """
     33    A date and time input field which uses non-US date and time input formats
     34    by default.
     35    """
     36    def __init__(self, input_formats=None, *args, **kwargs):
     37        input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
     38        super(DateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs)
  • tests/regressiontests/forms/localflavor.py

    === modified file 'tests/regressiontests/forms/localflavor.py'
     
    13561356...
    13571357ValidationError: [u'Enter valid a Chilean RUT. The format is XX.XXX.XXX-X.']
    13581358
     1359## Generic DateField ##########################################################
     1360
     1361>>> from django.contrib.localflavor.generic.forms import *
     1362
     1363A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
     1364appropriate.
     1365
     1366>>> import datetime
     1367>>> f = DateField()
     1368>>> f.clean(datetime.date(2006, 10, 25))
     1369datetime.date(2006, 10, 25)
     1370>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     1371datetime.date(2006, 10, 25)
     1372>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
     1373datetime.date(2006, 10, 25)
     1374>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
     1375datetime.date(2006, 10, 25)
     1376>>> f.clean('2006-10-25')
     1377datetime.date(2006, 10, 25)
     1378>>> f.clean('25/10/2006')
     1379datetime.date(2006, 10, 25)
     1380>>> f.clean('25/10/06')
     1381datetime.date(2006, 10, 25)
     1382>>> f.clean('Oct 25 2006')
     1383datetime.date(2006, 10, 25)
     1384>>> f.clean('October 25 2006')
     1385datetime.date(2006, 10, 25)
     1386>>> f.clean('October 25, 2006')
     1387datetime.date(2006, 10, 25)
     1388>>> f.clean('25 October 2006')
     1389datetime.date(2006, 10, 25)
     1390>>> f.clean('25 October, 2006')
     1391datetime.date(2006, 10, 25)
     1392>>> f.clean('2006-4-31')
     1393Traceback (most recent call last):
     1394...
     1395ValidationError: [u'Enter a valid date.']
     1396>>> f.clean('200a-10-25')
     1397Traceback (most recent call last):
     1398...
     1399ValidationError: [u'Enter a valid date.']
     1400>>> f.clean('10/25/06')
     1401Traceback (most recent call last):
     1402...
     1403ValidationError: [u'Enter a valid date.']
     1404>>> f.clean(None)
     1405Traceback (most recent call last):
     1406...
     1407ValidationError: [u'This field is required.']
     1408
     1409>>> f = DateField(required=False)
     1410>>> f.clean(None)
     1411>>> repr(f.clean(None))
     1412'None'
     1413>>> f.clean('')
     1414>>> repr(f.clean(''))
     1415'None'
     1416
     1417DateField accepts an optional input_formats parameter:
     1418>>> f = DateField(input_formats=['%Y %m %d'])
     1419>>> f.clean(datetime.date(2006, 10, 25))
     1420datetime.date(2006, 10, 25)
     1421>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     1422datetime.date(2006, 10, 25)
     1423>>> f.clean('2006 10 25')
     1424datetime.date(2006, 10, 25)
     1425
     1426The input_formats parameter overrides all default input formats,
     1427so the default formats won't work unless you specify them:
     1428>>> f.clean('2006-10-25')
     1429Traceback (most recent call last):
     1430...
     1431ValidationError: [u'Enter a valid date.']
     1432>>> f.clean('25/10/2006')
     1433Traceback (most recent call last):
     1434...
     1435ValidationError: [u'Enter a valid date.']
     1436>>> f.clean('25/10/06')
     1437Traceback (most recent call last):
     1438...
     1439ValidationError: [u'Enter a valid date.']
     1440
     1441## Generic DateTimeField ######################################################
     1442
     1443A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
     1444appropriate.
     1445
     1446>>> import datetime
     1447>>> f = DateTimeField()
     1448>>> f.clean(datetime.date(2006, 10, 25))
     1449datetime.datetime(2006, 10, 25, 0, 0)
     1450>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     1451datetime.datetime(2006, 10, 25, 14, 30)
     1452>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
     1453datetime.datetime(2006, 10, 25, 14, 30, 59)
     1454>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
     1455datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
     1456>>> f.clean('2006-10-25 14:30:45')
     1457datetime.datetime(2006, 10, 25, 14, 30, 45)
     1458>>> f.clean('2006-10-25 14:30:00')
     1459datetime.datetime(2006, 10, 25, 14, 30)
     1460>>> f.clean('2006-10-25 14:30')
     1461datetime.datetime(2006, 10, 25, 14, 30)
     1462>>> f.clean('2006-10-25')
     1463datetime.datetime(2006, 10, 25, 0, 0)
     1464>>> f.clean('25/10/2006 14:30:45')
     1465datetime.datetime(2006, 10, 25, 14, 30, 45)
     1466>>> f.clean('25/10/2006 14:30:00')
     1467datetime.datetime(2006, 10, 25, 14, 30)
     1468>>> f.clean('25/10/2006 14:30')
     1469datetime.datetime(2006, 10, 25, 14, 30)
     1470>>> f.clean('25/10/2006')
     1471datetime.datetime(2006, 10, 25, 0, 0)
     1472>>> f.clean('25/10/06 14:30:45')
     1473datetime.datetime(2006, 10, 25, 14, 30, 45)
     1474>>> f.clean('25/10/06 14:30:00')
     1475datetime.datetime(2006, 10, 25, 14, 30)
     1476>>> f.clean('25/10/06 14:30')
     1477datetime.datetime(2006, 10, 25, 14, 30)
     1478>>> f.clean('25/10/06')
     1479datetime.datetime(2006, 10, 25, 0, 0)
     1480>>> f.clean('hello')
     1481Traceback (most recent call last):
     1482...
     1483ValidationError: [u'Enter a valid date/time.']
     1484>>> f.clean('2006-10-25 4:30 p.m.')
     1485Traceback (most recent call last):
     1486...
     1487ValidationError: [u'Enter a valid date/time.']
     1488
     1489DateField accepts an optional input_formats parameter:
     1490>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
     1491>>> f.clean(datetime.date(2006, 10, 25))
     1492datetime.datetime(2006, 10, 25, 0, 0)
     1493>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     1494datetime.datetime(2006, 10, 25, 14, 30)
     1495>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
     1496datetime.datetime(2006, 10, 25, 14, 30, 59)
     1497>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
     1498datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
     1499>>> f.clean('2006 10 25 2:30 PM')
     1500datetime.datetime(2006, 10, 25, 14, 30)
     1501
     1502The input_formats parameter overrides all default input formats,
     1503so the default formats won't work unless you specify them:
     1504>>> f.clean('2006-10-25 14:30:45')
     1505Traceback (most recent call last):
     1506...
     1507ValidationError: [u'Enter a valid date/time.']
     1508
     1509>>> f = DateTimeField(required=False)
     1510>>> f.clean(None)
     1511>>> repr(f.clean(None))
     1512'None'
     1513>>> f.clean('')
     1514>>> repr(f.clean(''))
     1515'None'
     1516
    13591517"""
Back to Top