예제 #1
0
    def clean(self, value):
        value = super(BrCpfField, self).clean(value)
        if value in EMPTY_VALUES:
            return u''
        if len(value) == 11: # formato "99999999999"
            value = mask_cpf(value)
        if len(value) != 14: # formato "999.999.999-99"
            raise forms.ValidationError(self.error_messages['invalid'])
        if not re.match('\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) != 11:
            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(10, 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(11, 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
예제 #2
0
    def clean(self, value):
        value = super(BrCpfField, self).clean(value)
        if value in EMPTY_VALUES:
            return u''
        if len(value) == 11: # formato "99999999999"
            value = mask_cpf(value)
        if len(value) != 14: # formato "999.999.999-99"
            raise forms.ValidationError(self.error_messages['invalid'])
        if not re.match('\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) != 11:
            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(10, 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(11, 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
예제 #3
0
 def render(self, name, value, attrs=None):
     if value and value.isdigit() and len(value) == 11:
         value = mask_cpf(value)
     return super(self.__class__, self).render(name, value, attrs=attrs)
예제 #4
0
 def render(self, name, value, attrs=None):
     if value and value.isdigit() and len(value) == 11:
         value = mask_cpf(value)
     return super(self.__class__, self).render(name, value, attrs=attrs)