Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 525af7d5804852d4de0f2676990d19ff9dae969f 1 parent 55d6aeb
Jacob Kaplan-Moss authored September 14, 2007
38  django/contrib/localflavor/generic/forms.py
... ...
@@ -0,0 +1,38 @@
  1
+from django import newforms as forms
  2
+
  3
+DEFAULT_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
+
  11
+DEFAULT_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
+
  23
+class 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
+
  31
+class 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)
0  localflavor/generic/__init__.py b/django/contrib/localflavor/generic/__init__.py
No changes.
158  tests/regressiontests/forms/localflavor.py
@@ -2036,4 +2036,162 @@
2036 2036
 ...
2037 2037
 ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.']
2038 2038
 
  2039
+## Generic DateField ##########################################################
  2040
+
  2041
+>>> from django.contrib.localflavor.generic.forms import *
  2042
+
  2043
+A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
  2044
+appropriate.
  2045
+
  2046
+>>> import datetime
  2047
+>>> f = DateField()
  2048
+>>> f.clean(datetime.date(2006, 10, 25))
  2049
+datetime.date(2006, 10, 25)
  2050
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
  2051
+datetime.date(2006, 10, 25)
  2052
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
  2053
+datetime.date(2006, 10, 25)
  2054
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
  2055
+datetime.date(2006, 10, 25)
  2056
+>>> f.clean('2006-10-25')
  2057
+datetime.date(2006, 10, 25)
  2058
+>>> f.clean('25/10/2006')
  2059
+datetime.date(2006, 10, 25)
  2060
+>>> f.clean('25/10/06')
  2061
+datetime.date(2006, 10, 25)
  2062
+>>> f.clean('Oct 25 2006')
  2063
+datetime.date(2006, 10, 25)
  2064
+>>> f.clean('October 25 2006')
  2065
+datetime.date(2006, 10, 25)
  2066
+>>> f.clean('October 25, 2006')
  2067
+datetime.date(2006, 10, 25)
  2068
+>>> f.clean('25 October 2006')
  2069
+datetime.date(2006, 10, 25)
  2070
+>>> f.clean('25 October, 2006')
  2071
+datetime.date(2006, 10, 25)
  2072
+>>> f.clean('2006-4-31')
  2073
+Traceback (most recent call last):
  2074
+...
  2075
+ValidationError: [u'Enter a valid date.']
  2076
+>>> f.clean('200a-10-25')
  2077
+Traceback (most recent call last):
  2078
+...
  2079
+ValidationError: [u'Enter a valid date.']
  2080
+>>> f.clean('10/25/06')
  2081
+Traceback (most recent call last):
  2082
+...
  2083
+ValidationError: [u'Enter a valid date.']
  2084
+>>> f.clean(None)
  2085
+Traceback (most recent call last):
  2086
+...
  2087
+ValidationError: [u'This field is required.']
  2088
+
  2089
+>>> f = DateField(required=False)
  2090
+>>> f.clean(None)
  2091
+>>> repr(f.clean(None))
  2092
+'None'
  2093
+>>> f.clean('')
  2094
+>>> repr(f.clean(''))
  2095
+'None'
  2096
+
  2097
+DateField accepts an optional input_formats parameter:
  2098
+>>> f = DateField(input_formats=['%Y %m %d'])
  2099
+>>> f.clean(datetime.date(2006, 10, 25))
  2100
+datetime.date(2006, 10, 25)
  2101
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
  2102
+datetime.date(2006, 10, 25)
  2103
+>>> f.clean('2006 10 25')
  2104
+datetime.date(2006, 10, 25)
  2105
+
  2106
+The input_formats parameter overrides all default input formats,
  2107
+so the default formats won't work unless you specify them:
  2108
+>>> f.clean('2006-10-25')
  2109
+Traceback (most recent call last):
  2110
+...
  2111
+ValidationError: [u'Enter a valid date.']
  2112
+>>> f.clean('25/10/2006')
  2113
+Traceback (most recent call last):
  2114
+...
  2115
+ValidationError: [u'Enter a valid date.']
  2116
+>>> f.clean('25/10/06')
  2117
