Browse Source

Numeração da Legislatura de acordo com a data de início

Signed-off-by: Luciano Almeida <lucianoalmeida@interlegis.leg.br>
pull/735/head
Luciano Almeida 8 years ago
parent
commit
6b8c77d46d
  1. 16
      sapl/parlamentares/forms.py
  2. 10
      sapl/parlamentares/views.py

16
sapl/parlamentares/forms.py

@ -21,6 +21,7 @@ class LegislaturaForm(ModelForm):
class Meta: class Meta:
model = Legislatura model = Legislatura
exclude = [] exclude = []
widgets = {'numero': forms.HiddenInput(), }
def clean(self): def clean(self):
cleaned_data = self.cleaned_data cleaned_data = self.cleaned_data
@ -39,11 +40,18 @@ class LegislaturaCreateForm(LegislaturaForm):
def clean(self): def clean(self):
cleaned_data = super(LegislaturaCreateForm, self).clean() cleaned_data = super(LegislaturaCreateForm, self).clean()
numero = cleaned_data['numero'] inicio = cleaned_data['data_inicio']
if Legislatura.objects.filter(numero=numero).exists(): 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( 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 return cleaned_data

10
sapl/parlamentares/views.py

@ -3,6 +3,7 @@ from django.contrib import messages
from django.core.urlresolvers import reverse, reverse_lazy from django.core.urlresolvers import reverse, reverse_lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView from django.views.generic import FormView
from django.views.generic.edit import ModelFormMixin
from sapl.comissoes.models import Participacao from sapl.comissoes.models import Participacao
from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux,
@ -160,6 +161,15 @@ class LegislaturaCrud(CrudAux):
numero = 1 numero = 1
return {'numero': numero} 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): class UpdateView(CrudAux.UpdateView):
form_class = LegislaturaForm form_class = LegislaturaForm

Loading…
Cancel
Save