Code

Ticket #8735: django_localflavor_pt.diff

File django_localflavor_pt.diff, 6.8 KB (added by alcides, 6 years ago)

Patch to add Portuguese localflavor, along with tests, based on the French localflavor

Line 
1Index: django/contrib/localflavor/pt/forms.pyc
2===================================================================
3Cannot display: file marked as a binary type.
4svn:mime-type = application/octet-stream
5
6Property changes on: django/contrib/localflavor/pt/forms.pyc
7___________________________________________________________________
8Name: svn:mime-type
9   + application/octet-stream
10
11Index: django/contrib/localflavor/pt/__init__.py
12===================================================================
13Index: django/contrib/localflavor/pt/__init__.pyc
14===================================================================
15Cannot display: file marked as a binary type.
16svn:mime-type = application/octet-stream
17
18Property changes on: django/contrib/localflavor/pt/__init__.pyc
19___________________________________________________________________
20Name: svn:mime-type
21   + application/octet-stream
22
23Index: django/contrib/localflavor/pt/forms.py
24===================================================================
25--- django/contrib/localflavor/pt/forms.py      (revision 0)
26+++ django/contrib/localflavor/pt/forms.py      (revision 0)
27@@ -0,0 +1,48 @@
28+"""
29+PT-specific Form helpers
30+"""
31+
32+from django.forms import ValidationError
33+from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
34+from django.utils.encoding import smart_unicode
35+from django.utils.translation import ugettext_lazy as _
36+import re
37+
38+phone_digits_re = re.compile(r'^(\d{9}|(00|\+)\d*)$')
39+
40+
41+class PTZipCodeField(RegexField):
42+    default_error_messages = {
43+        'invalid': _('Enter a zip code in the format XXXX-XXX.'),
44+    }
45+
46+    def __init__(self, *args, **kwargs):
47+        super(PTZipCodeField, self).__init__(r'^(\d{4}-\d{3}|\d{7})$',
48+            max_length=None, min_length=None, *args, **kwargs)
49+
50+    def clean(self,value):
51+        cleaned = super(PTZipCodeField, self).clean(value)
52+        if len(cleaned) == 7:
53+              return u'%s-%s' % (cleaned[:4],cleaned[4:])
54+        else:
55+              return cleaned
56+               
57+class PTPhoneNumberField(Field):
58+    """
59+    Validate local Portuguese phone number (including international ones)
60+       It should have 9 digits (may include spaces) or start by 00 or + (international)
61+    """
62+    default_error_messages = {
63+        'invalid': u'Phone numbers must have 9 digits, or start by + or 00.',
64+    }
65+
66+    def clean(self, value):
67+        super(PTPhoneNumberField, self).clean(value)
68+        if value in EMPTY_VALUES:
69+            return u''
70+        value = re.sub('(\.|\s)', '', smart_unicode(value))
71+        m = phone_digits_re.search(value)
72+        if m:
73+            return u'%s' % value
74+        raise ValidationError(self.error_messages['invalid'])
75+
76Index: tests/regressiontests/forms/localflavor/pt.py
77===================================================================
78--- tests/regressiontests/forms/localflavor/pt.py       (revision 0)
79+++ tests/regressiontests/forms/localflavor/pt.py       (revision 0)
80@@ -0,0 +1,106 @@
81+# -*- coding: utf-8 -*-
82+# Tests for the contrib/localflavor/ PT form fields.
83+
84+tests = r"""
85+# PTZipCodeField #############################################################
86+
87+PTZipCodeField validates that the data is a valid PT zipcode.
88+>>> from django.contrib.localflavor.pt.forms import PTZipCodeField
89+>>> f = PTZipCodeField()
90+>>> f.clean('3030-034')
91+u'3030-034'
92+>>> f.clean('1003456')
93+u'1003-456'
94+>>> f.clean('2A200')
95+Traceback (most recent call last):
96+...
97+ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
98+>>> f.clean('980001')
99+Traceback (most recent call last):
100+...
101+ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
102+>>> f.clean(None)
103+Traceback (most recent call last):
104+...
105+ValidationError: [u'This field is required.']
106+>>> f.clean('')
107+Traceback (most recent call last):
108+...
109+ValidationError: [u'This field is required.']
110+
111+>>> f = PTZipCodeField(required=False)
112+>>> f.clean('3030-034')
113+u'3030-034'
114+>>> f.clean('1003456')
115+u'1003-456'
116+>>> f.clean('2A200')
117+Traceback (most recent call last):
118+...
119+ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
120+>>> f.clean('980001')
121+Traceback (most recent call last):
122+...
123+ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
124+>>> f.clean(None)
125+u''
126+>>> f.clean('')
127+u''
128+
129+# PTPhoneNumberField ##########################################################
130+
131+PTPhoneNumberField validates that the data is a valid Portuguese phone number.
132+It's normalized to XXXXXXXXX format  or +X(X) for international numbers. Dots are valid too.
133+>>> from django.contrib.localflavor.pt.forms import PTPhoneNumberField
134+>>> f = PTPhoneNumberField()
135+>>> f.clean('917845189')
136+u'917845189'
137+>>> f.clean('91 784 5189')
138+u'917845189'
139+>>> f.clean('91 784 5189')
140+u'917845189'
141+>>> f.clean('+351 91 111')
142+u'+35191111'
143+>>> f.clean('00351873')
144+u'00351873'
145+>>> f.clean('91 784 51 8')
146+Traceback (most recent call last):
147+...
148+ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
149+>>> f.clean('091 456 987 1')
150+Traceback (most recent call last):
151+...
152+ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
153+>>> f.clean(None)
154+Traceback (most recent call last):
155+...
156+ValidationError: [u'This field is required.']
157+>>> f.clean('')
158+Traceback (most recent call last):
159+...
160+ValidationError: [u'This field is required.']
161+
162+>>> f = PTPhoneNumberField(required=False)
163+>>> f.clean('917845189')
164+u'917845189'
165+>>> f.clean('91 784 5189')
166+u'917845189'
167+>>> f.clean('91 784 5189')
168+u'917845189'
169+>>> f.clean('+351 91 111')
170+u'+35191111'
171+>>> f.clean('00351873')
172+u'00351873'
173+>>> f.clean('91 784 51 8')
174+Traceback (most recent call last):
175+...
176+ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
177+>>> f.clean('091 456 987 1')
178+Traceback (most recent call last):
179+...
180+ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
181+>>> f.clean(None)
182+u''
183+>>> f.clean('')
184+u''
185+
186+"""
187Index: tests/regressiontests/forms/tests.py
188===================================================================
189--- tests/regressiontests/forms/tests.py        (revision 8770)
190+++ tests/regressiontests/forms/tests.py        (working copy)
191@@ -20,6 +20,7 @@
192 from localflavor.jp import tests as localflavor_jp_tests
193 from localflavor.nl import tests as localflavor_nl_tests
194 from localflavor.pl import tests as localflavor_pl_tests
195+from localflavor.pt import tests as localflavor_pt_tests
196 from localflavor.ro import tests as localflavor_ro_tests
197 from localflavor.sk import tests as localflavor_sk_tests
198 from localflavor.uk import tests as localflavor_uk_tests
199@@ -53,6 +54,7 @@
200     'localflavor_jp_tests': localflavor_jp_tests,
201     'localflavor_nl_tests': localflavor_nl_tests,
202     'localflavor_pl_tests': localflavor_pl_tests,
203+    'localflavor_pt_tests': localflavor_pt_tests,
204     'localflavor_ro_tests': localflavor_ro_tests,
205     'localflavor_sk_tests': localflavor_sk_tests,
206     'localflavor_uk_tests': localflavor_uk_tests,