From fe5a28ccdc9eaec40202927186daa73bab52e2b5 Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Mon, 22 May 2017 18:03:34 -0300 Subject: [PATCH] Add pesquisa em Autor pelo campo ativo de parlamentar --- sapl/api/forms.py | 11 ++++++----- sapl/parlamentares/models.py | 12 +++++++++++- sapl/utils.py | 24 +++++++++++++----------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/sapl/api/forms.py b/sapl/api/forms.py index c220c01b2..4609273e4 100644 --- a/sapl/api/forms.py +++ b/sapl/api/forms.py @@ -35,10 +35,11 @@ class SaplGenericRelationSearchFilterSet(FilterSet): item.related_query_name(), field[0]) ) - q_fs = q_fs | Q(**{'%s__%s%s' % ( - item.related_query_name(), - field[0], - field[1]): qtext}) + if len(field) == 3 and field[2](qtext) is not None: + q_fs = q_fs | Q(**{'%s__%s%s' % ( + item.related_query_name(), + field[0], + field[1]): qtext if len(field) == 2 else field[2](qtext)}) q = q & q_fs @@ -60,4 +61,4 @@ class AutorChoiceFilterSet(SaplGenericRelationSearchFilterSet): def filter_q(self, queryset, value): return SaplGenericRelationSearchFilterSet.filter_q( - self, queryset, value).order_by('nome') + self, queryset, value).distinct('nome').order_by('nome') diff --git a/sapl/parlamentares/models.py b/sapl/parlamentares/models.py index 4d15ab6fd..2f1284fbf 100644 --- a/sapl/parlamentares/models.py +++ b/sapl/parlamentares/models.py @@ -1,9 +1,9 @@ from datetime import datetime -import reversion from django.db import models from django.utils.translation import ugettext_lazy as _ from model_utils import Choices +import reversion from sapl.base.models import Autor from sapl.utils import (INDICADOR_AFASTAMENTO, UF, YES_NO_CHOICES, @@ -206,6 +206,15 @@ def foto_upload_path(instance, filename): return texto_upload_path(instance, filename, subpath='') +def true_false_none(x): + if x == 'True': + return True + elif x == 'False': + return False + else: + return None + + @reversion.register() class Parlamentar(models.Model): FEMININO = 'F' @@ -303,6 +312,7 @@ class Parlamentar(models.Model): ('nome_completo', '__icontains'), ('nome_parlamentar', '__icontains'), ('filiacao__partido__sigla', '__icontains'), + ('ativo', '', true_false_none), )) class Meta: diff --git a/sapl/utils.py b/sapl/utils.py index 7429a7e4c..4873d3519 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -1,15 +1,13 @@ -import hashlib -import logging -import os -import re from datetime import date from functools import wraps -from unicodedata import normalize as unicodedata_normalize from subprocess import PIPE, call from threading import Thread +from unicodedata import normalize as unicodedata_normalize +import hashlib +import logging +import os +import re -import django_filters -import magic from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Button from django import forms @@ -22,10 +20,13 @@ from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ from floppyforms import ClearableFileInput from reversion.admin import VersionAdmin +import django_filters +import magic from sapl.crispy_layout_mixin import SaplFormLayout, form_actions, to_row from sapl.settings import BASE_DIR, PROJECT_DIR + sapl_logger = logging.getLogger(BASE_DIR.name) @@ -159,8 +160,8 @@ class SaplGenericRelation(GenericRelation): assert isinstance(field, (tuple, list)), _( 'fields_search deve ser um array de tuplas ou listas.') - assert len(field) == 2, _( - 'cada tupla de fields_search deve possuir duas strins') + assert len(field) <= 3, _( + 'cada tupla de fields_search deve possuir até 3 strings') # TODO implementar assert para validar campos do Model e lookups @@ -368,8 +369,8 @@ def fabrica_validador_de_tipos_de_arquivo(lista, nome): def restringe_tipos_de_arquivo(value): if not os.path.splitext(value.path)[1][:1]: - raise ValidationError(_( - 'Não é possível fazer upload de arquivos sem extensão.')) + raise ValidationError(_( + 'Não é possível fazer upload de arquivos sem extensão.')) mime = magic.from_buffer(value.read(), mime=True) if mime not in lista: @@ -637,6 +638,7 @@ def texto_upload_path(instance, filename, subpath=''): class UpdateIndexCommand(Thread): + def run(self): call([PROJECT_DIR.child('manage.py'), 'update_index'], stdout=PIPE)