diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index 7d791207a..f21121c32 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -12,7 +12,7 @@ from django.utils.translation import ugettext_lazy as _ from sapl.crispy_layout_mixin import form_actions, to_row from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa from sapl.settings import MAX_DOC_UPLOAD_SIZE -from sapl.utils import RANGE_ANOS, RangeWidgetOverride +from sapl.utils import NormaPesquisaOrderingFilter, RANGE_ANOS, RangeWidgetOverride from .models import (AnexoNormaJuridica, AssuntoNorma, NormaJuridica, NormaRelacionada, TipoNormaJuridica) @@ -55,6 +55,8 @@ class NormaFilterSet(django_filters.FilterSet): assuntos = django_filters.ModelChoiceFilter( queryset=AssuntoNorma.objects.all()) + o = NormaPesquisaOrderingFilter() + class Meta: model = NormaJuridica fields = ['tipo', 'numero', 'ano', 'data', @@ -65,7 +67,7 @@ class NormaFilterSet(django_filters.FilterSet): row1 = to_row([('tipo', 4), ('numero', 4), ('ano', 4)]) row2 = to_row([('data', 6), ('data_publicacao', 6)]) - row3 = to_row([('ementa', 8), ('assuntos', 4)]) + row3 = to_row([('ementa', 6), ('assuntos', 6), ('o',6)]) self.form.helper = FormHelper() self.form.helper.form_method = 'GET' diff --git a/sapl/norma/views.py b/sapl/norma/views.py index a05d61289..c4a171da2 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -86,6 +86,12 @@ class NormaPesquisaView(FilterView): context['title'] = _('Pesquisar Norma Jurídica') + self.filterset.form.fields['o'].label = _('Ordenação') + + qs = self.object_list + if 'o' in self.request.GET and not self.request.GET['o']: + qs = qs.order_by('-ano', 'tipo', '-numero') + qr = self.request.GET.copy() if 'page' in qr: diff --git a/sapl/utils.py b/sapl/utils.py index 5c461f913..691034789 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -437,6 +437,32 @@ class MateriaPesquisaOrderingFilter(django_filters.OrderingFilter): return super().filter(qs, _value) +class NormaPesquisaOrderingFilter(django_filters.OrderingFilter): + + choices = ( + ('', 'Selecione'), + ('dataC', 'Data, Tipo, Ano, Numero - Ordem Crescente'), + ('dataD', 'Data, Tipo, Ano, Numero - Ordem Decrescente'), + ('tipoC', 'Tipo, Ano, Numero, Data - Ordem Crescente'), + ('tipoD', 'Tipo, Ano, Numero, Data - Ordem Decrescente') + ) + order_by_mapping = { + '': [], + 'dataC': ['data', 'tipo', 'ano', 'numero'], + 'dataD': ['-data', '-tipo', '-ano', '-numero'], + 'tipoC': ['tipo', 'ano', 'numero', 'data'], + 'tipoD': ['-tipo', '-ano', '-numero', '-data'], + } + + def __init__(self, *args, **kwargs): + kwargs['choices'] = self.choices + super(NormaPesquisaOrderingFilter, self).__init__(*args, **kwargs) + + def filter(self, qs, value): + _value = self.order_by_mapping[value[0]] if value else value + return super().filter(qs, _value) + + class AnoNumeroOrderingFilter(django_filters.OrderingFilter): choices = (('DEC', 'Ordem Decrescente'),