Ticket #4964: brlocalflavorv2.diff
File brlocalflavorv2.diff, 3.7 KB (added by , 17 years ago) |
---|
-
django/contrib/localflavor/br/forms.py
38 38 from br_states import STATE_CHOICES 39 39 super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES) 40 40 41 class BRStateChoiceField(Field): 42 """ 43 A choice field that uses a list of Brazilian states as its choices. 44 """ 45 widget = Select 41 46 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 42 68 def DV_maker(v): 43 69 if v >= 2: 44 70 return 11 - v -
tests/regressiontests/forms/localflavor.py
980 980 >>> w.render('states', 'PR') 981 981 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>' 982 982 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 ... 989 u'AC' 990 u'AL' 991 u'AP' 992 u'AM' 993 u'BA' 994 u'CE' 995 u'DF' 996 u'ES' 997 u'GO' 998 u'MA' 999 u'MT' 1000 u'MS' 1001 u'MG' 1002 u'PA' 1003 u'PB' 1004 u'PR' 1005 u'PE' 1006 u'PI' 1007 u'RJ' 1008 u'RN' 1009 u'RS' 1010 u'RO' 1011 u'RR' 1012 u'SC' 1013 u'SP' 1014 u'SE' 1015 u'TO' 1016 >>> f.clean('') 1017 Traceback (most recent call last): 1018 ... 1019 ValidationError: [u'This field is required.'] 1020 >>> f.clean('pr') 1021 Traceback (most recent call last): 1022 ... 1023 ValidationError: [u'Select a valid brazilian state. That state is not one of the available states.'] 1024 983 1025 # DEZipCodeField ############################################################## 984 1026 985 1027 >>> from django.contrib.localflavor.de.forms import DEZipCodeField