From d0b1d87c4e8cf8166dcdaa40e00f56d18737cdd7 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Wed, 15 Mar 2017 14:37:39 -0300 Subject: [PATCH] Fix #920 --- sapl/parlamentares/forms.py | 24 +++++++++++++++++++++++- sapl/parlamentares/views.py | 10 +++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index bdcf2d232..860e53191 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -5,7 +5,7 @@ from crispy_forms.layout import Fieldset, Layout from django import forms from django.contrib.auth import get_user_model from django.contrib.auth.models import Group, User -from django.core.exceptions import ValidationError +from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.db import transaction from django.db.models import Q from django.forms import ModelForm @@ -49,6 +49,28 @@ def validar_datas_legislatura(eleicao, inicio, fim, pk=None): return [True, ''] +class MandatoForm(ModelForm): + + class Meta: + model = Mandato + fields = ['legislatura', 'coligacao', 'votos_recebidos', + 'data_fim_mandato', 'data_expedicao_diploma', + 'tipo_afastamento', 'observacao', 'parlamentar'] + widgets = {'parlamentar': forms.HiddenInput()} + + def clean(self): + data = self.cleaned_data + try: + mandato = Mandato.objects.get( + parlamentar__pk=self.initial['parlamentar'].pk, + legislatura__pk=data['legislatura'].pk) + except ObjectDoesNotExist: + pass + else: + raise ValidationError('Mandato nesta legislatura já existe.') + return data + + class LegislaturaForm(ModelForm): class Meta: diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 1ee5299b8..cc45ef7ce 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -14,7 +14,8 @@ from sapl.materia.models import Proposicao, Relatoria from sapl.parlamentares.apps import AppConfig from .forms import (FiliacaoForm, LegislaturaCreateForm, LegislaturaUpdateForm, - ParlamentarCreateForm, ParlamentarForm, VotanteForm) + MandatoForm, ParlamentarCreateForm, ParlamentarForm, + VotanteForm) from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, Dependente, Filiacao, Frente, Legislatura, Mandato, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, @@ -195,6 +196,13 @@ class MandatoCrud(MasterDetailCrud): class ListView(MasterDetailCrud.ListView): ordering = ('-legislatura__numero') + class CreateView(MasterDetailCrud.CreateView): + form_class = MandatoForm + + def get_initial(self): + return {'parlamentar': Parlamentar.objects.get( + pk=self.kwargs['pk'])} + class ComposicaoColigacaoCrud(MasterDetailCrud): model = ComposicaoColigacao