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 %} -
+ {% crispy form %} {% endblock %}