def clean(self, value): """ Value can be either a string in the format XX.XXX.XXX/XXXX-XX or a group of 14 characters. """ value = super(BrCnpjField, self).clean(value) if value in EMPTY_VALUES: return u'' if len(value) == 14: # formato "XXXXXXXXXXXXXX" value = mask_cnpj(value) if len(value) != 18: # formato "XX.XXX.XXX/XXXX-XX" raise forms.ValidationError(self.error_messages['invalid']) if not re.match('\d\d.\d\d\d.\d\d\d/\d\d\d\d-\d\d', value): raise forms.ValidationError(self.error_messages['invalid']) orig_value = value[:] if not value.isdigit(): value = re.sub("[-\./]", "", value) try: int(value) except ValueError: raise forms.ValidationError(self.error_messages['digits_only']) if len(value) != 14: raise forms.ValidationError(self.error_messages['max_digits']) orig_dv = value[-2:] new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(5, 1, -1) + range(9, 1, -1))]) new_1dv = DV_maker(new_1dv % 11) value = value[:-2] + str(new_1dv) + value[-1] new_2dv = sum([i * int(value[idx]) for idx, i in enumerate(range(6, 1, -1) + range(9, 1, -1))]) new_2dv = DV_maker(new_2dv % 11) value = value[:-1] + str(new_2dv) if value[-2:] != orig_dv: raise forms.ValidationError(self.error_messages['invalid']) return orig_value
def clean(self, value): """ Value can be either a string in the format XX.XXX.XXX/XXXX-XX or a group of 14 characters. """ value = super(BrCnpjField, self).clean(value) if value in EMPTY_VALUES: return u'' if len(value) == 14: # formato "XXXXXXXXXXXXXX" value = mask_cnpj(value) if len(value) != 18: # formato "XX.XXX.XXX/XXXX-XX" raise forms.ValidationError(self.error_messages['invalid']) if not re.match('\d\d.\d\d\d.\d\d\d/\d\d\d\d-\d\d', value): raise forms.ValidationError(self.error_messages['invalid']) orig_value = value[:] # grupo1 = value.split('-') # grupo2 = grupo1.split('/') # grupo3 = grupo2.split('.') # if not grupo1[1].isdigit(): # raise forms.ValidationError(self.error_messages['invalid']) # if not grupo2[1].isdigit(): # raise forms.ValidationError(self.error_messages['invalid']) # if not grupo3[1].isdigit(): # raise forms.ValidationError(self.error_messages['invalid']) if not value.isdigit(): value = re.sub("[-\./]", "", value) try: int(value) except ValueError: raise forms.ValidationError(self.error_messages['digits_only']) if len(value) != 14: raise forms.ValidationError(self.error_messages['max_digits']) orig_dv = value[-2:] new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(5, 1, -1) + range(9, 1, -1))]) new_1dv = DV_maker(new_1dv % 11) value = value[:-2] + str(new_1dv) + value[-1] new_2dv = sum([i * int(value[idx]) for idx, i in enumerate(range(6, 1, -1) + range(9, 1, -1))]) new_2dv = DV_maker(new_2dv % 11) value = value[:-1] + str(new_2dv) if value[-2:] != orig_dv: raise forms.ValidationError(self.error_messages['invalid']) return orig_value
def render(self, name, value, attrs=None): if value and value.isdigit() and len(value) == 14: value = mask_cnpj(value) return super(self.__class__, self).render(name, value, attrs=attrs)