Code

Ticket #3876: localflavor_au.diff

File localflavor_au.diff, 7.6 KB (added by Matthew Flanagan <mattimustang@…>, 7 years ago)

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