diff --git a/comissoes/urls.py b/comissoes/urls.py index 138ae0230..bad1cf773 100644 --- a/comissoes/urls.py +++ b/comissoes/urls.py @@ -8,7 +8,7 @@ from comissoes.views import (ComposicaoView, MateriasView, ReunioesView, comissao_url_patterns = comissao_crud.urlpatterns + [ url(r'^(?P\d+)/composicao$', ComposicaoView.as_view(), name='composicao'), - url(r'^(?P\d+)/composicao/parlamentar', + url(r'^(?P\d+)/composicao/(?P\d+)/parlamentar$', ComissaoParlamentarIncluirView.as_view(), name='comissao_parlamentar'), url(r'^(?P\d+)/materias$', MateriasView.as_view(), name='materias'), diff --git a/comissoes/views.py b/comissoes/views.py index 0829ee7ea..20232a01f 100644 --- a/comissoes/views.py +++ b/comissoes/views.py @@ -1,19 +1,19 @@ +from crispy_forms.helper import FormHelper +from crispy_forms.layout import ButtonHolder, Field, Fieldset, Layout, Submit from django import forms +from django.core.urlresolvers import reverse +from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ from django.views.generic import ListView from django.views.generic.edit import FormMixin +from parlamentares.models import Filiacao, Parlamentar from sapl.crud import build_crud +from sessao.models import Parlamentar from vanilla import GenericView -from crispy_forms.helper import FormHelper -from crispy_forms.layout import ButtonHolder, Field, Fieldset, Layout, Submit -from django.forms import ModelForm - from .models import (CargoComissao, Comissao, Composicao, Participacao, Periodo, TipoComissao) -from parlamentares.models import Parlamentar, Filiacao - cargo_crud = build_crud( CargoComissao, 'cargo_comissao', [ @@ -108,41 +108,48 @@ class MateriasView(comissao_crud.CrudDetailView): class ReunioesView(comissao_crud.CrudDetailView): template_name = 'comissoes/reunioes.html' + class ParticipacaoCadastroForm(ModelForm): YES_OR_NO = ( - (True, 'Sim'), - (False, 'Não') + (True, 'Sim'), + (False, 'Não') ) - PARLAMENTARES_CHOICES = [('', '---------')] + [(p.parlamentar.id, p.parlamentar.nome_parlamentar + ' / ' + p.partido.sigla) for p in Filiacao.objects.filter(data_desfiliacao__isnull=True, parlamentar__ativo=True).order_by('parlamentar__nome_parlamentar')] + PARLAMENTARES_CHOICES = [('', '---------')] + [ + (p.parlamentar.id, + p.parlamentar.nome_parlamentar + ' / ' + p.partido.sigla) + for p in Filiacao.objects.filter( + data_desfiliacao__isnull=True, parlamentar__ativo=True).order_by( + 'parlamentar__nome_parlamentar')] + parlamentar_id = forms.ChoiceField(required=True, + label='Parlamentar', + choices=PARLAMENTARES_CHOICES, + widget=forms.Select( + attrs={'class': 'selector'})) - parlamentar = forms.ChoiceField(required=True, - label='Parlamentar', - choices=PARLAMENTARES_CHOICES, - widget=forms.Select( - attrs={'class': 'selector'})) - - titular = forms.BooleanField(widget=forms.RadioSelect(choices=YES_OR_NO), required=True) + titular = forms.BooleanField( + widget=forms.RadioSelect(choices=YES_OR_NO), required=True) data_designacao = forms.DateField(label=u'Data Designação', - input_formats=['%d/%m/%Y'], - required=True, - widget=forms.DateInput(format='%d/%m/%Y')) + input_formats=['%d/%m/%Y'], + required=True, + widget=forms.DateInput( + format='%d/%m/%Y')) data_desligamento = forms.DateField(label=u'Data Desligamento', - input_formats=['%d/%m/%Y'], - required=False, - widget=forms.DateInput(format='%d/%m/%Y')) + input_formats=['%d/%m/%Y'], + required=False, + widget=forms.DateInput( + format='%d/%m/%Y')) class Meta: model = Participacao - fields = ['parlamentar', - 'composicao', + fields = ['parlamentar_id', 'cargo', 'titular', - 'data_designacao', + 'data_designacao', 'data_desligamento', 'motivo_desligamento', 'observacao'] @@ -150,29 +157,70 @@ class ParticipacaoCadastroForm(ModelForm): def __init__(self, *args, **kwargs): self.helper = FormHelper() self.helper.layout = Layout( - Fieldset( - 'Formulário de Cadastro', - 'parlamentar', - 'composicao', # TODO colocar como hidden (remover daqui) - 'cargo', - 'titular', - 'data_designacao', - 'data_desligamento', - 'motivo_desligamento', - 'observacao' - ), - # Field('documento', type="hidden"), - ButtonHolder( - Submit('submit', 'Salvar', - css_class='button primary') - ) + Fieldset( + 'Formulário de Cadastro', + 'parlamentar_id', + 'cargo', + 'titular', + 'data_designacao', + 'data_desligamento', + 'motivo_desligamento', + 'observacao' + ), + ButtonHolder( + Submit('submit', 'Salvar', + css_class='button primary') + ) ) super(ParticipacaoCadastroForm, self).__init__(*args, **kwargs) + class ComissaoParlamentarIncluirView(FormMixin, GenericView): template_name = "comissoes/comissao_parlamentar.html" def get(self, request, *args, **kwargs): - pk = kwargs['pk'] - form = ParticipacaoCadastroForm() - return self.render_to_response({'form': form}) \ No newline at end of file + form = ParticipacaoCadastroForm() + return self.render_to_response({'form': form, + 'composicao_id': self.kwargs['id']}) + + def post(self, request, *args, **kwargs): + composicao = Composicao.objects.get(id=self.kwargs['id']) + form = ParticipacaoCadastroForm(request.POST) + + if form.is_valid(): + cargo = form.cleaned_data['cargo'] + if cargo.nome == 'Presidente': + for p in Participacao.objects.filter(composicao=composicao): + if p.cargo.nome == 'Presidente': + return self.render_to_response( + {'form': form, + 'composicao_id': self.kwargs['id'], + 'error': 'Esse cargo já está sendo ocupado!'}) + else: + # Pensar em forma melhor para não duplicar código + participacao = form.save(commit=False) + parlamentar = Parlamentar.objects.get( + id=int(form.cleaned_data['parlamentar_id'])) + + participacao.composicao = composicao + participacao.parlamentar = parlamentar + + participacao.save() + else: + participacao = form.save(commit=False) + parlamentar = Parlamentar.objects.get( + id=int(form.cleaned_data['parlamentar_id'])) + + participacao.composicao = composicao + participacao.parlamentar = parlamentar + + participacao.save() + return self.form_valid(form) + else: + return self.render_to_response( + {'form': form, + 'composicao_id': self.kwargs['id']}) + + def get_success_url(self): + pk = self.kwargs['pk'] + return reverse('comissao:composicao', kwargs={'pk': pk}) diff --git a/templates/comissoes/comissao_parlamentar.html b/templates/comissoes/comissao_parlamentar.html index 329345825..ba171eaec 100644 --- a/templates/comissoes/comissao_parlamentar.html +++ b/templates/comissoes/comissao_parlamentar.html @@ -3,7 +3,10 @@ {% load crispy_forms_tags %} {% block detail_content %} -

Incluir Parlamentar

+

Incluir Parlamentar

+ + {% if error %}
{{ error }}
{% endif %} + {% crispy form %} {% endblock detail_content %} diff --git a/templates/comissoes/composicao.html b/templates/comissoes/composicao.html index f04047dd1..d4e6618bd 100644 --- a/templates/comissoes/composicao.html +++ b/templates/comissoes/composicao.html @@ -42,6 +42,6 @@ {% endfor %} -Incluir Parlamentar +Incluir Parlamentar {% endblock detail_content %} \ No newline at end of file