Code

Ticket #13733: nz-localflavor.diff

File nz-localflavor.diff, 8.9 KB (added by schinckel, 4 years ago)
Line 
1Index: django/contrib/localflavor/nz/__init__.py
2===================================================================
3Index: django/contrib/localflavor/nz/nz_provincial_districts.py
4===================================================================
5--- django/contrib/localflavor/nz/nz_provincial_districts.py    (revision 0)
6+++ django/contrib/localflavor/nz/nz_provincial_districts.py    (revision 0)
7@@ -0,0 +1,18 @@
8+from django.utils.translation import gettext_lazy as _
9+
10+PROVINCIAL_DISTRICT_CHOICES = (
11+    ('Auckland', _('Auckland Province')),
12+    ('Canterbury', _('Canterbury')),
13+    ('Chatham', _('Chatham Islands')),
14+    ('HawkesBay', _("Hawke's Bay")),
15+    ('Marlborough', _('Marlborough')),
16+    ('Nelson', _('Nelson')),
17+    ('Northland', _('Northland')),
18+    ('Otago', _('Otago Province')),
19+    ('SouthCanterbury', _('South Canterbury')),
20+    ('Southland', _('Southland')),
21+    ('Taranaki', _('Taranaki (New Plymouth)')),
22+    ('Wellington', _('Wellington Province')),
23+    ('Westland', _('Westland')),
24+
25+)
26Index: django/contrib/localflavor/nz/forms.py
27===================================================================
28--- django/contrib/localflavor/nz/forms.py      (revision 0)
29+++ django/contrib/localflavor/nz/forms.py      (revision 0)
30@@ -0,0 +1,51 @@
31+"""
32+New Zealand-specific Form helpers
33+"""
34+
35+from django.core.validators import EMPTY_VALUES
36+from django.forms import ValidationError
37+from django.forms.fields import Field, RegexField, Select
38+from django.utils.encoding import smart_unicode
39+from django.utils.translation import ugettext_lazy as _
40+import re
41+
42+PHONE_DIGITS_RE = re.compile(r'^((0\d{8})|(02\d{7,9}))$')
43+
44+class NZPostCodeField(RegexField):
45+    """New Zealand post code field."""
46+    default_error_messages = {
47+        'invalid': _('Enter a 4 digit post code.'),
48+    }
49+
50+    def __init__(self, *args, **kwargs):
51+        super(NZPostCodeField, self).__init__(r'^\d{4}$',
52+            max_length=None, min_length=None, *args, **kwargs)
53+
54+class NZPhoneNumberField(Field):
55+    """New Zealand phone number field."""
56+    default_error_messages = {
57+        'invalid': u'Phone numbers must contain 9 digits (including area code),'
58+        ' unless they are mobile numbers (02, then 7-9 digits).',
59+    }
60+
61+    def clean(self, value):
62+        """
63+        Validate a phone number. Strips parentheses, whitespace and hyphens.
64+        """
65+        super(NZPhoneNumberField, self).clean(value)
66+        if value in EMPTY_VALUES:
67+            return u''
68+        value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value))
69+        phone_match = PHONE_DIGITS_RE.search(value)
70+        if phone_match:
71+            return u'%s' % phone_match.group(1)
72+        raise ValidationError(self.error_messages['invalid'])
73+
74+class NZProvincialDistrictSelect(Select):
75+    """
76+    A Select widget that uses a list of Australian states/territories as its
77+    choices.
78+    """
79+    def __init__(self, attrs=None):
80+        from nz_provincial_districts import PROVINCIAL_DISTRICT_CHOICES
81+        super(NZProvincialDistrictSelect, self).__init__(attrs, choices=PROVINCIAL_DISTRICT_CHOICES)
82Index: tests/regressiontests/forms/localflavor/nz.py
83===================================================================
84--- tests/regressiontests/forms/localflavor/nz.py       (revision 0)
85+++ tests/regressiontests/forms/localflavor/nz.py       (revision 0)
86@@ -0,0 +1,147 @@
87+# -*- coding: utf-8 -*-
88+# Tests for the contrib/localflavor/ NZ form fields.
89+
90+tests = r"""
91+## NZPostCodeField ##########################################################
92+
93+A field that accepts a four digit New Zealand post code.
94+
95+>>> from django.contrib.localflavor.nz.forms import NZPostCodeField
96+>>> f = NZPostCodeField()
97+>>> f.clean('1234')
98+u'1234'
99+>>> f.clean('2000')
100+u'2000'
101+>>> f.clean('abcd')
102+Traceback (most recent call last):
103+...
104+ValidationError: [u'Enter a 4 digit post code.']
105+>>> f.clean('20001')
106+Traceback (most recent call last):
107+...
108+ValidationError: [u'Enter a 4 digit post code.']
109+>>> f.clean(None)
110+Traceback (most recent call last):
111+...
112+ValidationError: [u'This field is required.']
113+>>> f.clean('')
114+Traceback (most recent call last):
115+...
116+ValidationError: [u'This field is required.']
117+
118+>>> f = NZPostCodeField(required=False)
119+>>> f.clean('1234')
120+u'1234'
121+>>> f.clean('2000')
122+u'2000'
123+>>> f.clean('abcd')
124+Traceback (most recent call last):
125+...
126+ValidationError: [u'Enter a 4 digit post code.']
127+>>> f.clean('20001')
128+Traceback (most recent call last):
129+...
130+ValidationError: [u'Enter a 4 digit post code.']
131+>>> f.clean(None)
132+u''
133+>>> f.clean('')
134+u''
135+
136+## NZPhoneNumberField ########################################################
137+
138+A field that accepts a 9 digit New Zealand phone number, or a 8-10 digit
139+New Zealand mobile phone number (starting with 02).
140+Allows spaces and parentheses around area code.
141+
142+>>> from django.contrib.localflavor.nz.forms import NZPhoneNumberField
143+>>> f = NZPhoneNumberField()
144+>>> f.clean('012345678')
145+u'012345678'
146+>>> f.clean('0213456789')
147+u'0213456789'
148+>>> f.clean('02 13 45 67 8')
149+u'021345678'
150+>>> f.clean('(02) 1345 6789')
151+u'0213456789'
152+>>> f.clean('(02) 1345-6789')
153+u'0213456789'
154+>>> f.clean('(02)1345-6789')
155+u'0213456789'
156+>>> f.clean('0228 123 45')
157+u'022812345'
158+>>> f.clean('123')
159+Traceback (most recent call last):
160+...
161+ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
162+>>> f.clean('1800DJANGO')
163+Traceback (most recent call last):
164+...
165+ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
166+>>> f.clean(None)
167+Traceback (most recent call last):
168+...
169+ValidationError: [u'This field is required.']
170+>>> f.clean('')
171+Traceback (most recent call last):
172+...
173+ValidationError: [u'This field is required.']
174+
175+>>> f = NZPhoneNumberField(required=False)
176+>>> f.clean('1234567890')
177+Traceback (most recent call last):
178+...
179+ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
180+>>> f.clean('0213456789')
181+u'0213456789'
182+>>> f.clean('02 13 45 67 89')
183+u'0213456789'
184+>>> f.clean('(02) 1345 6789')
185+u'0213456789'
186+>>> f.clean('(02) 1345-6789')
187+u'0213456789'
188+>>> f.clean('(02)1345-6789')
189+u'0213456789'
190+>>> f.clean('0408 123 456')
191+Traceback (most recent call last):
192+...
193+ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
194+
195+>>> f.clean('123')
196+Traceback (most recent call last):
197+...
198+ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
199+>>> f.clean('1800DJANGO')
200+Traceback (most recent call last):
201+...
202+ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
203+>>> f.clean(None)
204+u''
205+>>> f.clean('')
206+u''
207+
208+
209+## NZStateSelect #############################################################
210+
211+NZProvincialDistrictSelect is a Select widget that uses a list of New Zealand
212+provincial districts as its choices.
213+
214+>>> from django.contrib.localflavor.nz.forms import NZProvincialDistrictSelect
215+>>> f = NZProvincialDistrictSelect()
216+>>> print f.render('provincial_district', 'Wellington')
217+<select name="provincial_district">
218+<option value="Auckland">Auckland Province</option>
219+<option value="Canterbury">Canterbury</option>
220+<option value="Chatham">Chatham Islands</option>
221+<option value="HawkesBay">Hawke's Bay</option>
222+<option value="Marlborough">Marlborough</option>
223+<option value="Nelson">Nelson</option>
224+<option value="Northland">Northland</option>
225+<option value="Otago">Otago Province</option>
226+<option value="SouthCanterbury">South Canterbury</option>
227+<option value="Southland">Southland</option>
228+<option value="Taranaki">Taranaki (New Plymouth)</option>
229+<option value="Wellington" selected="selected">Wellington Province</option>
230+<option value="Westland">Westland</option>
231+</select>
232+
233+"""
234\ No newline at end of file
235Index: tests/regressiontests/forms/tests.py
236===================================================================
237--- tests/regressiontests/forms/tests.py        (revision 13336)
238+++ tests/regressiontests/forms/tests.py        (working copy)
239@@ -22,6 +22,7 @@
240 from localflavor.jp import tests as localflavor_jp_tests
241 from localflavor.kw import tests as localflavor_kw_tests
242 from localflavor.nl import tests as localflavor_nl_tests
243+from localflavor.nz import tests as localflavor_nz_tests
244 from localflavor.pl import tests as localflavor_pl_tests
245 from localflavor.pt import tests as localflavor_pt_tests
246 from localflavor.ro import tests as localflavor_ro_tests
247@@ -65,6 +66,7 @@
248     'localflavor_jp_tests': localflavor_jp_tests,
249     'localflavor_kw_tests': localflavor_kw_tests,
250     'localflavor_nl_tests': localflavor_nl_tests,
251+    'localflavor_nz_tests': localflavor_nz_tests,
252     'localflavor_pl_tests': localflavor_pl_tests,
253     'localflavor_pt_tests': localflavor_pt_tests,
254     'localflavor_ro_tests': localflavor_ro_tests,