Fixed #4964 -- Added Brazilian state field to localflavors. Thanks, William Alves de Souza.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6176 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
63dd4f5322
commit
5188a18dbe
|
@ -6,7 +6,7 @@ BR-specific Form helpers
|
||||||
from django.newforms import ValidationError
|
from django.newforms import ValidationError
|
||||||
from django.newforms.fields import Field, RegexField, CharField, Select, EMPTY_VALUES
|
from django.newforms.fields import Field, RegexField, CharField, Select, EMPTY_VALUES
|
||||||
from django.utils.encoding import smart_unicode
|
from django.utils.encoding import smart_unicode
|
||||||
from django.utils.translation import ugettext
|
from django.utils.translation import ugettext as _
|
||||||
import re
|
import re
|
||||||
|
|
||||||
phone_digits_re = re.compile(r'^(\d{2})[-\.]?(\d{4})[-\.]?(\d{4})$')
|
phone_digits_re = re.compile(r'^(\d{2})[-\.]?(\d{4})[-\.]?(\d{4})$')
|
||||||
|
@ -15,7 +15,7 @@ class BRZipCodeField(RegexField):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$',
|
super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$',
|
||||||
max_length=None, min_length=None,
|
max_length=None, min_length=None,
|
||||||
error_message=ugettext('Enter a zip code in the format XXXXX-XXX.'),
|
error_message=_('Enter a zip code in the format XXXXX-XXX.'),
|
||||||
*args, **kwargs)
|
*args, **kwargs)
|
||||||
|
|
||||||
class BRPhoneNumberField(Field):
|
class BRPhoneNumberField(Field):
|
||||||
|
@ -27,7 +27,7 @@ class BRPhoneNumberField(Field):
|
||||||
m = phone_digits_re.search(value)
|
m = phone_digits_re.search(value)
|
||||||
if m:
|
if m:
|
||||||
return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
|
return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
|
||||||
raise ValidationError(ugettext('Phone numbers must be in XX-XXXX-XXXX format.'))
|
raise ValidationError(_('Phone numbers must be in XX-XXXX-XXXX format.'))
|
||||||
|
|
||||||
class BRStateSelect(Select):
|
class BRStateSelect(Select):
|
||||||
"""
|
"""
|
||||||
|
@ -38,6 +38,32 @@ class BRStateSelect(Select):
|
||||||
from br_states import STATE_CHOICES
|
from br_states import STATE_CHOICES
|
||||||
super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
|
super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
|
||||||
|
|
||||||
|
class BRStateChoiceField(Field):
|
||||||
|
"""
|
||||||
|
A choice field that uses a list of Brazilian states as its choices.
|
||||||
|
"""
|
||||||
|
widget = Select
|
||||||
|
|
||||||
|
def __init__(self, required=True, widget=None, label=None,
|
||||||
|
initial=None, help_text=None):
|
||||||
|
super(BRStateChoiceField, self).__init__(required, widget, label,
|
||||||
|
initial, help_text)
|
||||||
|
from br_states import STATE_CHOICES
|
||||||
|
self.widget.choices = STATE_CHOICES
|
||||||
|
|
||||||
|
def clean(self, value):
|
||||||
|
value = super(BRStateChoiceField, self).clean(value)
|
||||||
|
if value in EMPTY_VALUES:
|
||||||
|
value = u''
|
||||||
|
value = smart_unicode(value)
|
||||||
|
if value == u'':
|
||||||
|
return value
|
||||||
|
valid_values = set([smart_unicode(k) for k, v in self.widget.choices])
|
||||||
|
if value not in valid_values:
|
||||||
|
raise ValidationError(_(u'Select a valid brazilian state.'
|
||||||
|
u' That state is not one'
|
||||||
|
u' of the available states.'))
|
||||||
|
return value
|
||||||
|
|
||||||
def DV_maker(v):
|
def DV_maker(v):
|
||||||
if v >= 2:
|
if v >= 2:
|
||||||
|
@ -69,9 +95,9 @@ class BRCPFField(CharField):
|
||||||
try:
|
try:
|
||||||
int(value)
|
int(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValidationError(ugettext("This field requires only numbers."))
|
raise ValidationError(_("This field requires only numbers."))
|
||||||
if len(value) != 11:
|
if len(value) != 11:
|
||||||
raise ValidationError(ugettext("This field requires at most 11 digits or 14 characters."))
|
raise ValidationError(_("This field requires at most 11 digits or 14 characters."))
|
||||||
orig_dv = value[-2:]
|
orig_dv = value[-2:]
|
||||||
|
|
||||||
new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(10, 1, -1))])
|
new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(10, 1, -1))])
|
||||||
|
@ -81,7 +107,7 @@ class BRCPFField(CharField):
|
||||||
new_2dv = DV_maker(new_2dv % 11)
|
new_2dv = DV_maker(new_2dv % 11)
|
||||||
value = value[:-1] + str(new_2dv)
|
value = value[:-1] + str(new_2dv)
|
||||||
if value[-2:] != orig_dv:
|
if value[-2:] != orig_dv:
|
||||||
raise ValidationError(ugettext("Invalid CPF number."))
|
raise ValidationError(_("Invalid CPF number."))
|
||||||
|
|
||||||
return orig_value
|
return orig_value
|
||||||
|
|
||||||
|
@ -103,7 +129,7 @@ class BRCNPJField(Field):
|
||||||
raise ValidationError("This field requires only numbers.")
|
raise ValidationError("This field requires only numbers.")
|
||||||
if len(value) != 14:
|
if len(value) != 14:
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
ugettext("This field requires at least 14 digits"))
|
_("This field requires at least 14 digits"))
|
||||||
orig_dv = value[-2:]
|
orig_dv = value[-2:]
|
||||||
|
|
||||||
new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(5, 1, -1) + range(9, 1, -1))])
|
new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(5, 1, -1) + range(9, 1, -1))])
|
||||||
|
@ -113,7 +139,7 @@ class BRCNPJField(Field):
|
||||||
new_2dv = DV_maker(new_2dv % 11)
|
new_2dv = DV_maker(new_2dv % 11)
|
||||||
value = value[:-1] + str(new_2dv)
|
value = value[:-1] + str(new_2dv)
|
||||||
if value[-2:] != orig_dv:
|
if value[-2:] != orig_dv:
|
||||||
raise ValidationError(ugettext("Invalid CNPJ number."))
|
raise ValidationError(_("Invalid CNPJ number."))
|
||||||
|
|
||||||
return orig_value
|
return orig_value
|
||||||
|
|
||||||
|
|
|
@ -980,6 +980,20 @@ u'41-3562-3464'
|
||||||
>>> w.render('states', 'PR')
|
>>> w.render('states', 'PR')
|
||||||
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>'
|
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>'
|
||||||
|
|
||||||
|
# BRStateChoiceField #########################################################
|
||||||
|
>>> from django.contrib.localflavor.br.forms import BRStateChoiceField
|
||||||
|
>>> f = BRStateChoiceField()
|
||||||
|
>>> ', '.join([f.clean(s) for s, _ in f.widget.choices])
|
||||||
|
u'AC, AL, AP, AM, BA, CE, DF, ES, GO, MA, MT, MS, MG, PA, PB, PR, PE, PI, RJ, RN, RS, RO, RR, SC, SP, SE, TO'
|
||||||
|
>>> f.clean('')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'This field is required.']
|
||||||
|
>>> f.clean('pr')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValidationError: [u'Select a valid brazilian state. That state is not one of the available states.']
|
||||||
|
|
||||||
# DEZipCodeField ##############################################################
|
# DEZipCodeField ##############################################################
|
||||||
|
|
||||||
>>> from django.contrib.localflavor.de.forms import DEZipCodeField
|
>>> from django.contrib.localflavor.de.forms import DEZipCodeField
|
||||||
|
|
Loading…
Reference in New Issue