Browse Source

Corrige validação de unicidade em AutoriaForm

pull/1361/head
LeandroRoberto 7 years ago
parent
commit
c7cd140eb2
  1. 19
      sapl/base/migrations/0007_auto_20170808_0850.py
  2. 24
      sapl/materia/forms.py
  3. 19
      sapl/materia/migrations/0010_auto_20170808_0850.py
  4. 1
      sapl/materia/models.py
  5. 5
      sapl/materia/views.py

19
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'},
),
]

24
sapl/materia/forms.py

@ -27,6 +27,7 @@ import django_filters
from sapl.base.models import Autor, TipoAutor from sapl.base.models import Autor, TipoAutor
from sapl.comissoes.models import Comissao from sapl.comissoes.models import Comissao
from sapl.compilacao.forms import error_messages
from sapl.compilacao.models import (STATUS_TA_IMMUTABLE_PUBLIC, from sapl.compilacao.models import (STATUS_TA_IMMUTABLE_PUBLIC,
STATUS_TA_PRIVATE) STATUS_TA_PRIVATE)
from sapl.crispy_layout_mixin import (SaplFormLayout, form_actions, to_column, from sapl.crispy_layout_mixin import (SaplFormLayout, form_actions, to_column,
@ -692,9 +693,8 @@ class AutoriaForm(ModelForm):
tipo_autor = ModelChoiceField(label=_('Tipo Autor'), tipo_autor = ModelChoiceField(label=_('Tipo Autor'),
required=False, required=False,
queryset=TipoAutor.objects.all().order_by( queryset=TipoAutor.objects.all(),
'descricao'), empty_label=_('Selecione'),)
empty_label='Selecione',)
data_relativa = forms.DateField( data_relativa = forms.DateField(
widget=forms.HiddenInput()) widget=forms.HiddenInput())
@ -719,19 +719,17 @@ class AutoriaForm(ModelForm):
fields = ['tipo_autor', 'autor', 'primeiro_autor', 'data_relativa'] fields = ['tipo_autor', 'autor', 'primeiro_autor', 'data_relativa']
def clean(self): def clean(self):
super(AutoriaForm, self).clean() cd = super(AutoriaForm, self).clean()
if self.errors: autorias = Autoria.objects.filter(
return self.errors materia=self.instance.materia, autor=cd['autor'])
pk = self.instance.pk
if Autoria.objects.filter( if ((not pk and autorias.exists())
materia=self.instance.materia, or (pk and autorias.exclude(pk=pk).exists())):
autor=self.cleaned_data['autor'], raise ValidationError(_('Esse Autor já foi cadastrado.'))
).exists():
msg = _('Esse Autor já foi cadastrado.')
raise ValidationError(msg)
return self.cleaned_data return cd
class AcessorioEmLoteFilterSet(django_filters.FilterSet): class AcessorioEmLoteFilterSet(django_filters.FilterSet):

19
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')]),
),
]

1
sapl/materia/models.py

@ -299,6 +299,7 @@ class Autoria(models.Model):
class Meta: class Meta:
verbose_name = _('Autoria') verbose_name = _('Autoria')
verbose_name_plural = _('Autorias') verbose_name_plural = _('Autorias')
unique_together = (('autor', 'materia'), )
def __str__(self): def __str__(self):
return _('%(autor)s - %(materia)s') % { return _('%(autor)s - %(materia)s') % {

5
sapl/materia/views.py

@ -1110,12 +1110,9 @@ class AutoriaCrud(MasterDetailCrud):
def get_initial(self): def get_initial(self):
initial = super().get_initial() initial = super().get_initial()
autoria = Autoria.objects.get(id=self.kwargs['pk'])
initial.update({ initial.update({
'data_relativa': autoria.materia.data_apresentacao, 'data_relativa': self.object.materia.data_apresentacao,
'tipo_autor': self.object.autor.tipo.id, 'tipo_autor': self.object.autor.tipo.id,
'autor': self.object.autor.id,
'primeiro_autor': self.object.primeiro_autor
}) })
return initial return initial

Loading…
Cancel
Save