Browse Source

Add Incluir Parlamentar in Composicao

pull/11/merge
Eduardo Edson Batista Cordeiro Alves 9 years ago
parent
commit
fb32c40e77
  1. 2
      comissoes/urls.py
  2. 136
      comissoes/views.py
  3. 5
      templates/comissoes/comissao_parlamentar.html
  4. 2
      templates/comissoes/composicao.html

2
comissoes/urls.py

@ -8,7 +8,7 @@ from comissoes.views import (ComposicaoView, MateriasView, ReunioesView,
comissao_url_patterns = comissao_crud.urlpatterns + [ comissao_url_patterns = comissao_crud.urlpatterns + [
url(r'^(?P<pk>\d+)/composicao$', url(r'^(?P<pk>\d+)/composicao$',
ComposicaoView.as_view(), name='composicao'), ComposicaoView.as_view(), name='composicao'),
url(r'^(?P<pk>\d+)/composicao/parlamentar', url(r'^(?P<pk>\d+)/composicao/(?P<id>\d+)/parlamentar$',
ComissaoParlamentarIncluirView.as_view(), name='comissao_parlamentar'), ComissaoParlamentarIncluirView.as_view(), name='comissao_parlamentar'),
url(r'^(?P<pk>\d+)/materias$', url(r'^(?P<pk>\d+)/materias$',
MateriasView.as_view(), name='materias'), MateriasView.as_view(), name='materias'),

136
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 import forms
from django.core.urlresolvers import reverse
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView from django.views.generic import ListView
from django.views.generic.edit import FormMixin from django.views.generic.edit import FormMixin
from parlamentares.models import Filiacao, Parlamentar
from sapl.crud import build_crud from sapl.crud import build_crud
from sessao.models import Parlamentar
from vanilla import GenericView 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, from .models import (CargoComissao, Comissao, Composicao, Participacao,
Periodo, TipoComissao) Periodo, TipoComissao)
from parlamentares.models import Parlamentar, Filiacao
cargo_crud = build_crud( cargo_crud = build_crud(
CargoComissao, 'cargo_comissao', [ CargoComissao, 'cargo_comissao', [
@ -108,41 +108,48 @@ class MateriasView(comissao_crud.CrudDetailView):
class ReunioesView(comissao_crud.CrudDetailView): class ReunioesView(comissao_crud.CrudDetailView):
template_name = 'comissoes/reunioes.html' template_name = 'comissoes/reunioes.html'
class ParticipacaoCadastroForm(ModelForm): class ParticipacaoCadastroForm(ModelForm):
YES_OR_NO = ( YES_OR_NO = (
(True, 'Sim'), (True, 'Sim'),
(False, 'Não') (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, titular = forms.BooleanField(
label='Parlamentar', widget=forms.RadioSelect(choices=YES_OR_NO), required=True)
choices=PARLAMENTARES_CHOICES,
widget=forms.Select(
attrs={'class': 'selector'}))
titular = forms.BooleanField(widget=forms.RadioSelect(choices=YES_OR_NO), required=True)
data_designacao = forms.DateField(label=u'Data Designação', data_designacao = forms.DateField(label=u'Data Designação',
input_formats=['%d/%m/%Y'], input_formats=['%d/%m/%Y'],
required=True, required=True,
widget=forms.DateInput(format='%d/%m/%Y')) widget=forms.DateInput(
format='%d/%m/%Y'))
data_desligamento = forms.DateField(label=u'Data Desligamento', data_desligamento = forms.DateField(label=u'Data Desligamento',
input_formats=['%d/%m/%Y'], input_formats=['%d/%m/%Y'],
required=False, required=False,
widget=forms.DateInput(format='%d/%m/%Y')) widget=forms.DateInput(
format='%d/%m/%Y'))
class Meta: class Meta:
model = Participacao model = Participacao
fields = ['parlamentar', fields = ['parlamentar_id',
'composicao',
'cargo', 'cargo',
'titular', 'titular',
'data_designacao', 'data_designacao',
'data_desligamento', 'data_desligamento',
'motivo_desligamento', 'motivo_desligamento',
'observacao'] 'observacao']
@ -150,29 +157,70 @@ class ParticipacaoCadastroForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.helper = FormHelper() self.helper = FormHelper()
self.helper.layout = Layout( self.helper.layout = Layout(
Fieldset( Fieldset(
'Formulário de Cadastro', 'Formulário de Cadastro',
'parlamentar', 'parlamentar_id',
'composicao', # TODO colocar como hidden (remover daqui) 'cargo',
'cargo', 'titular',
'titular', 'data_designacao',
'data_designacao', 'data_desligamento',
'data_desligamento', 'motivo_desligamento',
'motivo_desligamento', 'observacao'
'observacao' ),
), ButtonHolder(
# Field('documento', type="hidden"), Submit('submit', 'Salvar',
ButtonHolder( css_class='button primary')
Submit('submit', 'Salvar', )
css_class='button primary')
)
) )
super(ParticipacaoCadastroForm, self).__init__(*args, **kwargs) super(ParticipacaoCadastroForm, self).__init__(*args, **kwargs)
class ComissaoParlamentarIncluirView(FormMixin, GenericView): class ComissaoParlamentarIncluirView(FormMixin, GenericView):
template_name = "comissoes/comissao_parlamentar.html" template_name = "comissoes/comissao_parlamentar.html"
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
pk = kwargs['pk'] form = ParticipacaoCadastroForm()
form = ParticipacaoCadastroForm() return self.render_to_response({'form': form,
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})

5
templates/comissoes/comissao_parlamentar.html

@ -3,7 +3,10 @@
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% block detail_content %} {% block detail_content %}
<h2>Incluir Parlamentar</h2> <h2>Incluir Parlamentar</h2>
{% if error %} <h5 align="center"><font color="#FF0000">{{ error }}</font></h5> {% endif %}
{% crispy form %} {% crispy form %}
{% endblock detail_content %} {% endblock detail_content %}

2
templates/comissoes/composicao.html

@ -42,6 +42,6 @@
{% endfor %} {% endfor %}
</table> </table>
<a href="" class="button primary">Incluir Parlamentar</a> <a href="{% url 'comissao:comissao_parlamentar' pk composicao_id %}" class="button primary">Incluir Parlamentar</a>
{% endblock detail_content %} {% endblock detail_content %}
Loading…
Cancel
Save