Browse Source

Muda despacho inicial para CreateView e refatora a edição

pull/250/head
Eduardo Edson Batista Cordeiro Alves 9 years ago
parent
commit
f5894c2fbc
  1. 9
      materia/forms.py
  2. 23
      materia/views.py
  3. 21
      templates/materia/despacho_inicial_edit.html

9
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)

23
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,

21
templates/materia/despacho_inicial_edit.html

@ -3,24 +3,5 @@
{% load crispy_forms_tags %}
{% block actions %} {% endblock %}
{% block detail_content %}
<fieldset>
<legend>Matéria Legislativa</legend>
{% include "materia/resumo_detail_materia.html" %}
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend>Editar Despacho</legend>
<select name="comissao" class="form-control">
{% for c in comissoes %}
<option value="{{c.id}}" {% if c.id == despacho.comissao.id %} selected {% endif %}> {{c.nome}} </option>
{% endfor %}
</select>
<br />
<input type="submit" value="Salvar" id="salvar" name="salvar" class="btn btn-primary" />
<input type="submit" value="Excluir" id="excluir" name="excluir" class="btn btn-danger" />
</fieldset>
</form>
</fieldset>
{% crispy form %}
{% endblock %}

Loading…
Cancel
Save