From c6b471a2a9998fb57e4e160cfa509b605be5b3c8 Mon Sep 17 00:00:00 2001 From: Eduardo Calil Date: Mon, 28 Mar 2016 20:26:28 -0300 Subject: [PATCH] =?UTF-8?q?Come=C3=A7a=20a=20implementacao=20da=20pesquisa?= =?UTF-8?q?=20por=20status?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- materia/forms.py | 18 +++++++++--------- materia/views.py | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/materia/forms.py b/materia/forms.py index b11f3a76a..219231c03 100644 --- a/materia/forms.py +++ b/materia/forms.py @@ -2,6 +2,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Button, Column, Fieldset, Layout, Submit from django import forms from django.core.exceptions import ValidationError +from django.db.models import Max from django_filters import FilterSet from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ @@ -19,7 +20,7 @@ from parlamentares.models import Parlamentar, Partido from .models import (AcompanhamentoMateria, Anexada, Autor, Autoria, DespachoInicial, DocumentoAcessorio, MateriaLegislativa, Numeracao, Origem, Proposicao, Relatoria, - StatusTramitacao, TipoAutor, TipoDocumento, + StatusTramitacao, TipoAutor, TipoMateriaLegislativa, Tramitacao, UnidadeTramitacao) ORDENACAO_MATERIAIS = [(1, 'Crescente'), @@ -788,11 +789,10 @@ class MateriaLegislativaPesquisaFields(FilterSet): 'autoria__partido', 'local_origem_externa'] - def my_custom_filter(queryset, value): - materia = MateriaLegislativa.objects.raw( - 'SELECT DISTINCT materia_id, \ - max(data_encaminhamento) as data_encaminhamento \ - FROM materia_tramitacao \ - GROUP BY materia_id \ - ORDER BY materia_id') - return queryset.all() + # def filter_tramitacao__status(status): + # ultimas_tramitacoes = Tramitacao.objects.values( + # 'materia_id').annotate(data_encaminhamento=Max( + # 'data_encaminhamento'), + # id=Max('id')) + # ultimas_tramitacoes = ultimas_tramitacoes.filter(status=status) + # return ultimas_tramitacoes.materia diff --git a/materia/views.py b/materia/views.py index c3642e184..2b643ffca 100644 --- a/materia/views.py +++ b/materia/views.py @@ -8,6 +8,7 @@ from django.contrib import messages from django.core.exceptions import ObjectDoesNotExist from django.core.mail import send_mail from django.core.urlresolvers import reverse, reverse_lazy +from django.db.models import Max from django.http.response import HttpResponseRedirect from django.shortcuts import redirect from django.template import Context, loader @@ -1302,12 +1303,23 @@ class PesquisaMateriaListView(ListView): return materias +def filter_tramitacao__status(status): + ultimas_tramitacoes = Tramitacao.objects.values( + 'materia_id').annotate(data_encaminhamento=Max( + 'data_encaminhamento'), + id=Max('id')) + import ipdb; ipdb.set_trace() + ultimas_tramitacoes = ultimas_tramitacoes.filter(status=status) + return ultimas_tramitacoes + + class MateriaLegislativaPesquisaView(FilterView): model = MateriaLegislativa filterset_class = MateriaLegislativaPesquisaFields paginate_by = 10 def get_context_data(self, **kwargs): + # import ipdb; ipdb.set_trace() context = super(MateriaLegislativaPesquisaView, self).get_context_data(**kwargs) @@ -1319,10 +1331,17 @@ class MateriaLegislativaPesquisaView(FilterView): return context def get(self, request, *args, **kwargs): - # import ipdb; ipdb.set_trace() filterset_class = self.get_filterset_class() self.filterset = self.get_filterset(filterset_class) - self.object_list = self.filterset.qs + status_tramitacao = self.filterset.data.get('tramitacao__status') + if status_tramitacao and status_tramitacao != '': + status = filter_tramitacao__status(status_tramitacao) + mat_filt_ids = [ids.get('materia_id') for ids in status] + # import ipdb; ipdb.set_trace() + self.object_list = self.filterset.qs.filter( + id__in=mat_filt_ids) + else: + self.object_list = self.filterset.qs context = self.get_context_data(filter=self.filterset, object_list=self.object_list) return self.render_to_response(context)