Ticket #4964: brlocalflavorv2.diff

File brlocalflavorv2.diff, 3.7 KB (added by Wiliam Alves de Souza, 17 years ago)

Changes based on Malcolm Tredinnick comments.

  • django/contrib/localflavor/br/forms.py

     
    3838        from br_states import STATE_CHOICES
    3939        super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
    4040
     41class BRStateChoiceField(Field):
     42    """
     43    A choice field that uses a list of Brazilian states as its choices.
     44    """
     45    widget = Select
    4146
     47    def __init__(self, required=True, widget=None, label=None,
     48                 initial=None, help_text=None):
     49        super(BRStateChoiceField, self).__init__(required, widget, label,
     50                                                 initial, help_text)
     51        from br_states import STATE_CHOICES
     52        self.widget.choices = STATE_CHOICES
     53
     54    def clean(self, value):
     55        value = super(BRStateChoiceField, self).clean(value)
     56        if value in EMPTY_VALUES:
     57            value = u''
     58        value = smart_unicode(value)
     59        if value == u'':
     60            return value
     61        valid_values = set([smart_unicode(k) for k, v in self.widget.choices])
     62        if value not in valid_values:
     63            raise ValidationError(ugettext(u'Select a valid brazilian state.'
     64                                           ' That state is not one'
     65                                           ' of the available states.'))
     66        return value
     67
    4268def DV_maker(v):
    4369    if v >= 2:
    4470        return 11 - v
  • tests/regressiontests/forms/localflavor.py

     
    980980>>> w.render('states', 'PR')
    981981u'<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>'
    982982
     983# BRStateChoiceField #########################################################
     984>>> from django.contrib.localflavor.br.forms import BRStateChoiceField
     985>>> f = BRStateChoiceField()
     986>>> for s, n in f.widget.choices:
     987...   f.clean(s)
     988...
     989u'AC'
     990u'AL'
     991u'AP'
     992u'AM'
     993u'BA'
     994u'CE'
     995u'DF'
     996u'ES'
     997u'GO'
     998u'MA'
     999u'MT'
     1000u'MS'
     1001u'MG'
     1002u'PA'
     1003u'PB'
     1004u'PR'
     1005u'PE'
     1006u'PI'
     1007u'RJ'
     1008u'RN'
     1009u'RS'
     1010u'RO'
     1011u'RR'
     1012u'SC'
     1013u'SP'
     1014u'SE'
     1015u'TO'
     1016>>> f.clean('')
     1017Traceback (most recent call last):
     1018...
     1019ValidationError: [u'This field is required.']
     1020>>> f.clean('pr')
     1021Traceback (most recent call last):
     1022...
     1023ValidationError: [u'Select a valid brazilian state. That state is not one of the available states.']
     1024
    9831025# DEZipCodeField ##############################################################
    9841026
    9851027>>> from django.contrib.localflavor.de.forms import DEZipCodeField
Back to Top