From e4d875f711af75a12db25201ee0e19cb0643e317 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Wed, 25 Nov 2015 13:38:43 -0200 Subject: [PATCH] =?UTF-8?q?Refactor=20Preposi=C3=A7=C3=A3o=20Form=20and=20?= =?UTF-8?q?Add=20Preposi=C3=A7=C3=A3o=20List?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- materia/urls.py | 7 ++- materia/views.py | 67 ++++++++++++++++++-------- templates/base.html | 7 ++- templates/crud/form.html | 14 ------ templates/materia/proposicao.html | 35 ++++++++++++++ templates/materia/proposicao_list.html | 38 +++++++++++++++ templates/paginacao.html | 62 +++++++++++++----------- 7 files changed, 165 insertions(+), 65 deletions(-) create mode 100644 templates/materia/proposicao_list.html diff --git a/materia/urls.py b/materia/urls.py index 4deccbd68..9fad29f2a 100644 --- a/materia/urls.py +++ b/materia/urls.py @@ -5,7 +5,8 @@ from materia.views import (AutoriaEditView, AutoriaView, FormularioCadastroView, FormularioSimplificadoView, LegislacaoCitadaEditView, LegislacaoCitadaView, MateriaAnexadaEditView, MateriaAnexadaView, - NumeracaoEditView, NumeracaoView, ProposicaoView, + NumeracaoEditView, NumeracaoView, + ProposicaoListView, ProposicaoView, RelatoriaEditView, RelatoriaView, TramitacaoEditView, TramitacaoView, autor_crud, materia_legislativa_crud, orgao_crud, origem_crud, @@ -72,5 +73,7 @@ urlpatterns = [ url(r'^materia/(?P\d+)/relatoria/(?P\d+)/edit$', RelatoriaEditView.as_view(), name='relatoria_edit'), url(r'^materia/proposicao$', - ProposicaoView.as_view(), name='proposicao'), + ProposicaoView.as_view(), name='adicionar_proposicao'), + url(r'^materia/proposicao_list$', + ProposicaoListView.as_view(), name='list_proposicao'), ] diff --git a/materia/views.py b/materia/views.py index 82c035657..6ae633074 100644 --- a/materia/views.py +++ b/materia/views.py @@ -1,15 +1,20 @@ from datetime import date, datetime +from re import sub import sapl from comissoes.models import Comissao, Composicao from crispy_forms.helper import FormHelper -from crispy_forms.layout import HTML, ButtonHolder, Fieldset, Layout, Submit, Column +from crispy_forms.layout import (HTML, ButtonHolder, Column, Fieldset, Layout, + Submit) from django import forms from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse from django.forms import ModelForm +from django.utils.html import strip_tags from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ +from django.views.generic import ListView +from django.views.generic.detail import SingleObjectMixin from django.views.generic.edit import FormMixin from norma.models import LegislacaoCitada, NormaJuridica, TipoNormaJuridica from sapl.crud import build_crud @@ -22,7 +27,6 @@ from .models import (Anexada, Autor, Autoria, DespachoInicial, TipoFimRelatoria, TipoMateriaLegislativa, TipoProposicao, Tramitacao, UnidadeTramitacao) - origem_crud = build_crud( Origem, 'origem', [ @@ -1764,7 +1768,7 @@ class ProposicaoView(FormMixin, GenericView): template_name = "materia/proposicao.html" def get_success_url(self): - return reverse('proposicao') + return reverse('list_proposicao') def get(self, request, *args, **kwargs): form = ProposicaoForm() @@ -1774,26 +1778,51 @@ class ProposicaoView(FormMixin, GenericView): form = ProposicaoForm(request.POST) if form.is_valid(): - proposicao = form.save(commit=False) if 'texto_original' in request.FILES: proposicao.texto_original = request.FILES['texto_original'] - try: - materia = MateriaLegislativa.objects.get( - tipo_id=int(form.data['tipo_materia']), - ano=int(form.data['ano_materia']), - numero=int(form.data['numero_materia'])) - except ObjectDoesNotExist: - return self.render_to_response( - {'form': form, - 'error': 'Matéria adiconada não existe!'}) - else: - proposicao.autor = materia.autoria_set.first().autor - proposicao.materia = materia - proposicao.data_envio = datetime.now() - proposicao.save() + tipo = TipoProposicao.objects.get( + id=int(request.POST['tipo'])) + if tipo.descricao == 'Parecer': + try: + materia = MateriaLegislativa.objects.get( + tipo_id=int(form.data['tipo_materia']), + ano=int(form.data['ano_materia']), + numero=int(form.data['numero_materia'])) + except ObjectDoesNotExist: + return self.render_to_response( + {'form': form, + 'error': 'Matéria adicionada não existe!'}) + else: + proposicao.autor = materia.autoria_set.first().autor + proposicao.materia = materia + + proposicao.descricao = sub(' ', + ' ', + strip_tags(form.data['descricao'])) + # proposicao.data_envio = datetime.now() + proposicao.save() return self.form_valid(form) else: - self.render_to_response({'form': form}) + return self.render_to_response({'form': form}) + + +class ProposicaoListView(ListView): + template_name = "materia/proposicao_list.html" + paginate_by = 10 + model = Proposicao + + def get_queryset(self): + return Proposicao.objects.all().order_by('-data_envio') + + def get_context_data(self, **kwargs): + context = super(ProposicaoListView, self).get_context_data(**kwargs) + + paginator = context['paginator'] + page_obj = context['page_obj'] + + context['page_range'] = sapl.crud.make_pagination( + page_obj.number, paginator.num_pages) + return context diff --git a/templates/base.html b/templates/base.html index 783532f9a..17c56aa95 100644 --- a/templates/base.html +++ b/templates/base.html @@ -77,7 +77,7 @@