Browse Source

Reuso de código do django-filter

pull/1379/head
Eduardo Calil 7 years ago
parent
commit
41e3bc8e8d
  1. 7
      sapl/base/forms.py
  2. 42
      sapl/materia/forms.py
  3. 41
      sapl/utils.py

7
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):

42
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():

41
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
Loading…
Cancel
Save