From 82471d01e361673d3c9a34a4d5fa775443875830 Mon Sep 17 00:00:00 2001 From: Mariana Mendes Date: Fri, 23 Mar 2018 18:05:13 -0300 Subject: [PATCH] Fix #1776 (#1788) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix #1776 Co-authored-by: Mariana Mendes * Fix #1776 Co-authored-by: João Pedro Sconetto --- sapl/comissoes/forms.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sapl/comissoes/forms.py b/sapl/comissoes/forms.py index dd2c037da..abcce24f7 100644 --- a/sapl/comissoes/forms.py +++ b/sapl/comissoes/forms.py @@ -18,6 +18,7 @@ class ParticipacaoCreateForm(forms.ModelForm): class Meta: model = Participacao + fields = '__all__' exclude = ['composicao'] def __init__(self, user=None, **kwargs): @@ -51,6 +52,21 @@ class ParticipacaoCreateForm(forms.ModelForm): qs = Parlamentar.objects.filter(id__in=ids) self.fields['parlamentar'].queryset = qs + + def clean(self): + cleaned_data = super(ParticipacaoCreateForm, self).clean() + + if not self.is_valid(): + return cleaned_data + + composicao = Composicao.objects.get(id=self.initial['parent_pk']) + cargos_unicos = [c.cargo.nome for c in composicao.participacao_set.filter(cargo__unico=True)] + + if cleaned_data['cargo'].nome in cargos_unicos: + msg = _('Este cargo é único para esta Comissão.') + raise ValidationError(msg) + + def create_participacao(self): composicao = Composicao.objects.get(id=self.initial['parent_pk']) data_inicio_comissao = composicao.periodo.data_inicio