diff --git a/sapl/base/forms.py b/sapl/base/forms.py index 5aa3315fb..5b246a47f 100644 --- a/sapl/base/forms.py +++ b/sapl/base/forms.py @@ -32,7 +32,8 @@ from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, ChoiceWithoutValidationField, ImageThumbnailFileInput, RangeWidgetOverride, autor_label, autor_modal, models_with_gr_for_model, qs_override_django_filter, - choice_anos_com_normas, choice_anos_com_materias) + choice_anos_com_normas, choice_anos_com_materias, + FilterOverridesMetaMixin) from .models import AppConfig, CasaLegislativa @@ -674,13 +675,7 @@ class AutorFormForAdmin(AutorForm): class RelatorioAtasFilterSet(django_filters.FilterSet): - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = SessaoPlenaria fields = ['data_inicio'] @@ -714,12 +709,6 @@ class RelatorioNormasMesFilterSet(django_filters.FilterSet): choices=choice_anos_com_normas) class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Ano')), - 'widget': RangeWidgetOverride} - }} model = NormaJuridica fields = ['ano'] @@ -784,13 +773,7 @@ class RelatorioNormasVigenciaFilterSet(django_filters.FilterSet): class RelatorioPresencaSessaoFilterSet(django_filters.FilterSet): - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = SessaoPlenaria fields = ['data_inicio'] @@ -822,13 +805,7 @@ class RelatorioHistoricoTramitacaoFilterSet(django_filters.FilterSet): parent = super(RelatorioHistoricoTramitacaoFilterSet, self).qs return parent.distinct().prefetch_related('tipo').order_by('-ano', 'tipo', 'numero') - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = MateriaLegislativa fields = ['tipo', 'tramitacao__unidade_tramitacao_local', 'tramitacao__status', 'tramitacao__data_tramitacao'] @@ -861,13 +838,7 @@ class RelatorioDataFimPrazoTramitacaoFilterSet(django_filters.FilterSet): parent = super(RelatorioDataFimPrazoTramitacaoFilterSet, self).qs return parent.distinct().prefetch_related('tipo').order_by('-ano', 'tipo', 'numero') - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = MateriaLegislativa fields = ['tipo', 'tramitacao__unidade_tramitacao_local', 'tramitacao__status', 'tramitacao__data_fim_prazo'] @@ -1034,13 +1005,7 @@ class RelatorioMateriasPorAutorFilterSet(django_filters.FilterSet): return parent.distinct().filter(autoria__primeiro_autor=True)\ .order_by('autoria__autor', '-autoria__primeiro_autor', 'tipo', '-ano', '-numero') - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = MateriaLegislativa fields = ['tipo', 'data_apresentacao'] diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index 06d5bada8..a043d024d 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -45,7 +45,7 @@ from sapl.utils import (YES_NO_CHOICES, SEPARADOR_HASH_PROPOSICAO, MateriaPesquisaOrderingFilter, RangeWidgetOverride, autor_label, autor_modal, gerar_hash_arquivo, models_with_gr_for_model, qs_override_django_filter, - choice_anos_com_materias) + choice_anos_com_materias, FilterOverridesMetaMixin) from .models import (AcompanhamentoMateria, Anexada, Autoria, DespachoInicial, DocumentoAcessorio, Numeracao, Proposicao, Relatoria, @@ -806,13 +806,7 @@ class MateriaLegislativaFilterSet(django_filters.FilterSet): choices=CHOICE_TIPO_LISTAGEM, label=_('Tipo da Listagem do Resultado da Pesquisa')) - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial / Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = MateriaLegislativa fields = ['numero', 'numero_protocolo', @@ -1104,13 +1098,7 @@ class AutoriaMultiCreateForm(Form): class AcessorioEmLoteFilterSet(django_filters.FilterSet): - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = MateriaLegislativa fields = ['tipo', 'data_apresentacao'] @@ -1134,13 +1122,7 @@ class AcessorioEmLoteFilterSet(django_filters.FilterSet): class PrimeiraTramitacaoEmLoteFilterSet(django_filters.FilterSet): - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = MateriaLegislativa fields = ['tipo', 'data_apresentacao'] @@ -1165,13 +1147,7 @@ class PrimeiraTramitacaoEmLoteFilterSet(django_filters.FilterSet): class TramitacaoEmLoteFilterSet(django_filters.FilterSet): - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = MateriaLegislativa fields = ['tipo', 'data_apresentacao', 'tramitacao__status', 'tramitacao__unidade_tramitacao_destino'] diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index cd5a091a1..512308e4f 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -57,13 +57,7 @@ class NormaFilterSet(django_filters.FilterSet): o = NormaPesquisaOrderingFilter(help_text='') - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = NormaJuridica fields = ['tipo', 'numero', 'ano', 'data', 'data_vigencia', 'data_publicacao', 'ementa', 'assuntos'] diff --git a/sapl/protocoloadm/forms.py b/sapl/protocoloadm/forms.py index 9a9bda40b..2a324543b 100644 --- a/sapl/protocoloadm/forms.py +++ b/sapl/protocoloadm/forms.py @@ -96,13 +96,7 @@ class ProtocoloFilterSet(django_filters.FilterSet): o = AnoNumeroOrderingFilter(help_text='') - class Meta: - filter_overrides = {models.DateTimeField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': 'Data (%s)' % (_('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = Protocolo fields = ['numero', 'tipo_documento', @@ -113,6 +107,8 @@ class ProtocoloFilterSet(django_filters.FilterSet): def __init__(self, *args, **kwargs): super(ProtocoloFilterSet, self).__init__(*args, **kwargs) + self.filters['timestamp'].label = 'Data (Inicial - Final)' + row1 = to_row( [('numero', 4), ('ano', 4), @@ -173,13 +169,7 @@ class DocumentoAdministrativoFilterSet(django_filters.FilterSet): o = AnoNumeroOrderingFilter(help_text='') - class Meta: - filter_overrides = {models.DateField: { - 'filter_class': django_filters.DateFromToRangeFilter, - 'extra': lambda f: { - 'label': 'Data (%s)' % (_('Inicial - Final')), - 'widget': RangeWidgetOverride} - }} + class Meta(FilterOverridesMetaMixin): model = DocumentoAdministrativo fields = ['tipo', 'numero', diff --git a/sapl/protocoloadm/models.py b/sapl/protocoloadm/models.py index 438037205..6d3d90671 100644 --- a/sapl/protocoloadm/models.py +++ b/sapl/protocoloadm/models.py @@ -56,10 +56,11 @@ class Protocolo(models.Model): null=False, choices=RANGE_ANOS, verbose_name=_('Ano do Protocolo')) - # TODO: Remover esses dois campos após migração, - # TODO: pois timestamp supre a necessidade + + # FIXME: https://github.com/interlegis/sapl/issues/2337 data = models.DateField(null=True, blank=True) hora = models.TimeField(null=True, blank=True) + # Não foi utilizado auto_now_add=True em timestamp porque # ele usa datetime.now que não é timezone aware. timestamp = models.DateTimeField(default=timezone.now) diff --git a/sapl/utils.py b/sapl/utils.py index ee8271b4f..def839eb1 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -16,6 +16,7 @@ from django.contrib.contenttypes.fields import (GenericForeignKey, GenericRel, GenericRelation) from django.core.exceptions import ValidationError from django.core.mail import get_connection +from django.db import models from django.db.models import Q from django.utils import six, timezone from django.utils.translation import ugettext_lazy as _ @@ -407,6 +408,21 @@ def choice_force_optional(callable): return _func +FILTER_OVERRIDES_DATEFIELD = { + 'filter_class': django_filters.DateFromToRangeFilter, + 'extra': lambda f: { + 'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')), + 'widget': RangeWidgetOverride + } +} + + +class FilterOverridesMetaMixin: + filter_overrides = { + models.DateField: FILTER_OVERRIDES_DATEFIELD + } + + TIPOS_TEXTO_PERMITIDOS = ( 'application/vnd.oasis.opendocument.text', 'application/x-vnd.oasis.opendocument.text',