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. 63
      parlamentares/views.py

4
parlamentares/urls.py

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

63
parlamentares/views.py

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

Loading…
Cancel
Save