From 44e9b83dcb42afe476ffd39389c3adfbcde00b36 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Tue, 12 Jan 2016 11:52:48 -0200 Subject: [PATCH] Refactor CasaLegislativa POST Fix #108 and Fix #110 --- base/views.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/base/views.py b/base/views.py index d118a67d0..03e498d63 100644 --- a/base/views.py +++ b/base/views.py @@ -181,22 +181,23 @@ class CasaLegislativaTableAuxView(FormMixin, GenericView): form = CasaLegislativaTabelaAuxForm(request.POST, request.FILES) if form.is_valid(): - try: - casa = CasaLegislativa.objects.first() - except ObjectDoesNotExist: - casa_save = form.save(commit=False) - else: + casa = CasaLegislativa.objects.first() + if casa is not None: if "remover" in request.POST: try: os.unlink(casa.logotipo.path) except OSError: pass # Should log this error!!!!! casa.logotipo = None - casa_save = CasaLegislativaTabelaAuxForm( - request.POST, - request.FILES, - instance=casa).save(commit=False) - casa_save.save() + casa_save = CasaLegislativaTabelaAuxForm( + request.POST, + request.FILES, + instance=casa + ).save(commit=False) + casa_save.save() + elif casa is None: + form.save() + return self.form_valid(form) else: return self.render_to_response({'form': form})