From 2f68841f1a69e5772b794ae50a5755675f76e741 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Fri, 11 Mar 2016 15:07:29 -0300 Subject: [PATCH] Refatora edicao de numeracao para creatview --- materia/forms.py | 16 +++---- materia/migrations/0024_auto_20160311_1501.py | 20 +++++++++ materia/models.py | 3 +- materia/views.py | 31 +++++--------- templates/materia/numeracao_edit.html | 42 +------------------ 5 files changed, 38 insertions(+), 74 deletions(-) create mode 100644 materia/migrations/0024_auto_20160311_1501.py diff --git a/materia/forms.py b/materia/forms.py index ef18e9869..f092234ef 100644 --- a/materia/forms.py +++ b/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) diff --git a/materia/migrations/0024_auto_20160311_1501.py b/materia/migrations/0024_auto_20160311_1501.py new file mode 100644 index 000000000..dd2ca6397 --- /dev/null +++ b/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'), + ), + ] diff --git a/materia/models.py b/materia/models.py index a358b303b..adaeddce9 100644 --- a/materia/models.py +++ b/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') diff --git a/materia/views.py b/materia/views.py index 4b72269b7..6e4f4d0d3 100644 --- a/materia/views.py +++ b/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, diff --git a/templates/materia/numeracao_edit.html b/templates/materia/numeracao_edit.html index 692f4cfd3..6cf29f2e6 100644 --- a/templates/materia/numeracao_edit.html +++ b/templates/materia/numeracao_edit.html @@ -3,45 +3,5 @@ {% load crispy_forms_tags %} {% block actions %} {% endblock %} {% block detail_content %} -
- Matéria Legislativa - {% include "materia/resumo_detail_materia.html" %} - -
- Editar Numeração -
- {% csrf_token %} -
-
- - -
-
- -
-
-
-
- - -
-
- - -
-
-
- - -
-
-
+ {% crispy form %} {% endblock %}