From cdda18f9f5c354491e7160c4330cf084c6a48cfa Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Fri, 4 Aug 2017 16:20:33 -0300 Subject: [PATCH] Impl filtro p busca de possiveis autores parlamentares --- sapl/api/forms.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/sapl/api/forms.py b/sapl/api/forms.py index 87cbcdd11..30c25f5a0 100644 --- a/sapl/api/forms.py +++ b/sapl/api/forms.py @@ -1,6 +1,7 @@ -from django.db.models import Q +from django.db.models import Q, F from django.forms.fields import CharField, MultiValueField from django.forms.widgets import MultiWidget, TextInput +from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from django_filters.filters import MethodFilter, ModelChoiceFilter, DateFilter from rest_framework import serializers @@ -8,6 +9,7 @@ from rest_framework.compat import django_filters from rest_framework.filters import FilterSet from sapl.base.models import Autor, TipoAutor +from sapl.parlamentares.models import Legislatura from sapl.utils import generic_relations_for_model @@ -169,8 +171,26 @@ class AutoresPossiveisFilterSet(FilterSet): return getattr(self, filter_for_model)(qs, data_relativa) def filter_parlamentar(self, queryset, data_relativa): - # implementar regras específicas para parlamenar - return queryset + # não leva em conta afastamentos + if not data_relativa: + data_relativa = timezone.now() + + data_relativa_na_legislatura_atual = Legislatura.objects.filter( + data_inicio__lte=timezone.now(), + data_fim__gte=timezone.now()).exists() + + params = { + 'parlamentar_set__mandato__data_inicio_mandato__lte': + data_relativa, + 'parlamentar_set__mandato__data_fim_mandato__gte': data_relativa + } + + if data_relativa_na_legislatura_atual: + params['parlamentar_set__ativo'] = True + + qs = queryset.filter(**params).distinct() + + return qs def filter_frente(self, queryset, data_relativa): # implementar regras específicas para frente