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.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):

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:
verbose_name = _('Autoria')
verbose_name_plural = _('Autorias')
unique_together = (('autor', 'materia'), )
def __str__(self):
return _('%(autor)s - %(materia)s') % {

5
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

Loading…
Cancel
Save