Browse Source

Altera ParlamentaresEditView

pull/268/head
Eduardo Calil 9 years ago
parent
commit
8ce1150b29
  1. 59
      parlamentares/views.py

59
parlamentares/views.py

@ -3,7 +3,7 @@ import os
from django.contrib import messages from django.contrib import messages
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import CreateView, FormView from django.views.generic import FormView
from crud import Crud from crud import Crud
@ -139,44 +139,27 @@ class ParlamentaresCadastroView(FormView):
{'form': form, 'legislatura_id': pk}) {'form': form, 'legislatura_id': pk})
class ParlamentaresEditarView(FormView): class ParlamentaresEditarView(UpdateView):
template_name = "parlamentares/parlamentares_cadastro.html" template_name = "parlamentares/parlamentares_cadastro.html"
form_class = ParlamentaresForm form_class = ParlamentaresEditForm
model = Parlamentar
def get_success_url(self): success_url = reverse_lazy('parlamentares')
return reverse('parlamentares')
def form_valid(self, form):
def get(self, request, *args, **kwargs): parlamentar = form.instance
pk = kwargs['pk'] if 'salvar' in self.request.POST:
parlamentar = Parlamentar.objects.get(pk=pk) form.save()
form = ParlamentaresEditForm(instance=parlamentar) elif 'excluir' in self.request.POST:
return self.render_to_response( Mandato.objects.get(parlamentar=parlamentar).delete()
{'form': form, 'object': parlamentar}) parlamentar.delete()
elif "remover-foto" in self.request.POST:
def post(self, request, *args, **kwargs): try:
pk = kwargs['pk'] os.unlink(parlamentar.fotografia.path)
parlamentar = Parlamentar.objects.get(pk=pk) except OSError:
pass # Should log this error!!!!!
form = ParlamentaresEditForm(request.POST, parlamentar.fotografia = None
request.FILES, parlamentar.save()
instance=parlamentar) return HttpResponseRedirect(self.get_success_url())
if form.is_valid():
if 'salvar' in request.POST:
form.save()
elif 'excluir' in request.POST:
Mandato.objects.get(parlamentar=parlamentar).delete()
parlamentar.delete()
elif "remover" in request.POST:
try:
os.unlink(parlamentar.fotografia.path)
except OSError:
pass # Should log this error!!!!!
parlamentar.fotografia = None
parlamentar.save()
return self.form_valid(form)
else:
return self.render_to_response({'form': form})
class ParlamentaresDependentesView(FormView): class ParlamentaresDependentesView(FormView):

Loading…
Cancel
Save