Ticket #4542: au_dates.diff

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

Australian date fields

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

     
    33"""
    44
    55from django.newforms import ValidationError
    6 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
     6from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES, DateField, DateTimeField
    77from django.newforms.util import smart_unicode
    88from django.utils.translation import gettext
    99import re
    1010
    1111PHONE_DIGITS_RE = re.compile(r'^(\d{10})$')
    1212
     13DEFAULT_DATE_INPUT_FORMATS = (
     14    '%Y-%m-%d', '%d/%m/%Y', '%d/%m/%y', # '2006-10-25', '25/10/2006', '25/10/06'
     15    '%b %d %Y', '%b %d, %Y',            # 'Oct 25 2006', 'Oct 25, 2006'
     16    '%d %b %Y', '%d %b, %Y',            # '25 Oct 2006', '25 Oct, 2006'
     17    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006'
     18    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006'
     19)
     20
     21DEFAULT_DATETIME_INPUT_FORMATS = (
     22    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
     23    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
     24    '%Y-%m-%d',              # '2006-10-25'
     25    '%d/%m/%Y %H:%M:%S',     # '25/10/2006 14:30:59'
     26    '%d/%m/%Y %H:%M',        # '25/10/2006 14:30'
     27    '%d/%m/%Y',              # '25/10/2006'
     28    '%d/%m/%y %H:%M:%S',     # '25/10/06 14:30:59'
     29    '%d/%m/%y %H:%M',        # '25/10/06 14:30'
     30    '%d/%m/%y',              # '25/10/06'
     31)
     32
    1333class AUPostCodeField(RegexField):
    1434    """Australian post code field."""
    1535    def __init__(self, *args, **kwargs):
     
    4161    def __init__(self, attrs=None):
    4262        from au_states import STATE_CHOICES # relative import
    4363        super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
     64
     65class AUDateField(DateField):
     66    """
     67    A date input field which uses Australian date input formats by default.
     68    """
     69    def __init__(self, input_formats=None, *args, **kwargs):
     70        input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
     71        super(AUDateField, self).__init__(input_formats=input_formats, *args, **kwargs)
     72
     73class AUDateTimeField(DateTimeField):
     74    """
     75    A date and time input field which uses Australian date and time input
     76    formats by default.
     77    """
     78    def __init__(self, input_formats=None, *args, **kwargs):
     79        input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
     80        super(AUDateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs)
Back to Top