From 55df6c2e5a051155a2fed42c3bd45fc119edbfc3 Mon Sep 17 00:00:00 2001 From: Edward Ribeiro Date: Wed, 16 Mar 2016 10:51:38 -0300 Subject: [PATCH] Refatora parlamentares e norma --- norma/forms.py | 30 ++++++++--- norma/views.py | 118 +++++++++++++++-------------------------- parlamentares/views.py | 1 - sessao/views.py | 1 - 4 files changed, 67 insertions(+), 83 deletions(-) diff --git a/norma/forms.py b/norma/forms.py index 4b4c850d3..2d16da2fb 100644 --- a/norma/forms.py +++ b/norma/forms.py @@ -1,13 +1,14 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Fieldset, Layout from django import forms -from django.core.exceptions import ValidationError +from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.forms import ModelForm from django.utils.safestring import mark_safe import crispy_layout_mixin from crispy_layout_mixin import form_actions -from materia.models import TipoMateriaLegislativa +from materia.models import MateriaLegislativa, TipoMateriaLegislativa +from sapl.utils import RANGE_ANOS from .models import NormaJuridica @@ -89,16 +90,18 @@ class NormaJuridicaPesquisaForm(ModelForm): class NormaJuridicaForm(ModelForm): + # Campos de MateriaLegislativa tipo_materia = forms.ModelChoiceField( label='Matéria Legislativa', required=False, queryset=TipoMateriaLegislativa.objects.all(), empty_label='Selecione' ) - - numero_materia = forms.CharField(label='Número', required=False) - - ano_materia = forms.CharField(label='Ano', required=False) + numero_materia = forms.CharField(label='Número', + required=False) + ano_materia = forms.ChoiceField(label='Ano', + required=False, + choices=RANGE_ANOS) def clean_texto_integral(self): texto_integral = self.cleaned_data.get('texto_integral', False) @@ -130,6 +133,21 @@ class NormaJuridicaForm(ModelForm): 'texto_integral', ] + def clean(self): + data = super(NormaJuridicaForm, self).clean() + + if self.cleaned_data['tipo_materia']: + try: + MateriaLegislativa.objects.get( + tipo=self.cleaned_data['tipo_materia'], + numero=self.cleaned_data['numero_materia'], + ano=self.cleaned_data['ano_materia']) + except ObjectDoesNotExist: + msg = 'Matéria adicionada não existe!' + raise forms.ValidationError(msg) + + return data + def __init__(self, *args, **kwargs): row1 = crispy_layout_mixin.to_row( diff --git a/norma/views.py b/norma/views.py index 32f5eb9a5..8f97224f1 100644 --- a/norma/views.py +++ b/norma/views.py @@ -1,11 +1,9 @@ from datetime import datetime -from django.contrib import messages -from django.core.exceptions import ObjectDoesNotExist -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse_lazy from django.http import HttpResponseRedirect from django.shortcuts import redirect -from django.views.generic import CreateView, FormView, ListView +from django.views.generic import CreateView, FormView, ListView, UpdateView from compilacao.views import IntegracaoTaView from crud.base import Crud, make_pagination @@ -24,13 +22,8 @@ LegislacaoCitadaCrud = Crud.build(LegislacaoCitada, '') class NormaPesquisaView(FormView): template_name = "norma/pesquisa.html" - - def get_success_url(self): - return reverse('normajuridica:norma_pesquisa') - - def get(self, request, *args, **kwargs): - form = NormaJuridicaPesquisaForm() - return self.render_to_response({'form': form}) + success_url = "normajuridica:norma_pesquisa" + form_class = NormaJuridicaPesquisaForm def post(self, request, *args, **kwargs): form = NormaJuridicaPesquisaForm(request.POST) @@ -120,74 +113,49 @@ class PesquisaNormaListView(ListView): class NormaIncluirView(CreateView): template_name = "norma/normajuridica_incluir.html" form_class = NormaJuridicaForm - - def get_success_url(self): - return reverse('normajuridica:list') - - def get(self, request, *args, **kwargs): - form = NormaJuridicaForm() - return self.render_to_response({'form': form}) - - def post(self, request, *args, **kwargs): - form = self.get_form() - - if form.is_valid(): - norma = form.save(commit=False) - - if form.cleaned_data['tipo_materia']: - try: - materia = MateriaLegislativa.objects.get( - tipo_id=form.cleaned_data['tipo_materia'], - numero=form.cleaned_data['numero_materia'], - ano=form.cleaned_data['ano_materia']) - except ObjectDoesNotExist: - msg = 'Matéria adicionada não existe!' - messages.add_message(request, messages.INFO, msg) - return self.render_to_response({'form': form}) - else: - norma.materia = materia - norma.timestamp = datetime.now() - norma.save() - return HttpResponseRedirect(self.get_success_url()) - else: - return self.render_to_response({'form': form}) - - -class NormaEditView(CreateView): + success_url = reverse_lazy('normajuridica:list') + + def form_valid(self, form): + norma = form.save(commit=False) + norma.timestamp = datetime.now() + if form.cleaned_data['tipo_materia']: + materia = MateriaLegislativa.objects.get( + tipo_id=form.data['tipo_materia'], + numero=form.data['numero_materia'], + ano=form.data['ano_materia']) + norma.materia = materia + norma.save() + return HttpResponseRedirect(self.get_success_url()) + + +class NormaEditView(UpdateView): template_name = "norma/normajuridica_incluir.html" form_class = NormaJuridicaForm + model = NormaJuridica + success_url = reverse_lazy('normajuridica:list') - def get(self, request, *args, **kwargs): - norma = NormaJuridica.objects.get(id=self.kwargs['pk']) - form = NormaJuridicaForm(instance=norma) - return self.render_to_response({'form': form}) - - def post(self, request, *args, **kwargs): + def get_initial(self): + data = super(NormaEditView, self).get_initial() norma = NormaJuridica.objects.get(id=self.kwargs['pk']) - form = NormaJuridicaForm(instance=norma, data=request.POST) - - if form.is_valid(): - if form.data['tipo_materia']: - try: - materia = MateriaLegislativa.objects.get( - tipo_id=form.data['tipo_materia'], - numero=form.data['numero_materia'], - ano=form.data['ano_materia']) - except ObjectDoesNotExist: - msg = 'Matéria adicionada não existe!' - messages.add_message(request, messages.INFO, msg) - return self.render_to_response({'form': form}) - else: - norma.materia = materia - norma = form.save(commit=False) - norma.timestamp = datetime.now() - norma.save() - return HttpResponseRedirect(self.get_success_url()) - else: - return self.render_to_response({'form': form}) - - def get_success_url(self): - return reverse('normajuridica:list') + if norma.materia: + data.update({ + 'tipo_materia': norma.materia.tipo, + 'numero_materia': norma.materia.numero, + 'ano_materia': norma.materia.ano, + }) + return data + + def form_valid(self, form): + norma = form.save(commit=False) + norma.timestamp = datetime.now() + if form.cleaned_data['tipo_materia']: + materia = MateriaLegislativa.objects.get( + tipo_id=form.data['tipo_materia'], + numero=form.data['numero_materia'], + ano=form.data['ano_materia']) + norma.materia = materia + norma.save() + return HttpResponseRedirect(self.get_success_url()) class NormaTaView(IntegracaoTaView): diff --git a/parlamentares/views.py b/parlamentares/views.py index 1a5ab7c80..deacb89b2 100644 --- a/parlamentares/views.py +++ b/parlamentares/views.py @@ -185,7 +185,6 @@ class ParlamentaresCadastroView(CreateView): return context def form_valid(self, form): - import ipdb; ipdb.set_trace() form.save() mandato = Mandato() mandato.parlamentar = form.instance diff --git a/sessao/views.py b/sessao/views.py index 70dc2aef2..9fdf858a8 100644 --- a/sessao/views.py +++ b/sessao/views.py @@ -2053,7 +2053,6 @@ class VotacaoExpedienteEditView(FormMixin, SessaoCrud.CrudDetailView): yield tipo def get(self, request, *args, **kwargs): - # import ipdb; ipdb.set_trace() self.object = self.get_object() context = self.get_context_data(object=self.object)