From cca5cd23a412180cae90e3c0689fbee17e8fef0c Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 6 Oct 2016 18:56:25 -0300 Subject: [PATCH 1/3] Arruma cadastro de legislatura e de legislura edit Signed-off-by: Luciano Almeida --- sapl/parlamentares/forms.py | 9 ++++++++- sapl/parlamentares/views.py | 17 ++++++++++++++--- sapl/templates/parlamentares/layouts.yaml | 3 ++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index b09c054ec..827ed4f6b 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -24,7 +24,6 @@ class LegislaturaForm(ModelForm): def clean(self): cleaned_data = self.cleaned_data - numero = cleaned_data['numero'] data_inicio = cleaned_data['data_inicio'] data_fim = cleaned_data['data_fim'] data_eleicao = cleaned_data['data_eleicao'] @@ -33,6 +32,14 @@ class LegislaturaForm(ModelForm): raise ValidationError(_('A data início deve ser menor que a ' + 'data fim, e a data eleição deve ser ' + 'menor que a data início')) + return cleaned_data + + +class LegislaturaCreateForm(LegislaturaForm): + + def clean(self): + cleaned_data = super(LegislaturaCreateForm, self).clean() + numero = cleaned_data['numero'] if Legislatura.objects.filter(numero=numero).exists(): raise ValidationError( _('Já cadastrada uma legislatura com este número')) diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 55733dc22..2f243395a 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -11,8 +11,8 @@ from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, from sapl.materia.models import Proposicao, Relatoria from sapl.parlamentares.apps import AppConfig -from .forms import (FiliacaoForm, LegislaturaForm, ParlamentarCreateForm, - ParlamentarForm) +from .forms import (FiliacaoForm, LegislaturaForm, LegislaturaCreateForm, + ParlamentarCreateForm, ParlamentarForm) from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, Dependente, Filiacao, Frente, Legislatura, Mandato, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, @@ -149,7 +149,18 @@ class LegislaturaCrud(CrudAux): model = Legislatura help_path = 'tabelas_auxiliares#legislatura' - class BaseMixin(CrudAux.BaseMixin): + class CreateView(CrudAux.CreateView): + form_class = LegislaturaCreateForm + + def get_initial(self): + try: + ultima_legislatura = Legislatura.objects.latest('numero') + numero = ultima_legislatura.numero + 1 + except Legislatura.DoesNotExist: + numero = 1 + return {'numero': numero} + + class UpdateView(CrudAux.UpdateView): form_class = LegislaturaForm diff --git a/sapl/templates/parlamentares/layouts.yaml b/sapl/templates/parlamentares/layouts.yaml index 9948256bd..8b7306922 100644 --- a/sapl/templates/parlamentares/layouts.yaml +++ b/sapl/templates/parlamentares/layouts.yaml @@ -5,7 +5,8 @@ CargoMesa: Legislatura: {% trans 'Legislatura' %}: - - numero:2 data_inicio data_fim data_eleicao + - numero:12 + - data_eleicao data_inicio data_fim Coligacao: {% trans 'Coligação' %}: From 6b8c77d46de6e6751636afa8c244a780b6ca202a Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 13 Oct 2016 12:53:24 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Numera=C3=A7=C3=A3o=20da=20Legislatura=20de?= =?UTF-8?q?=20acordo=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 From a216540c84186f64f6327350d215851ba7c5c885 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 13 Oct 2016 16:14:04 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Arruma=20edi=C3=A7=C3=A3o=20de=20legislatur?= =?UTF-8?q?a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- sapl/parlamentares/forms.py | 63 ++++++++++++++++------- sapl/parlamentares/views.py | 17 ++---- sapl/templates/parlamentares/layouts.yaml | 3 +- 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index 806e635eb..975ed7621 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -16,42 +16,65 @@ class ImageThumbnailFileInput(ClearableFileInput): template_name = 'floppyforms/image_thumbnail.html' +def validar_datas_legislatura(eleicao, inicio, fim, pk=None): + + # Verifica se data de eleição < inicio < fim + if inicio >= fim or eleicao >= inicio: + msg_error = _('A data início deve ser menor que a ' + + 'data fim, e a data eleição deve ser ' + + 'menor que a data início') + return [False, msg_error] + + # Verifica se há alguma data cadastrada no intervalo de tempo desejado + if Legislatura.objects.filter( + data_inicio__range=[inicio, fim]).exclude(pk=pk).exists()\ + or Legislatura.objects.filter( + data_fim__range=[inicio, fim]).exclude(pk=pk).exists(): + msg_error = _('Já existe uma legislatura neste intervalo de datas') + return [False, msg_error] + + # Verifica se há alguma outra data de eleição cadastrada + if Legislatura.objects.filter( + data_eleicao=eleicao).exclude(pk=pk).exists(): + msg_error = _('Esta data de eleição já foi cadastrada') + return [False, msg_error] + + return [True, ''] + + class LegislaturaForm(ModelForm): class Meta: model = Legislatura exclude = [] - widgets = {'numero': forms.HiddenInput(), } + + +class LegislaturaCreateForm(LegislaturaForm): def clean(self): cleaned_data = self.cleaned_data - data_inicio = cleaned_data['data_inicio'] - data_fim = cleaned_data['data_fim'] - data_eleicao = cleaned_data['data_eleicao'] - - if data_inicio >= data_fim or data_eleicao >= data_inicio: - raise ValidationError(_('A data início deve ser menor que a ' + - 'data fim, e a data eleição deve ser ' + - 'menor que a data início')) + eleicao = cleaned_data['data_eleicao'] + inicio = cleaned_data['data_inicio'] + fim = cleaned_data['data_fim'] + + valida_datas = validar_datas_legislatura(eleicao, inicio, fim) + if not valida_datas[0]: + raise ValidationError(valida_datas[1]) return cleaned_data -class LegislaturaCreateForm(LegislaturaForm): +class LegislaturaUpdateForm(LegislaturaCreateForm): def clean(self): cleaned_data = super(LegislaturaCreateForm, self).clean() + eleicao = cleaned_data['data_eleicao'] 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á existe uma legislatura neste intervalo de datas')) - if Legislatura.objects.filter(data_eleicao=eleicao): - raise ValidationError( - _('Esta data de eleição já foi cadastrada')) + + valida_datas = validar_datas_legislatura( + eleicao, inicio, fim, pk=self.instance.pk) + if not valida_datas[0]: + raise ValidationError(valida_datas[1]) return cleaned_data diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 3254d8c90..6ee5a1e81 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -3,7 +3,6 @@ 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, @@ -12,8 +11,9 @@ from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, from sapl.materia.models import Proposicao, Relatoria from sapl.parlamentares.apps import AppConfig -from .forms import (FiliacaoForm, LegislaturaForm, LegislaturaCreateForm, - ParlamentarCreateForm, ParlamentarForm) +from .forms import (FiliacaoForm, LegislaturaCreateForm, + LegislaturaUpdateForm, ParlamentarCreateForm, + ParlamentarForm) from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, Dependente, Filiacao, Frente, Legislatura, Mandato, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, @@ -161,17 +161,8 @@ 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 + form_class = LegislaturaUpdateForm class FiliacaoCrud(MasterDetailCrud): diff --git a/sapl/templates/parlamentares/layouts.yaml b/sapl/templates/parlamentares/layouts.yaml index 8b7306922..98119cf06 100644 --- a/sapl/templates/parlamentares/layouts.yaml +++ b/sapl/templates/parlamentares/layouts.yaml @@ -5,8 +5,7 @@ CargoMesa: Legislatura: {% trans 'Legislatura' %}: - - numero:12 - - data_eleicao data_inicio data_fim + - numero data_eleicao data_inicio data_fim Coligacao: {% trans 'Coligação' %}: