From 41e3bc8e8dc0ac67ee770081ce055a017b7e3d54 Mon Sep 17 00:00:00 2001 From: Eduardo Calil Date: Wed, 9 Aug 2017 20:08:28 -0300 Subject: [PATCH] =?UTF-8?q?Reuso=20de=20c=C3=B3digo=20do=20django-filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/base/forms.py | 7 ++++++- sapl/materia/forms.py | 42 +++--------------------------------------- sapl/utils.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/sapl/base/forms.py b/sapl/base/forms.py index 8ecd74d5a..f95cc9bb8 100644 --- a/sapl/base/forms.py +++ b/sapl/base/forms.py @@ -24,7 +24,8 @@ from sapl.sessao.models import SessaoPlenaria from sapl.settings import MAX_IMAGE_UPLOAD_SIZE from sapl.utils import (RANGE_ANOS, ChoiceWithoutValidationField, ImageThumbnailFileInput, RangeWidgetOverride, - autor_label, autor_modal, models_with_gr_for_model) + autor_label, autor_modal, models_with_gr_for_model, + qs_override_django_filter) from .models import AppConfig, CasaLegislativa @@ -501,6 +502,10 @@ class RelatorioPresencaSessaoFilterSet(django_filters.FilterSet): row1, form_actions(save_label='Pesquisar')) ) + @property + def qs(self): + return qs_override_django_filter(self) + class RelatorioHistoricoTramitacaoFilterSet(django_filters.FilterSet): diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index 812250309..d56345179 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -17,12 +17,10 @@ from django.forms import ModelForm, ModelChoiceField, widgets from django.forms.forms import Form from django.forms.models import ModelMultipleChoiceField from django.forms.widgets import Select, CheckboxSelectMultiple, HiddenInput -from django.utils import six from django.utils.encoding import force_text from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ -from django_filters.filterset import STRICTNESS import django_filters from sapl.base.models import Autor, TipoAutor @@ -41,7 +39,8 @@ from sapl.settings import MAX_DOC_UPLOAD_SIZE from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, ChoiceWithoutValidationField, MateriaPesquisaOrderingFilter, RangeWidgetOverride, - autor_label, autor_modal, models_with_gr_for_model) + autor_label, autor_modal, models_with_gr_for_model, + qs_override_django_filter) import sapl from .models import (AcompanhamentoMateria, Anexada, Autoria, DespachoInicial, @@ -591,42 +590,7 @@ class MateriaLegislativaFilterSet(django_filters.FilterSet): @property def qs(self): - if not hasattr(self, '_qs'): - valid = self.is_bound and self.form.is_valid() - - if self.is_bound and not valid: - if self.strict == STRICTNESS.RAISE_VALIDATION_ERROR: - raise forms.ValidationError(self.form.errors) - elif bool(self.strict) == STRICTNESS.RETURN_NO_RESULTS: - self._qs = self.queryset.none() - return self._qs - # else STRICTNESS.IGNORE... ignoring - - # start with all the results and filter from there - qs = self.queryset.all() - for name, filter_ in six.iteritems(self.filters): - value = None - if valid: - value = self.form.cleaned_data[name] - else: - raw_value = self.form[name].value() - try: - value = self.form.fields[name].clean(raw_value) - except forms.ValidationError: - if self.strict == STRICTNESS.RAISE_VALIDATION_ERROR: - raise - elif bool(self.strict) == STRICTNESS.RETURN_NO_RESULTS: - self._qs = self.queryset.none() - return self._qs - # else STRICTNESS.IGNORE... ignoring - - if value is not None: # valid & clean data - qs = qs._next_is_sticky() - qs = filter_.filter(qs, value) - - self._qs = qs - - return self._qs + return qs_override_django_filter(self) def pega_ultima_tramitacao(): diff --git a/sapl/utils.py b/sapl/utils.py index 7a4146fc5..5336e4c1c 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -3,6 +3,7 @@ import logging import os import re from datetime import date +from django_filters.filterset import STRICTNESS from functools import wraps from subprocess import PIPE, call from threading import Thread @@ -19,6 +20,7 @@ from django.contrib import admin from django.contrib.contenttypes.fields import (GenericForeignKey, GenericRel, GenericRelation) from django.core.exceptions import ValidationError +from django.utils import six from django.utils.translation import ugettext_lazy as _ from floppyforms import ClearableFileInput from reversion.admin import VersionAdmin @@ -553,3 +555,42 @@ def texto_upload_path(instance, filename, subpath='', pk_first=False): } return path + + +def qs_override_django_filter(self): + if not hasattr(self, '_qs'): + valid = self.is_bound and self.form.is_valid() + + if self.is_bound and not valid: + if self.strict == STRICTNESS.RAISE_VALIDATION_ERROR: + raise forms.ValidationError(self.form.errors) + elif bool(self.strict) == STRICTNESS.RETURN_NO_RESULTS: + self._qs = self.queryset.none() + return self._qs + # else STRICTNESS.IGNORE... ignoring + + # start with all the results and filter from there + qs = self.queryset.all() + for name, filter_ in six.iteritems(self.filters): + value = None + if valid: + value = self.form.cleaned_data[name] + else: + raw_value = self.form[name].value() + try: + value = self.form.fields[name].clean(raw_value) + except forms.ValidationError: + if self.strict == STRICTNESS.RAISE_VALIDATION_ERROR: + raise + elif bool(self.strict) == STRICTNESS.RETURN_NO_RESULTS: + self._qs = self.queryset.none() + return self._qs + # else STRICTNESS.IGNORE... ignoring + + if value is not None: # valid & clean data + qs = qs._next_is_sticky() + qs = filter_.filter(qs, value) + + self._qs = qs + + return self._qs \ No newline at end of file