From 6241655a6a46ad2eb56c44573f26e0b2abd64983 Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Fri, 16 Jun 2017 15:51:40 -0300 Subject: [PATCH] =?UTF-8?q?Corrige=20inser=C3=A7=C3=A3o=20de=20duplicatas?= =?UTF-8?q?=20na=20implementa=C3=A7=C3=A3o=20de=20TipoProposicaoForm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Eliseu Egewarth --- sapl/materia/forms.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index aac86c019..bf61440c4 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -803,13 +803,28 @@ class TipoProposicaoForm(ModelForm): def clean(self): cd = self.cleaned_data - + super(TipoProposicaoForm, self).clean() content_type = cd['content_type'] - - if 'tipo_conteudo_related' not in cd or not cd[ - 'tipo_conteudo_related']: + try: + tipo_conteudo_related = cd['tipo_conteudo_related'] + if not tipo_conteudo_related: + raise ValidationError( + _('Seleção de Tipo não definida')) + except KeyError: raise ValidationError( _('Seleção de Tipo não definida')) + tipo = TipoProposicao.objects.filter( + content_type_id=content_type, + object_id=tipo_conteudo_related).first() + if tipo: + raise ValidationError( + _('O %(model)s: %(content_type)s, %(object_id)s já existe.'), + code='invalid', + params={ + 'model': self._meta.model._meta.verbose_name, + 'content_type': content_type.__str__(), + 'object_id': tipo.tipo_conteudo_related.__str__()}, + ) if not content_type.model_class().objects.filter( pk=cd['tipo_conteudo_related']).exists(): @@ -819,6 +834,17 @@ class TipoProposicaoForm(ModelForm): return self.cleaned_data + def clean_content_type(self): + try: + content_type = self.cleaned_data['content_type'] + if not content_type: + raise ValidationError( + _('Definição de Tipo não definida')) + except KeyError: + raise ValidationError( + _('Definição de Tipo não definida')) + return content_type + @transaction.atomic def save(self, commit=False):