Browse Source

Muda documento acessorio para CreateView

pull/258/head
Eduardo Edson Batista Cordeiro Alves 9 years ago
parent
commit
00e9a24e3c
  1. 12
      materia/forms.py
  2. 32
      materia/views.py
  3. 49
      templates/materia/documento_acessorio_edit.html

12
materia/forms.py

@ -114,12 +114,10 @@ class DocumentoAcessorioForm(ModelForm):
'data': forms.DateInput(attrs={'class': 'dateinput'})
}
def __init__(self, *args, **kwargs):
def __init__(self, excluir=False, *args, **kwargs):
row1 = crispy_layout_mixin.to_row(
[('tipo', 4),
('nome', 4),
('data', 4)])
[('tipo', 4), ('nome', 4), ('data', 4)])
row2 = crispy_layout_mixin.to_row(
[('autor', 12)])
@ -127,12 +125,16 @@ class DocumentoAcessorioForm(ModelForm):
row3 = crispy_layout_mixin.to_row(
[('ementa', 12)])
more = []
if excluir:
more = [Submit('Excluir', 'Excluir')]
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
_('Incluir Documento Acessório'),
row1, row2, row3,
form_actions()
form_actions(more=more)
)
)
super(DocumentoAcessorioForm, self).__init__(*args, **kwargs)

32
materia/views.py

@ -513,8 +513,9 @@ class NumeracaoEditView(FormView):
return reverse('numeracao', kwargs={'pk': pk})
class DocumentoAcessorioView(FormView):
class DocumentoAcessorioView(CreateView):
template_name = "materia/documento_acessorio.html"
form_class = DocumentoAcessorioForm
def get(self, request, *args, **kwargs):
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
@ -527,21 +528,14 @@ class DocumentoAcessorioView(FormView):
'docs': docs})
def post(self, request, *args, **kwargs):
form = DocumentoAcessorioForm(request.POST)
form = self.get_form()
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
docs_list = DocumentoAcessorio.objects.filter(
materia_id=kwargs['pk'])
if form.is_valid():
documento_acessorio = DocumentoAcessorio()
documento_acessorio = form.save(commit=False)
documento_acessorio.materia = materia
documento_acessorio.tipo = form.cleaned_data['tipo']
documento_acessorio.data = form.cleaned_data['data']
documento_acessorio.nome = form.cleaned_data['nome']
documento_acessorio.autor = form.cleaned_data['autor']
documento_acessorio.ementa = form.cleaned_data['ementa']
documento_acessorio.save()
return self.form_valid(form)
else:
@ -589,26 +583,23 @@ class AcompanhamentoExcluirView(TemplateView):
return HttpResponseRedirect(self.get_redirect_url())
class DocumentoAcessorioEditView(FormView):
class DocumentoAcessorioEditView(CreateView):
template_name = "materia/documento_acessorio_edit.html"
form_class = DocumentoAcessorioForm
def get(self, request, *args, **kwargs):
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
documento = DocumentoAcessorio.objects.get(id=kwargs['id'])
form = DocumentoAcessorioForm()
form = DocumentoAcessorioForm(instance=documento, excluir=True)
return self.render_to_response(
{'object': materia,
'form': form,
'doc': documento,
'tipos': TipoDocumento.objects.all()})
return self.render_to_response({'object': materia, 'form': form})
def post(self, request, *args, **kwargs):
form = DocumentoAcessorioForm(request.POST)
form = self.get_form()
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
documento = DocumentoAcessorio.objects.get(id=kwargs['id'])
if form.is_valid():
if 'excluir' in request.POST:
if 'Excluir' in request.POST:
documento.delete()
elif 'salvar' in request.POST:
documento.materia = materia
@ -617,9 +608,8 @@ class DocumentoAcessorioEditView(FormView):
documento.nome = form.cleaned_data['nome']
documento.autor = form.cleaned_data['autor']
documento.ementa = form.cleaned_data['ementa']
documento.save()
return self.form_valid(form)
return redirect(self.get_success_url())
else:
return self.render_to_response({'form': form,
'object': materia,

49
templates/materia/documento_acessorio_edit.html

@ -3,52 +3,5 @@
{% load crispy_forms_tags %}
{% block actions %} {% endblock %}
{% block detail_content %}
<fieldset>
<legend>Matéria Legislativa</legend>
{% include "materia/resumo_detail_materia.html" %}
<fieldset class="form-group">
<legend>Editar Documento Acessório</legend>
<form method="POST">
{% csrf_token %}
<div class="row">
<div class="col-md-4">
<labe>Tipo*</labe>
<select name="tipo" class="form-control">
{% for t in tipos %}
<option value="{{t.id}}" {% if t.id == doc.tipo.id %} selected {% endif %}>
{{t.descricao}}
</option>
{% endfor %}
</select>
</div>
<div class="col-md-4">
<labe>Data</labe>
<input type="text" name="data" value="{{doc.data|date:'d/m/Y'}}" class="dateinput form-control"/>
</div>
<div class="col-md-4">
<labe>Nome*</labe>
<input type="text" name="nome" value="{{doc.nome}}" class="form-control"/>
</div>
</div>
<div class="row">
<div class="col-md-6">
<labe>Autor*</labe>
<input type="text" name="autor" value="{{doc.autor}}" class="form-control"/>
</div>
<div class="col-md-6">
<labe>Ementa*</labe>
<input type="text" name="ementa" value="{{doc.ementa}}" class="form-control"/>
</div>
</div>
<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" />
</form>
</fieldset>
</fieldset>
{% crispy form %}
{% endblock %}

Loading…
Cancel
Save