From c7cd140eb2163a7e9089167933262f1f6441cc04 Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Tue, 8 Aug 2017 09:37:06 -0300 Subject: [PATCH] =?UTF-8?q?Corrige=20valida=C3=A7=C3=A3o=20de=20unicidade?= =?UTF-8?q?=20em=20AutoriaForm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0007_auto_20170808_0850.py | 19 +++++++++++++++ sapl/materia/forms.py | 24 +++++++++---------- .../migrations/0010_auto_20170808_0850.py | 19 +++++++++++++++ sapl/materia/models.py | 1 + sapl/materia/views.py | 5 +--- 5 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 sapl/base/migrations/0007_auto_20170808_0850.py create mode 100644 sapl/materia/migrations/0010_auto_20170808_0850.py diff --git a/sapl/base/migrations/0007_auto_20170808_0850.py b/sapl/base/migrations/0007_auto_20170808_0850.py new file mode 100644 index 000000000..8d3b4777a --- /dev/null +++ b/sapl/base/migrations/0007_auto_20170808_0850.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.13 on 2017-08-08 08:50 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0006_auto_20170802_1908'), + ] + + operations = [ + migrations.AlterModelOptions( + name='autor', + options={'ordering': ('nome',), 'verbose_name': 'Autor', 'verbose_name_plural': 'Autores'}, + ), + ] diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index a125c6f84..11e2d62b7 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -27,6 +27,7 @@ import django_filters from sapl.base.models import Autor, TipoAutor from sapl.comissoes.models import Comissao +from sapl.compilacao.forms import error_messages from sapl.compilacao.models import (STATUS_TA_IMMUTABLE_PUBLIC, STATUS_TA_PRIVATE) from sapl.crispy_layout_mixin import (SaplFormLayout, form_actions, to_column, @@ -692,9 +693,8 @@ class AutoriaForm(ModelForm): tipo_autor = ModelChoiceField(label=_('Tipo Autor'), required=False, - queryset=TipoAutor.objects.all().order_by( - 'descricao'), - empty_label='Selecione',) + queryset=TipoAutor.objects.all(), + empty_label=_('Selecione'),) data_relativa = forms.DateField( widget=forms.HiddenInput()) @@ -719,19 +719,17 @@ class AutoriaForm(ModelForm): fields = ['tipo_autor', 'autor', 'primeiro_autor', 'data_relativa'] def clean(self): - super(AutoriaForm, self).clean() + cd = super(AutoriaForm, self).clean() - if self.errors: - return self.errors + autorias = Autoria.objects.filter( + materia=self.instance.materia, autor=cd['autor']) + pk = self.instance.pk - if Autoria.objects.filter( - materia=self.instance.materia, - autor=self.cleaned_data['autor'], - ).exists(): - msg = _('Esse Autor já foi cadastrado.') - raise ValidationError(msg) + if ((not pk and autorias.exists()) + or (pk and autorias.exclude(pk=pk).exists())): + raise ValidationError(_('Esse Autor já foi cadastrado.')) - return self.cleaned_data + return cd class AcessorioEmLoteFilterSet(django_filters.FilterSet): diff --git a/sapl/materia/migrations/0010_auto_20170808_0850.py b/sapl/materia/migrations/0010_auto_20170808_0850.py new file mode 100644 index 000000000..b32c05b23 --- /dev/null +++ b/sapl/materia/migrations/0010_auto_20170808_0850.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.13 on 2017-08-08 08:50 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('materia', '0009_auto_20170712_0951'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='autoria', + unique_together=set([('autor', 'materia')]), + ), + ] diff --git a/sapl/materia/models.py b/sapl/materia/models.py index 8d9eae4f5..1bb308c6d 100644 --- a/sapl/materia/models.py +++ b/sapl/materia/models.py @@ -299,6 +299,7 @@ class Autoria(models.Model): class Meta: verbose_name = _('Autoria') verbose_name_plural = _('Autorias') + unique_together = (('autor', 'materia'), ) def __str__(self): return _('%(autor)s - %(materia)s') % { diff --git a/sapl/materia/views.py b/sapl/materia/views.py index b587d027b..eeb465cd1 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -1110,12 +1110,9 @@ class AutoriaCrud(MasterDetailCrud): def get_initial(self): initial = super().get_initial() - autoria = Autoria.objects.get(id=self.kwargs['pk']) initial.update({ - 'data_relativa': autoria.materia.data_apresentacao, + 'data_relativa': self.object.materia.data_apresentacao, 'tipo_autor': self.object.autor.tipo.id, - 'autor': self.object.autor.id, - 'primeiro_autor': self.object.primeiro_autor }) return initial