+Traceback (most recent call last):
  2118
+...
  2119
+ValidationError: [u'Enter a valid date.']
  2120
+
  2121
+## Generic DateTimeField ######################################################
  2122
+
  2123
+A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
  2124
+appropriate.
  2125
+
  2126
+>>> import datetime
  2127
+>>> f = DateTimeField()
  2128
+>>> f.clean(datetime.date(2006, 10, 25))
  2129
+datetime.datetime(2006, 10, 25, 0, 0)
  2130
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
  2131
+datetime.datetime(2006, 10, 25, 14, 30)
  2132
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
  2133
+datetime.datetime(2006, 10, 25, 14, 30, 59)
  2134
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
  2135
+datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
  2136
+>>> f.clean('2006-10-25 14:30:45')
  2137
+datetime.datetime(2006, 10, 25, 14, 30, 45)
  2138
+>>> f.clean('2006-10-25 14:30:00')
  2139
+datetime.datetime(2006, 10, 25, 14, 30)
  2140
+>>> f.clean('2006-10-25 14:30')
  2141
+datetime.datetime(2006, 10, 25, 14, 30)
  2142
+>>> f.clean('2006-10-25')
  2143
+datetime.datetime(2006, 10, 25, 0, 0)
  2144
+>>> f.clean('25/10/2006 14:30:45')
  2145
+datetime.datetime(2006, 10, 25, 14, 30, 45)
  2146
+>>> f.clean('25/10/2006 14:30:00')
  2147
+datetime.datetime(2006, 10, 25, 14, 30)
  2148
+>>> f.clean('25/10/2006 14:30')
  2149
+datetime.datetime(2006, 10, 25, 14, 30)
  2150
+>>> f.clean('25/10/2006')
  2151
+datetime.datetime(2006, 10, 25, 0, 0)
  2152
+>>> f.clean('25/10/06 14:30:45')
  2153
+datetime.datetime(2006, 10, 25, 14, 30, 45)
  2154
+>>> f.clean('25/10/06 14:30:00')
  2155
+datetime.datetime(2006, 10, 25, 14, 30)
  2156
+>>> f.clean('25/10/06 14:30')
  2157
+datetime.datetime(2006, 10, 25, 14, 30)
  2158
+>>> f.clean('25/10/06')
  2159
+datetime.datetime(2006, 10, 25, 0, 0)
  2160
+>>> f.clean('hello')
  2161
+Traceback (most recent call last):
  2162
+...
  2163
+ValidationError: [u'Enter a valid date/time.']
  2164
+>>> f.clean('2006-10-25 4:30 p.m.')
  2165
+Traceback (most recent call last):
  2166
+...
  2167
+ValidationError: [u'Enter a valid date/time.']
  2168
+
  2169
+DateField accepts an optional input_formats parameter:
  2170
+>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
  2171
+>>> f.clean(datetime.date(2006, 10, 25))
  2172
+datetime.datetime(2006, 10, 25, 0, 0)
  2173
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
  2174
+datetime.datetime(2006, 10, 25, 14, 30)
  2175
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
  2176
+datetime.datetime(2006, 10, 25, 14, 30, 59)
  2177
+>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
  2178
+datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
  2179
+>>> f.clean('2006 10 25 2:30 PM')
  2180
+datetime.datetime(2006, 10, 25, 14, 30)
  2181
+
  2182
+The input_formats parameter overrides all default input formats,
  2183
+so the default formats won't work unless you specify them:
  2184
+>>> f.clean('2006-10-25 14:30:45')
  2185
+Traceback (most recent call last):
  2186
+...
  2187
+ValidationError: [u'Enter a valid date/time.']
  2188
+
  2189
+>>> f = DateTimeField(required=False)
  2190
+>>> f.clean(None)
  2191
+>>> repr(f.clean(None))
  2192
+'None'
  2193
+>>> f.clean('')
  2194
+>>> repr(f.clean(''))
  2195
+'None'
  2196
+
2039 2197
 """

0 notes on commit 525af7d

Please sign in to comment.
Something went wrong with that request. Please try again.