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', 'ano_materia',
'data_materia'] 'data_materia']
widgets = { def __init__(self, excluir=False, *args, **kwargs):
'data_materia': forms.DateInput(attrs={'class': 'dateinput'}), more = []
'ano_materia': forms.Select(attrs={'class': 'selector'}), if excluir:
} more = [Submit('Excluir', 'Excluir')]
def __init__(self, *args, **kwargs):
row1 = crispy_layout_mixin.to_row( row1 = crispy_layout_mixin.to_row(
[('tipo_materia', 12)]) [('tipo_materia', 12)])
row2 = crispy_layout_mixin.to_row( row2 = crispy_layout_mixin.to_row(
[('numero_materia', 4), [('numero_materia', 4), ('ano_materia', 4), ('data_materia', 4)])
('ano_materia', 4),
('data_materia', 4)])
self.helper = FormHelper() self.helper = FormHelper()
self.helper.layout = Layout( self.helper.layout = Layout(
Fieldset( Fieldset(
_('Incluir Numeração'), _('Incluir Numeração'),
row1, row2, row1, row2,
form_actions() form_actions(more=more)
) )
) )
super(NumeracaoForm, self).__init__(*args, **kwargs) 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')) verbose_name=_('Número'))
ano_materia = models.PositiveSmallIntegerField(verbose_name=_('Ano'), ano_materia = models.PositiveSmallIntegerField(verbose_name=_('Ano'),
choices=RANGE_ANOS) choices=RANGE_ANOS)
data_materia = models.DateField( data_materia = models.DateField(verbose_name=_('Data'))
blank=True, verbose_name=_('Data'))
class Meta: class Meta:
verbose_name = _('Numeração') verbose_name = _('Numeração')

31
materia/views.py

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

42
templates/materia/numeracao_edit.html

@ -3,45 +3,5 @@
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% block actions %} {% endblock %} {% block actions %} {% endblock %}
{% block detail_content %} {% block detail_content %}
<fieldset> {% crispy form %}
<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>
{% endblock %} {% endblock %}

Loading…
Cancel
Save