From 6b8c77d46de6e6751636afa8c244a780b6ca202a Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 13 Oct 2016 12:53:24 -0300 Subject: [PATCH] =?UTF-8?q?Numera=C3=A7=C3=A3o=20da=20Legislatura=20de=20a?= =?UTF-8?q?cordo=20com=20a=20data=20de=20in=C3=ADcio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- sapl/parlamentares/forms.py | 16 ++++++++++++---- sapl/parlamentares/views.py | 10 ++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index 827ed4f6b..806e635eb 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -21,6 +21,7 @@ class LegislaturaForm(ModelForm): class Meta: model = Legislatura exclude = [] + widgets = {'numero': forms.HiddenInput(), } def clean(self): cleaned_data = self.cleaned_data @@ -39,11 +40,18 @@ class LegislaturaCreateForm(LegislaturaForm): def clean(self): cleaned_data = super(LegislaturaCreateForm, self).clean() - numero = cleaned_data['numero'] - if Legislatura.objects.filter(numero=numero).exists(): + inicio = cleaned_data['data_inicio'] + fim = cleaned_data['data_fim'] + eleicao = cleaned_data['data_eleicao'] + if Legislatura.objects.filter( + data_inicio__range=[inicio, fim]).exists() or \ + Legislatura.objects.filter( + data_fim__range=[inicio, fim]).exists(): raise ValidationError( - _('Já cadastrada uma legislatura com este número')) - + _('Já existe uma legislatura neste intervalo de datas')) + if Legislatura.objects.filter(data_eleicao=eleicao): + raise ValidationError( + _('Esta data de eleição já foi cadastrada')) return cleaned_data diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 2f243395a..3254d8c90 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -3,6 +3,7 @@ from django.contrib import messages from django.core.urlresolvers import reverse, reverse_lazy from django.utils.translation import ugettext_lazy as _ from django.views.generic import FormView +from django.views.generic.edit import ModelFormMixin from sapl.comissoes.models import Participacao from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, @@ -160,6 +161,15 @@ class LegislaturaCrud(CrudAux): numero = 1 return {'numero': numero} + def form_valid(self, form): + self.object = form.save() + queryset = Legislatura.objects.all().order_by('data_inicio') + for i, obj in enumerate(queryset): + obj.numero = i + 1 + obj.save() + + return super(ModelFormMixin, self).form_valid(form) + class UpdateView(CrudAux.UpdateView): form_class = LegislaturaForm