Ticket #3882: localflavor_br.diff

File localflavor_br.diff, 7.7 KB (added by Wiliam Alves de Souza <wiliamsouza83@…>, 18 years ago)
  • django/contrib/localflavor/br/forms.py

     
     1# -*- coding: utf-8 -*-
     2"""
     3BR-specific Form helpers
     4"""
     5
     6from django.newforms import ValidationError
     7from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
     8from django.newforms.util import smart_unicode
     9from django.utils.translation import gettext
     10import re
     11
     12phone_digits_re = re.compile(r'^(\d{2})[-\.]?(\d{4})[-\.]?(\d{4})$')
     13
     14class BRZipCodeField(RegexField):
     15    def __init__(self, *args, **kwargs):
     16        super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$',
     17            max_length=None, min_length=None,
     18            error_message=u'Informe um código postal no formato XXXXX-XXX.',
     19            *args, **kwargs)
     20
     21class BRPhoneNumberField(Field):
     22    def clean(self, value):
     23        super(BRPhoneNumberField, self).clean(value)
     24        if value in EMPTY_VALUES:
     25            return u''
     26        value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
     27        m = phone_digits_re.search(value)
     28        if m:
     29            return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
     30        raise ValidationError(u'Números de telefone devem estar no formato XX-XXXX-XXXX.')
     31
     32class BRStateSelect(Select):
     33    """
     34    A Select widget that uses a list of brazilian states/territories
     35    as its choices.
     36    """
     37    def __init__(self, attrs=None):
     38        from br_states import STATE_CHOICES # relative import
     39        super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
  • django/contrib/localflavor/br/br_states.py

     
     1# -*- coding: utf-8 -*-
     2"""
     3A brazilian mapping of state misspellings/abbreviations to normalized
     4abbreviations, and an alphabetical list of states for use as `choices
     5in a formfield.
     6
     7This exists in this standalone file so that it's only imported into
     8memory when explicitly needed.
     9"""
     10
     11STATE_CHOICES = (
     12    ('AC', 'Acre'),
     13    ('AL', 'Alagoas'),
     14    ('AP', u'Amapá'),
     15    ('AM', 'Amazonas'),
     16    ('BA', 'Bahia'),
     17    ('CE', u'Ceará'),
     18    ('DF', 'Distrito Federal'),
     19    ('ES', u'Espírito Santo'),
     20    ('GO', u'Goiás'),
     21    ('MA', u'Maranhão'),
     22    ('MT', 'Mato Grosso'),
     23    ('MS', 'Mato Grosso do Sul'),
     24    ('MG', 'Minas Gerais'),
     25    ('PA', u'Pará'),
     26    ('PB', u'Paraíba'),
     27    ('PR', u'Paraná'),
     28    ('PE', 'Pernambuco'),
     29    ('PI', u'Piauí'),
     30    ('RJ', 'Rio de Janeiro'),
     31    ('RN', 'Rio Grande do Norte'),
     32    ('RS', 'Rio Grande do Sul'),
     33    ('RO', u'Rondônia'),
     34    ('RR', 'Roraima'),
     35    ('SC', 'Santa Catarina'),
     36    ('SP', u'São Paulo'),
     37    ('SE', 'Sergipe'),
     38    ('TO', 'Tocantins'),
     39)
  • tests/regressiontests/forms/tests.py

     
    39743974>>> f.clean('')
    39753975u''
    39763976
     3977# BRZipCodeField ############################################################
     3978>>> from django.contrib.localflavor.br.forms import BRZipCodeField
     3979>>> f = BRZipCodeField()
     3980>>> f.clean('12345-123')
     3981u'12345-123'
     3982>>> f.clean('12345_123')
     3983Traceback (most recent call last):
     3984...
     3985ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     3986>>> f.clean('1234-123')
     3987Traceback (most recent call last):
     3988...
     3989ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     3990>>> f.clean('abcde-abc')
     3991Traceback (most recent call last):
     3992...
     3993ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     3994>>> f.clean('12345-')
     3995Traceback (most recent call last):
     3996...
     3997ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     3998>>> f.clean('-123')
     3999Traceback (most recent call last):
     4000...
     4001ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     4002>>> f.clean('')
     4003Traceback (most recent call last):
     4004...
     4005ValidationError: [u'This field is required.']
     4006>>> f.clean(None)
     4007Traceback (most recent call last):
     4008...
     4009ValidationError: [u'This field is required.']
     4010
     4011>>> f = BRZipCodeField(required=False)
     4012>>> f.clean(None)
     4013u''
     4014>>> f.clean('')
     4015u''
     4016>>> f.clean('-123')
     4017Traceback (most recent call last):
     4018...
     4019ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     4020>>> f.clean('12345-')
     4021Traceback (most recent call last):
     4022...
     4023ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     4024>>> f.clean('abcde-abc')
     4025Traceback (most recent call last):
     4026...
     4027ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     4028>>> f.clean('1234-123')
     4029Traceback (most recent call last):
     4030...
     4031ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     4032>>> f.clean('12345_123')
     4033Traceback (most recent call last):
     4034...
     4035ValidationError: [u'Informe um c\xf3digo postal no formato XXXXX-XXX.']
     4036>>> f.clean('12345-123')
     4037u'12345-123'
     4038
     4039# BRPhoneNumberField #########################################################
     4040
     4041>>> from django.contrib.localflavor.br.forms import BRPhoneNumberField
     4042>>> f = BRPhoneNumberField()
     4043>>> f.clean('41-3562-3464')
     4044u'41-3562-3464'
     4045>>> f.clean('4135623464')
     4046u'41-3562-3464'
     4047>>> f.clean('41 3562-3464')
     4048u'41-3562-3464'
     4049>>> f.clean('41 3562 3464')
     4050u'41-3562-3464'
     4051>>> f.clean('(41) 3562 3464')
     4052u'41-3562-3464'
     4053>>> f.clean('41.3562.3464')
     4054u'41-3562-3464'
     4055>>> f.clean('41.3562-3464')
     4056u'41-3562-3464'
     4057>>> f.clean(' (41) 3562.3464')
     4058u'41-3562-3464'
     4059>>> f.clean(None)
     4060Traceback (most recent call last):
     4061...
     4062ValidationError: [u'This field is required.']
     4063>>> f.clean('')
     4064Traceback (most recent call last):
     4065...
     4066ValidationError: [u'This field is required.']
     4067
     4068>>> f = BRPhoneNumberField(required=False)
     4069>>> f.clean('')
     4070u''
     4071>>> f.clean(None)
     4072u''
     4073>>> f.clean(' (41) 3562.3464')
     4074u'41-3562-3464'
     4075>>> f.clean('41.3562-3464')
     4076u'41-3562-3464'
     4077>>> f.clean('(41) 3562 3464')
     4078u'41-3562-3464'
     4079>>> f.clean('4135623464')
     4080u'41-3562-3464'
     4081>>> f.clean('41 3562-3464')
     4082u'41-3562-3464'
     4083
     4084# BRStateSelect ##############################################################
     4085
     4086>>> from django.contrib.localflavor.br.forms import BRStateSelect
     4087>>> w = BRStateSelect()
     4088>>> w.render('states', 'PR')
     4089u'<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>'
     4090
    39774091#################################
    39784092# Tests of underlying functions #
    39794093#################################
Back to Top