Ticket #10895: localflavor_br_forms_r10620_updated.diff
File localflavor_br_forms_r10620_updated.diff, 7.4 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/br/forms.py
86 86 """ 87 87 This field validate a CPF number or a CPF string. A CPF number is 88 88 compounded by XXX.XXX.XXX-VD. The two last digits are check digits. 89 By default, the cleaned value is in the same format as entered. 90 If you'd like to keep a unique format for cleaned values use 91 the parameter "return_format" to provide the string format of the cleaned 92 value. 93 94 You can use one of these class constants to the "return_format" parameter: 95 96 DIGITS_ONLY = '%s%s%s%s' (ex. XXXXXXXXXXVD) 97 DOTS_AND_DASH = '%s.%s.%s-%s' (ex. XXX.XXX.XXX-VD) 89 98 90 99 More information: 91 100 http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas … … 96 105 'digits_only': _("This field requires only numbers."), 97 106 } 98 107 99 def __init__(self, *args, **kwargs):100 super(BRCPFField, self).__init__(max_length=14, min_length=11, *args, **kwargs)108 DIGITS_ONLY = u'%s%s%s%s' 109 DOTS_AND_DASH = u'%s.%s.%s-%s' 101 110 111 def __init__(self, return_format=None, *args, **kwargs): 112 self.return_format = return_format 113 super(BRCPFField, self).__init__(max_length=14, min_length=11, 114 *args, **kwargs) 115 102 116 def clean(self, value): 103 117 """ 104 118 Value can be either a string in the format XXX.XXX.XXX-XX or an … … 116 130 raise ValidationError(self.error_messages['digits_only']) 117 131 if len(value) != 11: 118 132 raise ValidationError(self.error_messages['max_digits']) 133 134 # exclude cases of repeated digits form 0 to 9 135 if value in [str(d) * 11 for d in range(10)]: 136 raise ValidationError(self.error_messages['invalid']) 137 119 138 orig_dv = value[-2:] 120 121 139 new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(10, 1, -1))]) 122 140 new_1dv = DV_maker(new_1dv % 11) 123 141 value = value[:-2] + str(new_1dv) + value[-1] … … 127 145 if value[-2:] != orig_dv: 128 146 raise ValidationError(self.error_messages['invalid']) 129 147 148 # return a formatted string if asked 149 if self.return_format: 150 value = re.sub(r'[-\.]', '', orig_value) 151 return self.return_format % tuple(re.findall(r'(\d{2,3})', value)) 152 130 153 return orig_value 131 154 132 class BRCNPJField(Field): 155 class BRCNPJField(CharField): 156 """ 157 This field validates a CNPJ number or a CNPJ string. A CNPJ number is 158 compounded by XX.XXX.XXX/XXXX-XX. 159 By default, the cleaned value is in the same format as entered. 160 If you'd like to keep a unique format for cleaned values use 161 the parameter "return_format" to provide the string format of the cleaned 162 value. 163 164 You can use one of these class constants to the "return_format" parameter: 165 166 DIGITS_ONLY = '%s%s%s%s%s' (ex. XXXXXXXXXXXXXX) 167 DOTS_SLASH_DASH = '%s.%s.%s/%s-%s' (ex. XX.XXX.XXX/XXXX-XX) 168 169 """ 133 170 default_error_messages = { 134 171 'invalid': _("Invalid CNPJ number."), 135 172 'digits_only': _("This field requires only numbers."), 136 173 'max_digits': _("This field requires at least 14 digits"), 137 174 } 138 175 176 DIGITS_ONLY = u'%s%s%s%s%s' 177 DOTS_SLASH_DASH = u'%s.%s.%s/%s-%s' 178 179 def __init__(self, return_format=None, *args, **kwargs): 180 self.return_format = return_format 181 super(BRCNPJField, self).__init__(max_length=18, min_length=14, 182 *args, **kwargs) 183 139 184 def clean(self, value): 140 185 """ 141 186 Value can be either a string in the format XX.XXX.XXX/XXXX-XX or a … … 163 208 value = value[:-1] + str(new_2dv) 164 209 if value[-2:] != orig_dv: 165 210 raise ValidationError(self.error_messages['invalid']) 211 212 if self.return_format: 213 value = re.sub('[-/\.]', '', orig_value) 214 m = re.match(r'(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})', value) 215 return self.return_format % tuple(m.groups()) 166 216 167 217 return orig_value -
tests/regressiontests/forms/localflavor/br.py
85 85 ... 86 86 ValidationError: [u'Invalid CNPJ number.'] 87 87 >>> f.clean('64.132.916/0001-88') 88 '64.132.916/0001-88'88 u'64.132.916/0001-88' 89 89 >>> f.clean('64-132-916/0001-88') 90 '64-132-916/0001-88'90 u'64-132-916/0001-88' 91 91 >>> f.clean('64132916/0001-88') 92 '64132916/0001-88'92 u'64132916/0001-88' 93 93 >>> f.clean('64.132.916/0001-XX') 94 94 Traceback (most recent call last): 95 95 ... … … 97 97 >>> f = BRCNPJField(required=False) 98 98 >>> f.clean('') 99 99 u'' 100 >>> f.clean('64.132.916/0001-8') 101 Traceback (most recent call last): 102 ... 103 ValidationError: [u'This field requires at least 14 digits'] 104 >>> f.clean('6413291600018') 105 Traceback (most recent call last): 106 ... 107 ValidationError: [u'Ensure this value has at least 14 characters (it has 13).'] 108 >>> f.clean('64.132.916/0001-889') 109 Traceback (most recent call last): 110 ... 111 ValidationError: [u'Ensure this value has at most 18 characters (it has 19).'] 112 >>> from django.contrib.localflavor.br.forms import BRCNPJField 113 >>> f = BRCNPJField(return_format=BRCNPJField.DIGITS_ONLY) 114 >>> f.clean('12-345-678/9012-10') 115 Traceback (most recent call last): 116 ... 117 ValidationError: [u'Invalid CNPJ number.'] 118 >>> f.clean('64.132.916/0001-88') 119 u'64132916000188' 120 >>> f.clean('64132916000188') 121 u'64132916000188' 122 >>> f.clean('64132916/0001-88') 123 u'64132916000188' 124 >>> f = BRCNPJField(return_format=BRCNPJField.DOTS_SLASH_DASH) 125 >>> f.clean('64132916/0001-88') 126 u'64.132.916/0001-88' 127 >>> f.clean('64132916000188') 128 u'64.132.916/0001-88' 129 >>> f.clean('64.132.916/0001-88') 130 u'64.132.916/0001-88' 131 >>> f = BRCNPJField(return_format='%s%s%s/%s%s') 132 >>> f.clean('64132916/0001-88') 133 u'64132916/000188' 134 >>> f.clean('64132916000188') 135 u'64132916/000188' 136 >>> f.clean('64.132.916/0001-88') 137 u'64132916/000188' 138 >>> f = BRCNPJField(return_format='%s%s%s/%s%s', required=False) 139 >>> f.clean('') 140 u'' 100 141 101 142 # BRCPFField ################################################################# 102 143 … … 151 192 u'' 152 193 >>> f.clean(None) 153 194 u'' 195 >>> f = BRCPFField(return_format=BRCPFField.DIGITS_ONLY) 196 >>> f.clean('') 197 Traceback (most recent call last): 198 ... 199 ValidationError: [u'This field is required.'] 200 >>> f.clean('663.256.017-26') 201 u'66325601726' 202 >>> f.clean('66325601726') 203 u'66325601726' 204 >>> f = BRCPFField(return_format=BRCPFField.DOTS_AND_DASH) 205 >>> f.clean('663.256.017-26') 206 u'663.256.017-26' 207 >>> f.clean('66325601726') 208 u'663.256.017-26' 209 >>> f = BRCPFField(return_format='%s%s%s-%s') 210 >>> f.clean('663.256.017-26') 211 u'663256017-26' 212 >>> f.clean('66325601726') 213 u'663256017-26' 214 >>> f = BRCPFField(return_format='%s%s%s-%s', required=False) 215 >>> f.clean('') 216 u'' 217 >>> f.clean('000.000.000-00') 218 Traceback (most recent call last): 219 ... 220 ValidationError: [u'Invalid CPF number.'] 221 >>> f.clean('444.444.444-44') 222 Traceback (most recent call last): 223 ... 224 ValidationError: [u'Invalid CPF number.'] 225 >>> f.clean('999.999.999-99') 226 Traceback (most recent call last): 227 ... 228 ValidationError: [u'Invalid CPF number.'] 154 229 155 230 # BRPhoneNumberField ######################################################### 156 231