From f5894c2fbc4be73913966d798e1056dcabf35286 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Tue, 8 Mar 2016 11:47:35 -0300 Subject: [PATCH] =?UTF-8?q?Muda=20despacho=20inicial=20para=20CreateView?= =?UTF-8?q?=20e=20refatora=20a=20edi=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- materia/forms.py | 9 ++++++-- materia/views.py | 23 ++++++++------------ templates/materia/despacho_inicial_edit.html | 21 +----------------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/materia/forms.py b/materia/forms.py index 3e9ce7762..af1794f04 100644 --- a/materia/forms.py +++ b/materia/forms.py @@ -432,13 +432,18 @@ class DespachoInicialForm(ModelForm): model = DespachoInicial fields = ['comissao'] - def __init__(self, *args, **kwargs): + def __init__(self, excluir=False, *args, **kwargs): + + more = [] + if excluir: + more = [Submit('Excluir', 'Excluir')] + self.helper = FormHelper() self.helper.layout = Layout( Fieldset( _('Adicionar Despacho Inicial'), 'comissao', - form_actions() + form_actions(more=more) ) ) super(DespachoInicialForm, self).__init__(*args, **kwargs) diff --git a/materia/views.py b/materia/views.py index e3b05c22b..9ec885e59 100644 --- a/materia/views.py +++ b/materia/views.py @@ -234,7 +234,7 @@ class MateriaAnexadaEditView(FormMixin, GenericView): return reverse('materia_anexada', kwargs={'pk': pk}) -class DespachoInicialView(FormMixin, GenericView): +class DespachoInicialView(CreateView): template_name = "materia/despacho_inicial.html" form_class = DespachoInicialForm @@ -249,7 +249,7 @@ class DespachoInicialView(FormMixin, GenericView): 'despachos': despacho}) def post(self, request, *args, **kwargs): - form = DespachoInicialForm(request.POST) + form = self.get_form() materia = MateriaLegislativa.objects.get(id=kwargs['pk']) despacho = DespachoInicial.objects.filter(materia_id=materia.id) @@ -258,7 +258,7 @@ class DespachoInicialView(FormMixin, GenericView): despacho.comissao = form.cleaned_data['comissao'] despacho.materia = materia despacho.save() - return self.form_valid(form) + return redirect(self.get_success_url()) else: return self.render_to_response({'form': form, 'object': materia, @@ -269,35 +269,30 @@ class DespachoInicialView(FormMixin, GenericView): return reverse('despacho_inicial', kwargs={'pk': pk}) -class DespachoInicialEditView(FormMixin, GenericView): +class DespachoInicialEditView(CreateView): template_name = "materia/despacho_inicial_edit.html" form_class = DespachoInicialForm def get(self, request, *args, **kwargs): materia = MateriaLegislativa.objects.get(id=kwargs['pk']) despacho = DespachoInicial.objects.get(id=kwargs['id']) - form = DespachoInicialForm() + form = DespachoInicialForm(instance=despacho, excluir=True) - return self.render_to_response( - {'object': materia, - 'form': form, - 'despacho': despacho, - 'comissoes': Comissao.objects.all()}) + return self.render_to_response({'object': materia, 'form': form}) def post(self, request, *args, **kwargs): - form = DespachoInicialForm(request.POST) + form = self.get_form() materia = MateriaLegislativa.objects.get(id=kwargs['pk']) despacho = DespachoInicial.objects.get(id=kwargs['id']) if form.is_valid(): - if 'excluir' in request.POST: + if 'Excluir' in request.POST: despacho.delete() - return self.form_valid(form) elif 'salvar' in request.POST: despacho.comissao = form.cleaned_data['comissao'] despacho.materia = materia despacho.save() - return self.form_valid(form) + return redirect(self.get_success_url()) else: return self.render_to_response( {'object': materia, diff --git a/templates/materia/despacho_inicial_edit.html b/templates/materia/despacho_inicial_edit.html index 2054719c5..338595dc0 100644 --- a/templates/materia/despacho_inicial_edit.html +++ b/templates/materia/despacho_inicial_edit.html @@ -3,24 +3,5 @@ {% load crispy_forms_tags %} {% block actions %} {% endblock %} {% block detail_content %} -
- Matéria Legislativa - {% include "materia/resumo_detail_materia.html" %} - -
- {% csrf_token %} - -
- Editar Despacho - -
- - -
-
-
+ {% crispy form %} {% endblock %}