Browse Source

Refatora edicao de numeracao para creatview

pull/258/head
Eduardo Edson Batista Cordeiro Alves 9 years ago
parent
commit
2f68841f1a
  1. 16
      materia/forms.py
  2. 20
      materia/migrations/0024_auto_20160311_1501.py
  3. 3
      materia/models.py
  4. 31
      materia/views.py
  5. 42
      templates/materia/numeracao_edit.html

16
materia/forms.py

@ -313,26 +313,22 @@ class NumeracaoForm(ModelForm):
'ano_materia',
'data_materia']
widgets = {
'data_materia': forms.DateInput(attrs={'class': 'dateinput'}),
'ano_materia': forms.Select(attrs={'class': 'selector'}),
}
def __init__(self, *args, **kwargs):
def __init__(self, excluir=False, *args, **kwargs):
more = []
if excluir:
more = [Submit('Excluir', 'Excluir')]
row1 = crispy_layout_mixin.to_row(
[('tipo_materia', 12)])
row2 = crispy_layout_mixin.to_row(
[('numero_materia', 4),
('ano_materia', 4),
('data_materia', 4)])
[('numero_materia', 4), ('ano_materia', 4), ('data_materia', 4)])
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
_('Incluir Numeração'),
row1, row2,
form_actions()
form_actions(more=more)
)
)
super(NumeracaoForm, self).__init__(*args, **kwargs)

20
materia/migrations/0024_auto_20160311_1501.py

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-03-11 18:01
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('materia', '0023_auto_20160309_1407'),
]
operations = [
migrations.AlterField(
model_name='numeracao',
name='data_materia',
field=models.DateField(verbose_name='Data'),
),
]

3
materia/models.py

@ -309,8 +309,7 @@ class Numeracao(models.Model):
verbose_name=_('Número'))
ano_materia = models.PositiveSmallIntegerField(verbose_name=_('Ano'),
choices=RANGE_ANOS)
data_materia = models.DateField(
blank=True, verbose_name=_('Data'))
data_materia = models.DateField(verbose_name=_('Data'))
class Meta:
verbose_name = _('Numeração')

31
materia/views.py

@ -428,34 +428,28 @@ class LegislacaoCitadaEditView(FormView):
'object': materia})
class NumeracaoView(FormView):
class NumeracaoView(CreateView):
template_name = "materia/numeracao.html"
form_class = NumeracaoForm
def get(self, request, *args, **kwargs):
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
numeracao = Numeracao.objects.filter(materia_id=kwargs['pk'])
form = NumeracaoForm()
return self.render_to_response(
{'object': materia,
'form': form,
'form': self.get_form(),
'numeracao': numeracao})
def post(self, request, *args, **kwargs):
form = NumeracaoForm(request.POST)
form = self.get_form()
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
numeracao_list = Numeracao.objects.filter(
materia_id=kwargs['pk'])
if form.is_valid():
numeracao = Numeracao()
numeracao = form.save(commit=False)
numeracao.materia = materia
numeracao.tipo_materia = form.cleaned_data['tipo_materia']
numeracao.numero_materia = form.cleaned_data['numero_materia']
numeracao.ano_materia = form.cleaned_data['ano_materia']
numeracao.data_materia = form.cleaned_data['data_materia']
numeracao.save()
return self.form_valid(form)
else:
@ -468,23 +462,22 @@ class NumeracaoView(FormView):
return reverse('numeracao', kwargs={'pk': pk})
class NumeracaoEditView(FormView):
class NumeracaoEditView(CreateView):
template_name = "materia/numeracao_edit.html"
form_class = NumeracaoForm
def get(self, request, *args, **kwargs):
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
numeracao = Numeracao.objects.get(id=kwargs['id'])
form = NumeracaoForm()
form = NumeracaoForm(instance=numeracao, excluir=True)
return self.render_to_response(
{'object': materia,
'form': form,
'numeracao': numeracao,
'tipos': TipoMateriaLegislativa.objects.all()})
'numeracao': numeracao})
def post(self, request, *args, **kwargs):
form = NumeracaoForm(request.POST)
form = self.get_form()
materia = MateriaLegislativa.objects.get(id=kwargs['pk'])
numeracao = Numeracao.objects.get(id=kwargs['id'])
@ -492,17 +485,13 @@ class NumeracaoEditView(FormView):
if 'excluir' in request.POST:
numeracao.delete()
elif 'salvar' in request.POST:
tipo = TipoMateriaLegislativa.objects.get(
id=form.cleaned_data['tipo_materia'])
numeracao.materia = materia
numeracao.tipo_materia = tipo
numeracao.tipo_materia = form.cleaned_data['tipo_materia']
numeracao.numero_materia = form.cleaned_data['numero_materia']
numeracao.ano_materia = form.cleaned_data['ano_materia']
numeracao.data_materia = form.cleaned_data['data_materia']
numeracao.save()
return self.form_valid(form)
return redirect(self.get_success_url())
else:
return self.render_to_response({'form': form,
'object': materia,

42
templates/materia/numeracao_edit.html

@ -3,45 +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 Numeração</legend>
<form method="POST">
{% csrf_token %}
<div class="row">
<div class="col-md-12">
<label>Tipo Matéria*</label>
<select name="tipo_materialegislativa" class="form-control">
{% for t in tipos %}
<option value="{{t.id}}" {% if t.id == numeracao.tipo_materia %} selected {% endif %}>
{{t.sigla}} - {{t.descricao}}
</option>
{% endfor %}
</select>
</div>
</div>
<div class="row">
<div class="col-md-4">
<label>Número*</select>label>
<input type="text" name="numero_materialegislativa" value="{{numeracao.numero_materia}}" class="form-control"/>
</div>
<div class="col-md-4">
<label>Ano*</label>
<input type="text" name="ano_materialegislativa" value="{{numeracao.ano_materia}}" class="form-control"/>
</div>
<div class="col-md-4">
<label>Data</label>
<input type="text" name="data_materialegislativa" class="dateinput form-control" value="{{numeracao.data_materia|date:'d/m/Y'}}"/>
</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