Code

Ticket #3876: localflavor_au2.diff

File localflavor_au2.diff, 6.9 KB (added by mattimustang@…, 7 years ago)

updated patch with better ValidationError message and moved tests into localflavor.py module

Line 
1Index: django/contrib/localflavor/au/au_states.py
2===================================================================
3--- django/contrib/localflavor/au/au_states.py  (revision 0)
4+++ django/contrib/localflavor/au/au_states.py  (revision 0)
5@@ -0,0 +1,17 @@
6+"""
7+An alphabetical list of states for use as `choices` in a formfield.
8+
9+This exists in this standalone file so that it's only imported into memory
10+when explicitly needed.
11+"""
12+
13+STATE_CHOICES = (
14+    ('ACT', 'Australian Capital Territory'),
15+    ('NSW', 'New South Wales'),
16+    ('NT', 'Northern Territory'),
17+    ('QLD', 'Queensland'),
18+    ('SA', 'South Australia'),
19+    ('TAS', 'Tasmania'),
20+    ('VIC', 'Victoria'),
21+    ('WA', 'Western Australia'),
22+)
23Index: django/contrib/localflavor/au/__init__.py
24===================================================================
25Index: django/contrib/localflavor/au/forms.py
26===================================================================
27--- django/contrib/localflavor/au/forms.py      (revision 0)
28+++ django/contrib/localflavor/au/forms.py      (revision 0)
29@@ -0,0 +1,44 @@
30+"""
31+Australian-specific Form helpers
32+"""
33+
34+from django.newforms import ValidationError
35+from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
36+from django.newforms.util import smart_unicode
37+from django.utils.translation import gettext
38+import re
39+
40+PHONE_DIGITS_RE = re.compile(r'^(\d{10})$')
41+
42+class AUPostCodeField(RegexField):
43+    """Australian post code field."""
44+    def __init__(self, *args, **kwargs):
45+        super(AUPostCodeField, self).__init__(r'^\d{4}$',
46+            max_length=None, min_length=None,
47+            error_message=gettext(u'Enter a post code in the format XXXX.'),
48+            *args, **kwargs)
49+
50+class AUPhoneNumberField(Field):
51+    """Australian phone number field."""
52+    def clean(self, value):
53+        """Validate a phone number. Strips parentheses, whitespace and
54+        hyphens.
55+        """
56+        super(AUPhoneNumberField, self).clean(value)
57+        if value in EMPTY_VALUES:
58+            return u''
59+        value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value))
60+        phone_match = PHONE_DIGITS_RE.search(value)
61+        if phone_match:
62+            return u'%s' % phone_match.group(1)
63+        raise ValidationError(u'Phone numbers must consist of 10 digits.' \
64+            u' Parentheses, white space and hyphens are allowed.')
65+
66+class AUStateSelect(Select):
67+    """
68+    A Select widget that uses a list of Australian states/territories as its
69+    choices.
70+    """
71+    def __init__(self, attrs=None):
72+        from au_states import STATE_CHOICES # relative import
73+        super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
74Index: tests/regressiontests/forms/localflavor.py
75===================================================================
76--- tests/regressiontests/forms/localflavor.py  (revision 4940)
77+++ tests/regressiontests/forms/localflavor.py  (working copy)
78@@ -882,4 +882,122 @@
79 Traceback (most recent call last):
80 ...
81 ValidationError: [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.']
82+
83+## AUPostCodeField ##########################################################
84+
85+A field that accepts a four digit Australian post code.
86+
87+>>> from django.contrib.localflavor.au.forms import AUPostCodeField
88+>>> f = AUPostCodeField()
89+>>> f.clean('1234')
90+u'1234'
91+>>> f.clean('2000')
92+u'2000'
93+>>> f.clean('abcd')
94+Traceback (most recent call last):
95+...
96+ValidationError: [u'Enter a post code in the format XXXX.']
97+>>> f.clean('20001')
98+Traceback (most recent call last):
99+...
100+ValidationError: [u'Enter a post code in the format XXXX.']
101+>>> f.clean(None)
102+Traceback (most recent call last):
103+...
104+ValidationError: [u'This field is required.']
105+>>> f.clean('')
106+Traceback (most recent call last):
107+...
108+ValidationError: [u'This field is required.']
109+
110+>>> f = AUPostCodeField(required=False)
111+>>> f.clean('1234')
112+u'1234'
113+>>> f.clean('2000')
114+u'2000'
115+>>> f.clean('abcd')
116+Traceback (most recent call last):
117+...
118+ValidationError: [u'Enter a post code in the format XXXX.']
119+>>> f.clean('20001')
120+Traceback (most recent call last):
121+...
122+ValidationError: [u'Enter a post code in the format XXXX.']
123+>>> f.clean(None)
124+u''
125+>>> f.clean('')
126+u''
127+
128+## AUPhoneNumberField ########################################################
129+
130+A field that accepts a 10 digit Australian phone number.
131+llows spaces and parentheses around area code.
132+
133+>>> from django.contrib.localflavor.au.forms import AUPhoneNumberField
134+>>> f = AUPhoneNumberField()
135+>>> f.clean('1234567890')
136+u'1234567890'
137+>>> f.clean('0213456789')
138+u'0213456789'
139+>>> f.clean('02 13 45 67 89')
140+u'0213456789'
141+>>> f.clean('(02) 1345 6789')
142+u'0213456789'
143+>>> f.clean('123')
144+Traceback (most recent call last):
145+...
146+ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
147+>>> f.clean('1800DJANGO')
148+Traceback (most recent call last):
149+...
150+ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
151+>>> f.clean(None)
152+Traceback (most recent call last):
153+...
154+ValidationError: [u'This field is required.']
155+>>> f.clean('')
156+Traceback (most recent call last):
157+...
158+ValidationError: [u'This field is required.']
159+
160+>>> f = AUPhoneNumberField(required=False)
161+>>> f.clean('1234567890')
162+u'1234567890'
163+>>> f.clean('0213456789')
164+u'0213456789'
165+>>> f.clean('02 13 45 67 89')
166+u'0213456789'
167+>>> f.clean('(02) 1345 6789')
168+u'0213456789'
169+>>> f.clean('123')
170+Traceback (most recent call last):
171+...
172+ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
173+>>> f.clean('1800DJANGO')
174+Traceback (most recent call last):
175+...
176+ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
177+>>> f.clean(None)
178+u''
179+>>> f.clean('')
180+u''
181+
182+## AUStateSelect #############################################################
183+
184+AUStateSelect is a Select widget that uses a list of Australian
185+states/territories as its choices.
186+
187+>>> from django.contrib.localflavor.au.forms import AUStateSelect
188+>>> f = AUStateSelect()
189+>>> print f.render('state', 'NSW')
190+<select name="state">
191+<option value="ACT">Australian Capital Territory</option>
192+<option value="NSW" selected="selected">New South Wales</option>
193+<option value="NT">Northern Territory</option>
194+<option value="QLD">Queensland</option>
195+<option value="SA">South Australia</option>
196+<option value="TAS">Tasmania</option>
197+<option value="VIC">Victoria</option>
198+<option value="WA">Western Australia</option>
199+</select>
200 """
201Index: AUTHORS
202===================================================================
203--- AUTHORS     (revision 4940)
204+++ AUTHORS     (working copy)
205@@ -88,6 +88,7 @@
206     Dirk Eschler <dirk.eschler@gmx.net>
207     Marc Fargas <telenieko@telenieko.com>
208     favo@exoweb.net
209+    Matthew Flanagan <http://wadofstuff.blogspot.com>
210     Eric Floehr <eric@intellovations.com>
211     Jorge Gajon <gajon@gajon.org>
212     gandalf@owca.info