Ticket #4542: generic-localflavor.diff

File generic-localflavor.diff, 8.1 KB (added by nick.lane.au@…, 12 years ago)

Generic localflavor with date fields

  • 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

    === added file 'tests/regressiontests/forms/localflavor.py'
     
     1# -*- coding: utf-8 -*-
     2# Tests for the different contrib/localflavor/ form fields.
     3
     4localflavor_tests = r"""
     5>>> from django.contrib.localflavor.generic.forms import *
     6
     7
     8## Generic DateField ##########################################################
     9
     10A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
     11appropriate.
     12
     13>>> import datetime
     14>>> f = DateField()
     15>>> f.clean(datetime.date(2006, 10, 25))
     16datetime.date(2006, 10, 25)
     17>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     18datetime.date(2006, 10, 25)
     19>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
     20datetime.date(2006, 10, 25)
     21>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
     22datetime.date(2006, 10, 25)
     23>>> f.clean('2006-10-25')
     24datetime.date(2006, 10, 25)
     25>>> f.clean('25/10/2006')
     26datetime.date(2006, 10, 25)
     27>>> f.clean('25/10/06')
     28datetime.date(2006, 10, 25)
     29>>> f.clean('Oct 25 2006')
     30datetime.date(2006, 10, 25)
     31>>> f.clean('October 25 2006')
     32datetime.date(2006, 10, 25)
     33>>> f.clean('October 25, 2006')
     34datetime.date(2006, 10, 25)
     35>>> f.clean('25 October 2006')
     36datetime.date(2006, 10, 25)
     37>>> f.clean('25 October, 2006')
     38datetime.date(2006, 10, 25)
     39>>> f.clean('2006-4-31')
     40Traceback (most recent call last):
     41...
     42ValidationError: [u'Enter a valid date.']
     43>>> f.clean('200a-10-25')
     44Traceback (most recent call last):
     45...
     46ValidationError: [u'Enter a valid date.']
     47>>> f.clean('10/25/06')
     48Traceback (most recent call last):
     49...
     50ValidationError: [u'Enter a valid date.']
     51>>> f.clean(None)
     52Traceback (most recent call last):
     53...
     54ValidationError: [u'This field is required.']
     55
     56>>> f = DateField(required=False)
     57>>> f.clean(None)
     58>>> repr(f.clean(None))
     59'None'
     60>>> f.clean('')
     61>>> repr(f.clean(''))
     62'None'
     63
     64DateField accepts an optional input_formats parameter:
     65>>> f = DateField(input_formats=['%Y %m %d'])
     66>>> f.clean(datetime.date(2006, 10, 25))
     67datetime.date(2006, 10, 25)
     68>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     69datetime.date(2006, 10, 25)
     70>>> f.clean('2006 10 25')
     71datetime.date(2006, 10, 25)
     72
     73The input_formats parameter overrides all default input formats,
     74so the default formats won't work unless you specify them:
     75>>> f.clean('2006-10-25')
     76Traceback (most recent call last):
     77...
     78ValidationError: [u'Enter a valid date.']
     79>>> f.clean('25/10/2006')
     80Traceback (most recent call last):
     81...
     82ValidationError: [u'Enter a valid date.']
     83>>> f.clean('25/10/06')
     84Traceback (most recent call last):
     85...
     86ValidationError: [u'Enter a valid date.']
     87
     88## Generic DateTimeField ######################################################
     89
     90A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
     91appropriate.
     92
     93>>> import datetime
     94>>> f = DateTimeField()
     95>>> f.clean(datetime.date(2006, 10, 25))
     96datetime.datetime(2006, 10, 25, 0, 0)
     97>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     98datetime.datetime(2006, 10, 25, 14, 30)
     99>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
     100datetime.datetime(2006, 10, 25, 14, 30, 59)
     101>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
     102datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
     103>>> f.clean('2006-10-25 14:30:45')
     104datetime.datetime(2006, 10, 25, 14, 30, 45)
     105>>> f.clean('2006-10-25 14:30:00')
     106datetime.datetime(2006, 10, 25, 14, 30)
     107>>> f.clean('2006-10-25 14:30')
     108datetime.datetime(2006, 10, 25, 14, 30)
     109>>> f.clean('2006-10-25')
     110datetime.datetime(2006, 10, 25, 0, 0)
     111>>> f.clean('25/10/2006 14:30:45')
     112datetime.datetime(2006, 10, 25, 14, 30, 45)
     113>>> f.clean('25/10/2006 14:30:00')
     114datetime.datetime(2006, 10, 25, 14, 30)
     115>>> f.clean('25/10/2006 14:30')
     116datetime.datetime(2006, 10, 25, 14, 30)
     117>>> f.clean('25/10/2006')
     118datetime.datetime(2006, 10, 25, 0, 0)
     119>>> f.clean('25/10/06 14:30:45')
     120datetime.datetime(2006, 10, 25, 14, 30, 45)
     121>>> f.clean('25/10/06 14:30:00')
     122datetime.datetime(2006, 10, 25, 14, 30)
     123>>> f.clean('25/10/06 14:30')
     124datetime.datetime(2006, 10, 25, 14, 30)
     125>>> f.clean('25/10/06')
     126datetime.datetime(2006, 10, 25, 0, 0)
     127>>> f.clean('hello')
     128Traceback (most recent call last):
     129...
     130ValidationError: [u'Enter a valid date/time.']
     131>>> f.clean('2006-10-25 4:30 p.m.')
     132Traceback (most recent call last):
     133...
     134ValidationError: [u'Enter a valid date/time.']
     135
     136DateField accepts an optional input_formats parameter:
     137>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
     138>>> f.clean(datetime.date(2006, 10, 25))
     139datetime.datetime(2006, 10, 25, 0, 0)
     140>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
     141datetime.datetime(2006, 10, 25, 14, 30)
     142>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
     143datetime.datetime(2006, 10, 25, 14, 30, 59)
     144>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
     145datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
     146>>> f.clean('2006 10 25 2:30 PM')
     147datetime.datetime(2006, 10, 25, 14, 30)
     148
     149The input_formats parameter overrides all default input formats,
     150so the default formats won't work unless you specify them:
     151>>> f.clean('2006-10-25 14:30:45')
     152Traceback (most recent call last):
     153...
     154ValidationError: [u'Enter a valid date/time.']
     155
     156>>> f = DateTimeField(required=False)
     157>>> f.clean(None)
     158>>> repr(f.clean(None))
     159'None'
     160>>> f.clean('')
     161>>> repr(f.clean(''))
     162'None'
     163
     164"""
  • tests/regressiontests/forms/tests.py

    === modified file 'tests/regressiontests/forms/tests.py'
     
    11# -*- coding: utf-8 -*-
    2 r"""
     2from localflavor import localflavor_tests
     3
     4form_tests = r"""
    35>>> from django.newforms import *
    46>>> import datetime
    57>>> import re
     
    35803582u'foo'
    35813583"""
    35823584
     3585__test__ = {
     3586    'form_tests': form_tests,
     3587    'localflavor': localflavor_tests,
     3588}
     3589
    35833590if __name__ == "__main__":
    35843591    import doctest
    35853592    doctest.testmod()
Back to Top