diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-03-31 09:05:54 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-03-31 09:05:54 +0000 |
| commit | 79312f7a4e06e08ec69c15d832013ed30bb6d1ad (patch) | |
| tree | f027860b1ba52d43f6dbbd6421e754bcea05cd22 /django | |
| parent | 7be1f44053e3b099637cee625852f273bf8cad21 (diff) | |
Fixed #3882 -- Added Brazilian localflavor. Thanks, Wiliam Alves de Souza.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4874 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/localflavor/br/__init__.py | 0 | ||||
| -rw-r--r-- | django/contrib/localflavor/br/br_states.py | 39 | ||||
| -rw-r--r-- | django/contrib/localflavor/br/forms.py | 39 |
3 files changed, 78 insertions, 0 deletions
diff --git a/django/contrib/localflavor/br/__init__.py b/django/contrib/localflavor/br/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/django/contrib/localflavor/br/__init__.py diff --git a/django/contrib/localflavor/br/br_states.py b/django/contrib/localflavor/br/br_states.py new file mode 100644 index 0000000000..c6ce0a1bb7 --- /dev/null +++ b/django/contrib/localflavor/br/br_states.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +""" +A brazilian mapping of state misspellings/abbreviations to normalized +abbreviations, and an alphabetical list of states for use as `choices +in a formfield. + +This exists in this standalone file so that it's only imported into +memory when explicitly needed. +""" + +STATE_CHOICES = ( + ('AC', 'Acre'), + ('AL', 'Alagoas'), + ('AP', u'Amapá'), + ('AM', 'Amazonas'), + ('BA', 'Bahia'), + ('CE', u'Ceará'), + ('DF', 'Distrito Federal'), + ('ES', u'Espírito Santo'), + ('GO', u'Goiás'), + ('MA', u'Maranhão'), + ('MT', 'Mato Grosso'), + ('MS', 'Mato Grosso do Sul'), + ('MG', 'Minas Gerais'), + ('PA', u'Pará'), + ('PB', u'Paraíba'), + ('PR', u'Paraná'), + ('PE', 'Pernambuco'), + ('PI', u'Piauí'), + ('RJ', 'Rio de Janeiro'), + ('RN', 'Rio Grande do Norte'), + ('RS', 'Rio Grande do Sul'), + ('RO', u'Rondônia'), + ('RR', 'Roraima'), + ('SC', 'Santa Catarina'), + ('SP', u'São Paulo'), + ('SE', 'Sergipe'), + ('TO', 'Tocantins'), +) diff --git a/django/contrib/localflavor/br/forms.py b/django/contrib/localflavor/br/forms.py new file mode 100644 index 0000000000..d3ca28f053 --- /dev/null +++ b/django/contrib/localflavor/br/forms.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +""" +BR-specific Form helpers +""" + +from django.newforms import ValidationError +from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES +from django.newforms.util import smart_unicode +from django.utils.translation import gettext +import re + +phone_digits_re = re.compile(r'^(\d{2})[-\.]?(\d{4})[-\.]?(\d{4})$') + +class BRZipCodeField(RegexField): + def __init__(self, *args, **kwargs): + super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$', + max_length=None, min_length=None, + error_message=u'Informe um código postal no formato XXXXX-XXX.', + *args, **kwargs) + +class BRPhoneNumberField(Field): + def clean(self, value): + super(BRPhoneNumberField, self).clean(value) + if value in EMPTY_VALUES: + return u'' + value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) + m = phone_digits_re.search(value) + if m: + return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) + raise ValidationError(u'Números de telefone devem estar no formato XX-XXXX-XXXX.') + +class BRStateSelect(Select): + """ + A Select widget that uses a list of brazilian states/territories + as its choices. + """ + def __init__(self, attrs=None): + from br_states import STATE_CHOICES # relative import + super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES) |
