Browse Source

Implement delete parlamentar

pull/11/merge
Eduardo Edson Batista Cordeiro Alves 9 years ago
parent
commit
d4f9973bbe
  1. 4
      parlamentares/urls.py
  2. 41
      parlamentares/views.py

4
parlamentares/urls.py

@ -1,7 +1,7 @@
from django.conf.urls import include, url
from parlamentares.views import (ParlamentaresCadastroView,
ParlamentaresEditarView,
ParlamentaresView,
ParlamentaresEditarView, ParlamentaresView,
cargo_mesa_crud, coligacao_crud,
legislatura_crud, nivel_instrucao_crud,
parlamentar_crud, partido_crud,

41
parlamentares/views.py

@ -1,7 +1,7 @@
import sapl
from re import sub
from crispy_forms.helper import FormHelper
from crispy_forms.layout import ButtonHolder, Fieldset, Layout, Submit
from crispy_forms.layout import HTML, ButtonHolder, Fieldset, Layout, Submit
from django import forms
from django.core.urlresolvers import reverse
from django.forms import ModelForm
@ -9,9 +9,11 @@ from django.utils.html import strip_tags
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.views.generic.edit import FormMixin
from sapl.crud import build_crud
from vanilla import GenericView
import sapl
from sapl.crud import build_crud
from .models import (CargoMesa, Coligacao, Dependente, Filiacao, Legislatura,
Mandato, NivelInstrucao, Parlamentar, Partido,
SessaoLegislativa, SituacaoMilitar, TipoAfastamento,
@ -278,8 +280,8 @@ class ParlamentaresForm (ModelForm):
row6, row7, row8, row9, row10,
row11, row12, row13, row14,
ButtonHolder(
Submit('sumbmit', 'Salvar',
css_class='button primary')
Submit('submit', 'Salvar',
css_class='button primary'),
))
)
@ -287,6 +289,20 @@ class ParlamentaresForm (ModelForm):
*args, **kwargs)
class ParlamentaresEditForm(ParlamentaresForm):
def __init__(self, *args, **kwargs):
super(ParlamentaresEditForm, 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 ParlamentaresCadastroView(FormMixin, GenericView):
template_name = "parlamentares/parlamentares_cadastro.html"
@ -304,7 +320,6 @@ class ParlamentaresCadastroView(FormMixin, GenericView):
pk = kwargs['pk']
if form.is_valid():
parlamentar = form.save(commit=False)
if 'fotografia' in request.FILES:
parlamentar.fotografia = request.FILES['fotografia']
@ -313,6 +328,11 @@ class ParlamentaresCadastroView(FormMixin, GenericView):
strip_tags(form.data['biografia']))
parlamentar.save()
mandato = Mandato()
mandato.parlamentar = parlamentar
mandato.legislatura = Legislatura.objects.get(id=pk)
mandato.save()
return self.form_valid(form)
else:
return self.render_to_response(
@ -330,17 +350,17 @@ class ParlamentaresEditarView(FormMixin, GenericView):
pid = kwargs['pid']
parlamentar = Parlamentar.objects.get(id=pid)
form = ParlamentaresForm(instance=parlamentar)
form = ParlamentaresEditForm(instance=parlamentar)
return self.render_to_response({'form': form, 'legislatura_id': pk})
def post(self, request, *args, **kwargs):
pk = kwargs['pk']
pid = kwargs['pid']
parlamentar = Parlamentar.objects.get(id=pid)
form = ParlamentaresForm(request.POST, instance=parlamentar)
form = ParlamentaresEditForm(request.POST, instance=parlamentar)
if form.is_valid():
if 'salvar' in request.POST:
parlamentar = form.save(commit=False)
if 'fotografia' in request.FILES:
parlamentar.fotografia = request.FILES['fotografia']
@ -348,6 +368,9 @@ class ParlamentaresEditarView(FormMixin, GenericView):
' ',
strip_tags(form.data['biografia']))
parlamentar.save()
elif 'excluir' in request.POST:
Mandato.objects.get(parlamentar=parlamentar).delete()
parlamentar.delete()
return self.form_valid(form)
else:

Loading…
Cancel
Save