From 155cd5a3e41c0b92898914ccbf323a487eabdbd8 Mon Sep 17 00:00:00 2001 From: Edward Date: Sun, 7 Apr 2019 10:06:51 -0300 Subject: [PATCH] fixes #2689 (#2690) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fixes #2689 * Trata inexistencia de chaves * Adiciona verificação de data --- sapl/materia/forms.py | 15 ++++++++++++++- sapl/materia/views.py | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index 673dfd024..d6935b697 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -402,7 +402,16 @@ class RelatoriaForm(ModelForm): self.fields['composicao'].choices = [('', '---------')] + \ [(c.pk, c) for c in composicoes] - self.fields['parlamentar'].choices = [('', '---------')] + # UPDATE + if self.initial.get('composicao') and self.initial.get('parlamentar'): + parlamentares = [(p.parlamentar.id, p.parlamentar) for p in + Participacao.objects.filter(composicao__comissao_id=comissao_pk, + composicao_id=self.initial['composicao'])] + + self.fields['parlamentar'].choices = [('', '---------')] + parlamentares + # INSERT + else: + self.fields['parlamentar'].choices = [('', '---------')] def clean(self): super().clean() @@ -424,6 +433,10 @@ class RelatoriaForm(ModelForm): else: cleaned_data['comissao'] = comissao + if cleaned_data['data_designacao_relator'] < cleaned_data['composicao'].periodo.data_inicio \ + or cleaned_data['data_designacao_relator'] > cleaned_data['composicao'].periodo.data_fim: + raise ValidationError(_('Data de designação deve estar dentro do período da composição.')) + return cleaned_data diff --git a/sapl/materia/views.py b/sapl/materia/views.py index e30b756fb..6c0da0ce0 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -33,7 +33,7 @@ import sapl from sapl.base.email_utils import do_envia_email_confirmacao from sapl.base.models import Autor, CasaLegislativa, AppConfig as BaseAppConfig from sapl.base.signals import tramitacao_signal -from sapl.comissoes.models import Comissao, Participacao +from sapl.comissoes.models import Comissao, Participacao, Composicao from sapl.compilacao.models import (STATUS_TA_IMMUTABLE_RESTRICT, STATUS_TA_PRIVATE) from sapl.compilacao.views import IntegracaoTaView @@ -1135,6 +1135,26 @@ class RelatoriaCrud(MasterDetailCrud): layout_key = None logger = logging.getLogger(__name__) + def get_initial(self): + relatoria = Relatoria.objects.get(id=self.kwargs['pk']) + parlamentar = relatoria.parlamentar + comissao = relatoria.comissao + composicoes = [p.composicao for p in + Participacao.objects.filter( + parlamentar=parlamentar, + composicao__comissao=comissao)] + data_designacao = relatoria.data_designacao_relator + composicao = '' + for c in composicoes: + data_inicial = c.periodo.data_inicio + data_fim = c.periodo.data_fim if c.periodo.data_fim else timezone.now().date() + if data_inicial <= data_designacao <= data_fim: + composicao = c.id + break + return {'comissao': relatoria.comissao.id, + 'parlamentar': relatoria.parlamentar.id, + 'composicao': composicao} + class TramitacaoCrud(MasterDetailCrud): model = Tramitacao