Exemplo n.º 1
0
    def clean_certificates(self):
        # TODO: Refactor
        data = self.cleaned_data['certificates']

        template = self.cleaned_data['template']

        certificates = json.loads(data)

        any_object = False

        for line, attrs_certificate in enumerate(certificates, 1):
            if any(attrs_certificate):
                any_object = True
                # ('' in attrs_certificate[1:] = Remove ENDERECO_EMAIL (optional)
                if (None in attrs_certificate[1:]
                    ) or ('' in attrs_certificate[1:]) or (len(
                        template.template_fields()) != len(attrs_certificate)):
                    raise forms.ValidationError(
                        'A tabela não pode conter valores em branco')
                    break
                else:
                    try:
                        attrs_certificate[1] = validate_cpf(
                            attrs_certificate[1])
                    except Exception as e:
                        raise forms.ValidationError(
                            'O CPF {} da linha {} é inválido.'.format(
                                attrs_certificate[1], line))
                        break

        if any_object is False:
            raise forms.ValidationError(
                'A tabela não pode conter valores em branco')

        return data
Exemplo n.º 2
0
    def test_cpf_is_valid(self):
        for cpf in self.valid_inputs:
            with self.subTest():
                # Remove '-' and '.'
                cpf = re.sub('[-\.]', '', cpf)
                self.assertEqual(cpf, validate_cpf(cpf))

        for cpf in self.invalid_numbers_inputs:
            with self.subTest():
                # Remove '-' and '.'
                cpf = re.sub('[-\.]', '', cpf)
                with self.assertRaisesMessage(ValidationError, 'Invalid CPF number'):
                    validate_cpf(cpf)

        for cpf in self.invalid_inputs:
            with self.subTest():
                # Remove '-' and '.'
                cpf = re.sub('[-\.]', '', cpf)
                with self.assertRaisesMessage(ValidationError, 'This field requires only numbers'):
                    validate_cpf(cpf)
Exemplo n.º 3
0
def certificates_creator(request):
    context = {}

    if request.method == 'POST':
        form = CertificatesCreatorForm(request.user, request.POST,
                                       request.FILES)
        context['certificates'] = request.POST['certificates']

        if form.is_valid():
            template = form.cleaned_data['template']
            certificates = form.cleaned_data['certificates']

            certificates = json.loads(certificates)

            inspector = {'certificates': [], 'error': []}

            for certificate_attrs in certificates:
                email, cpf, name, *args = certificate_attrs
                attrs = {}

                for key, value in enumerate(args, 3):
                    attrs[template.template_fields()[key]] = value

                participant, created = Participant.objects.get_or_create(
                    cpf=validate_cpf(cpf), defaults={'name': name})

                if email:
                    try:
                        validate_email(email)
                        participant.email = email
                        participant.save()
                    except:
                        pass

                try:
                    certificate = Certificate.objects.create(
                        participant=participant,
                        template=template,
                        fields=attrs)
                    inspector['certificates'].append(certificate)
                except IntegrityError:
                    inspector['error'].append(certificate_attrs)

                return render(request, 'certificates/template/inspector.html',
                              {'inspector': inspector})

    else:
        form = CertificatesCreatorForm(request.user)

    context['form'] = form

    return render(request, 'certificates/certificate/generator.html', context)
Exemplo n.º 4
0
 def clean_cpf(self):
     cpf = self.cleaned_data['cpf']
     cpf = validate_cpf(cpf)
     return cpf