diff --git a/parlamentares/urls.py b/parlamentares/urls.py index 4ae7af173..11a18e248 100644 --- a/parlamentares/urls.py +++ b/parlamentares/urls.py @@ -1,6 +1,6 @@ from django.conf.urls import include, url -from parlamentares.views import (FiliacaoView, MesaDiretoraView, - ParlamentaresCadastroView, +from parlamentares.views import (FiliacaoEditView, FiliacaoView, + MesaDiretoraView, ParlamentaresCadastroView, ParlamentaresDependentesEditView, ParlamentaresDependentesView, ParlamentaresEditarView, ParlamentaresView, @@ -42,6 +42,9 @@ urlpatterns = [ url(r'^parlamentares/(?P\d+)/filiacao$', FiliacaoView.as_view(), name='parlamentares_filiacao'), + url(r'^parlamentares/(?P\d+)/filiacao/(?P\d+)$', + FiliacaoEditView.as_view(), + name='parlamentares_filiacao_edit'), url(r'^parlamentares/(?P\d+)/(?P\d+)/editar$', ParlamentaresEditarView.as_view(), name='parlamentares_editar'), diff --git a/parlamentares/views.py b/parlamentares/views.py index 9cebc8950..e26ba3136 100644 --- a/parlamentares/views.py +++ b/parlamentares/views.py @@ -641,8 +641,21 @@ class FiliacaoForm(ModelForm): *args, **kwargs) -class FiliacaoView(FormMixin, GenericView): +class FiliacaoEditForm(FiliacaoForm): + + def __init__(self, *args, **kwargs): + super(FiliacaoEditForm, self).__init__( + *args, **kwargs) + + self.helper.layout[0][-1:] = ButtonHolder( + Submit('Salvar', 'Salvar', + css_class='button primary'), + HTML(' '), + Submit('Excluir', 'Excluir', + css_class='button primary'),) + +class FiliacaoView(FormMixin, GenericView): template_name = "parlamentares/parlamentares_filiacao.html" def get_success_url(self): @@ -679,7 +692,7 @@ class FiliacaoView(FormMixin, GenericView): pid = kwargs['pk'] parlamentar = Parlamentar.objects.get(id=pid) filiacoes = Filiacao.objects.filter( - parlamentar=parlamentar) + parlamentar=parlamentar) return self.render_to_response( {'parlamentar': parlamentar, @@ -687,3 +700,39 @@ class FiliacaoView(FormMixin, GenericView): 'form': form, 'legislatura_id': parlamentar.mandato_set.last( ).legislatura.id}) + + +class FiliacaoEditView(FormMixin, GenericView): + template_name = "parlamentares/parlamentares_filiacao_edit.html" + + def get_success_url(self): + pk = self.kwargs['pk'] + return reverse('parlamentares_filiacao', kwargs={'pk': pk}) + + def get(self, request, *args, **kwargs): + filiacao = Filiacao.objects.get(id=kwargs['dk']) + parlamentar = Parlamentar.objects.get(id=kwargs['pk']) + form = FiliacaoEditForm(instance=filiacao) + return self.render_to_response( + {'form': form, + 'parlamentar': parlamentar, + 'legislatura_id': parlamentar.mandato_set.last( + ).legislatura_id}) + + def post(self, request, *args, **kwargs): + filiacao = Filiacao.objects.get(id=kwargs['dk']) + form = FiliacaoEditForm(request.POST, instance=filiacao) + parlamentar = Parlamentar.objects.get(id=kwargs['pk']) + + if form.is_valid(): + if 'Salvar' in request.POST: + filiacao.save() + elif 'Excluir' in request.POST: + filiacao.delete() + return self.form_valid(form) + else: + return self.render_to_response( + {'form': form, + 'parlamentar': parlamentar, + 'legislatura_id': parlamentar.mandato_set.last( + ).legislatura_id}) diff --git a/templates/parlamentares/parlamentares_filiacao.html b/templates/parlamentares/parlamentares_filiacao.html index bb9e126ad..60c0a112f 100644 --- a/templates/parlamentares/parlamentares_filiacao.html +++ b/templates/parlamentares/parlamentares_filiacao.html @@ -24,7 +24,7 @@ {% for f in filiacoes %} - {{f.partido.sigla}} + {{f.partido.sigla}} {{f.data}} {{f.data_desfiliacao}} diff --git a/templates/parlamentares/parlamentares_filiacao_edit.html b/templates/parlamentares/parlamentares_filiacao_edit.html new file mode 100644 index 000000000..cfe9a7702 --- /dev/null +++ b/templates/parlamentares/parlamentares_filiacao_edit.html @@ -0,0 +1,16 @@ +{% extends "crud/detail.html" %} +{% load i18n %} +{% load crispy_forms_tags %} +{% block actions %} + +{% endblock %} +{% block detail_content %} + +{% crispy form %} + +{% endblock detail_content %}