Code

Ticket #3882: localflavor_br.diff

File localflavor_br.diff, 7.7 KB (added by Wiliam Alves de Souza <wiliamsouza83@…>, 7 years ago)
Line 
1Index: django/contrib/localflavor/br/__init__.py
2===================================================================
3Index: django/contrib/localflavor/br/forms.py
4===================================================================
5--- django/contrib/localflavor/br/forms.py      (revision 0)
6+++ django/contrib/localflavor/br/forms.py      (revision 0)
7@@ -0,0 +1,39 @@
8+# -*- coding: utf-8 -*-
9+"""
10+BR-specific Form helpers
11+"""
12+
13+from django.newforms import ValidationError
14+from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
15+from django.newforms.util import smart_unicode
16+from django.utils.translation import gettext
17+import re
18+
19+phone_digits_re = re.compile(r'^(\d{2})[-\.]?(\d{4})[-\.]?(\d{4})$')
20+
21+class BRZipCodeField(RegexField):
22+    def __init__(self, *args, **kwargs):
23+        super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$',
24+            max_length=None, min_length=None,
25+            error_message=u'Informe um código postal no formato XXXXX-XXX.',
26+            *args, **kwargs)
27+
28+class BRPhoneNumberField(Field):
29+    def clean(self, value):
30+        super(BRPhoneNumberField, self).clean(value)
31+        if value in EMPTY_VALUES:
32+            return u''
33+        value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
34+        m = phone_digits_re.search(value)
35+        if m:
36+            return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
37+        raise ValidationError(u'Números de telefone devem estar no formato XX-XXXX-XXXX.')
38+
39+class BRStateSelect(Select):
40+    """
41+    A Select widget that uses a list of brazilian states/territories
42+    as its choices.
43+    """
44+    def __init__(self, attrs=None):
45+        from br_states import STATE_CHOICES # relative import
46+        super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
47Index: django/contrib/localflavor/br/br_states.py
48===================================================================
49--- django/contrib/localflavor/br/br_states.py  (revision 0)
50+++ django/contrib/localflavor/br/br_states.py  (revision 0)
51@@ -0,0 +1,39 @@
52+# -*- coding: utf-8 -*-
53+"""
54+A brazilian mapping of state misspellings/abbreviations to normalized
55+abbreviations, and an alphabetical list of states for use as `choices
56+in a formfield.
57+
58+This exists in this standalone file so that it's only imported into
59+memory when explicitly needed.
60+"""
61+
62+STATE_CHOICES = (
63+    ('AC', 'Acre'),
64+    ('AL', 'Alagoas'),
65+    ('AP', u'Amapá'),
66+    ('AM', 'Amazonas'),
67+    ('BA', 'Bahia'),
68+    ('CE', u'Ceará'),
69+    ('DF', 'Distrito Federal'),
70+    ('ES', u'Espírito Santo'),
71+    ('GO', u'Goiás'),
72+    ('MA', u'Maranhão'),
73+    ('MT', 'Mato Grosso'),
74+    ('MS', 'Mato Grosso do Sul'),
75+    ('MG', 'Minas Gerais'),
76+    ('PA', u'Pará'),
77+    ('PB', u'Paraíba'),
78+    ('PR', u'Paraná'),
79+    ('PE', 'Pernambuco'),
80+    ('PI', u'Piauí'),
81+    ('RJ', 'Rio de Janeiro'),
82+    ('RN', 'Rio Grande do Norte'),
83+    ('RS', 'Rio Grande do Sul'),
84+    ('RO', u'Rondônia'),
85+    ('RR', 'Roraima'),
86+    ('SC', 'Santa Catarina'),
87+    ('SP', u'São Paulo'),
88+    ('SE', 'Sergipe'),
89+    ('TO', 'Tocantins'),
90+)
91Index: tests/regressiontests/forms/tests.py
92===================================================================
93--- tests/regressiontests/forms/tests.py        (revision 4872)
94+++ tests/regressiontests/forms/tests.py        (working copy)
95@@ -3974,6 +3974,120 @@
96 >>> f.clean('')
97 u''
98 
99+# BRZipCodeField ############################################################
100+>>> from django.contrib.localflavor.br.forms import BRZipCodeField
101+>>> f = BRZipCodeField()
102+>>> f.clean('12345-123')
103+u'12345-123'
104+>>> f.clean('12345_123')
105+Traceback (most recent call last):
106+...
107+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
108+>>> f.clean('1234-123')
109+Traceback (most recent call last):
110+...
111+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
112+>>> f.clean('abcde-abc')
113+Traceback (most recent call last):
114+...
115+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
116+>>> f.clean('12345-')
117+Traceback (most recent call last):
118+...
119+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
120+>>> f.clean('-123')
121+Traceback (most recent call last):
122+...
123+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
124+>>> f.clean('')
125+Traceback (most recent call last):
126+...
127+ValidationError: [u'This field is required.']
128+>>> f.clean(None)
129+Traceback (most recent call last):
130+...
131+ValidationError: [u'This field is required.']
132+
133+>>> f = BRZipCodeField(required=False)
134+>>> f.clean(None)
135+u''
136+>>> f.clean('')
137+u''
138+>>> f.clean('-123')
139+Traceback (most recent call last):
140+...
141+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
142+>>> f.clean('12345-')
143+Traceback (most recent call last):
144+...
145+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
146+>>> f.clean('abcde-abc')
147+Traceback (most recent call last):
148+...
149+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
150+>>> f.clean('1234-123')
151+Traceback (most recent call last):
152+...
153+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
154+>>> f.clean('12345_123')
155+Traceback (most recent call last):
156+...
157+ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
158+>>> f.clean('12345-123')
159+u'12345-123'
160+
161+# BRPhoneNumberField #########################################################
162+
163+>>> from django.contrib.localflavor.br.forms import BRPhoneNumberField
164+>>> f = BRPhoneNumberField()
165+>>> f.clean('41-3562-3464')
166+u'41-3562-3464'
167+>>> f.clean('4135623464')
168+u'41-3562-3464'
169+>>> f.clean('41 3562-3464')
170+u'41-3562-3464'
171+>>> f.clean('41 3562 3464')
172+u'41-3562-3464'
173+>>> f.clean('(41) 3562 3464')
174+u'41-3562-3464'
175+>>> f.clean('41.3562.3464')
176+u'41-3562-3464'
177+>>> f.clean('41.3562-3464')
178+u'41-3562-3464'
179+>>> f.clean(' (41) 3562.3464')
180+u'41-3562-3464'
181+>>> f.clean(None)
182+Traceback (most recent call last):
183+...
184+ValidationError: [u'This field is required.']
185+>>> f.clean('')
186+Traceback (most recent call last):
187+...
188+ValidationError: [u'This field is required.']
189+
190+>>> f = BRPhoneNumberField(required=False)
191+>>> f.clean('')
192+u''
193+>>> f.clean(None)
194+u''
195+>>> f.clean(' (41) 3562.3464')
196+u'41-3562-3464'
197+>>> f.clean('41.3562-3464')
198+u'41-3562-3464'
199+>>> f.clean('(41) 3562 3464')
200+u'41-3562-3464'
201+>>> f.clean('4135623464')
202+u'41-3562-3464'
203+>>> f.clean('41 3562-3464')
204+u'41-3562-3464'
205+
206+# BRStateSelect ##############################################################
207+
208+>>> from django.contrib.localflavor.br.forms import BRStateSelect
209+>>> w = BRStateSelect()
210+>>> w.render('states', 'PR')
211+u'<select name="states">\n<option value="AC">Acre</option>\n<option value="AL">Alagoas</option>\n<option value="AP">Amap\xe1</option>\n<option value="AM">Amazonas</option>\n<option value="BA">Bahia</option>\n<option value="CE">Cear\xe1</option>\n<option value="DF">Distrito Federal</option>\n<option value="ES">Esp\xedrito Santo</option>\n<option value="GO">Goi\xe1s</option>\n<option value="MA">Maranh\xe3o</option>\n<option value="MT">Mato Grosso</option>\n<option value="MS">Mato Grosso do Sul</option>\n<option value="MG">Minas Gerais</option>\n<option value="PA">Par\xe1</option>\n<option value="PB">Para\xedba</option>\n<option value="PR" selected="selected">Paran\xe1</option>\n<option value="PE">Pernambuco</option>\n<option value="PI">Piau\xed</option>\n<option value="RJ">Rio de Janeiro</option>\n<option value="RN">Rio Grande do Norte</option>\n<option value="RS">Rio Grande do Sul</option>\n<option value="RO">Rond\xf4nia</option>\n<option value="RR">Roraima</option>\n<option value="SC">Santa Catarina</option>\n<option value="SP">S\xe3o Paulo</option>\n<option value="SE">Sergipe</option>\n<option value="TO">Tocantins</option>\n</select>'
212+
213 #################################
214 # Tests of underlying functions #
215 #################################