From 45adbb9fbe7d8be395ac55195095ecd41a52ec75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Wed, 14 Jun 2023 17:22:04 -0300 Subject: [PATCH] =?UTF-8?q?Generaliza=20filtros=20por=20conv=C3=AAnio/excl?= =?UTF-8?q?uir=20conv=C3=AAnio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/casas/admin.py | 10 ++-- sigi/apps/casas/filters.py | 39 ------------- sigi/apps/convenios/filters.py | 103 +++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 43 deletions(-) create mode 100644 sigi/apps/convenios/filters.py diff --git a/sigi/apps/casas/admin.py b/sigi/apps/casas/admin.py index 2bc03cd..c58181a 100644 --- a/sigi/apps/casas/admin.py +++ b/sigi/apps/casas/admin.py @@ -11,10 +11,12 @@ from sigi.apps.casas.forms import OrgaoForm from sigi.apps.casas.models import Orgao, Funcionario, TipoOrgao from sigi.apps.casas.filters import ( GerentesInterlegisFilter, - ConvenioFilter, - ExcluirConvenioFilter, ServicoFilter, ) +from sigi.apps.convenios.filters import ( + TipoProjetoFilter, + ExcluirTipoProjetoFilter, +) from sigi.apps.contatos.models import Telefone from sigi.apps.convenios.models import Convenio from sigi.apps.ocorrencias.models import Ocorrencia @@ -348,9 +350,9 @@ class OrgaoAdmin(CartExportReportMixin, admin.ModelAdmin): "tipo", ("gerentes_interlegis", GerentesInterlegisFilter), "municipio__uf__nome", - ConvenioFilter, + ("convenio__projeto_id", TipoProjetoFilter), ("servico__data_desativacao", ServicoAtivoFilter), - ExcluirConvenioFilter, + ("convenio__projeto_id", ExcluirTipoProjetoFilter), ServicoFilter, "inclusao_digital", ("email", EmptyFilter), diff --git a/sigi/apps/casas/filters.py b/sigi/apps/casas/filters.py index 0e96be8..d92c24e 100644 --- a/sigi/apps/casas/filters.py +++ b/sigi/apps/casas/filters.py @@ -14,45 +14,6 @@ class GerentesInterlegisFilter(admin.filters.RelatedFieldListFilter): self.lookup_choices = [(x.id, x) for x in gerentes] -class ConvenioFilter(admin.SimpleListFilter): - title = _("Tipo de convênio") - parameter_name = "convenio" - - def lookups(self, request, model_admin): - return ( - ("SC", _("Sem nenhum convênio")), - ("CC", _("Com algum convênio")), - ) + tuple([(p.pk, p.sigla) for p in Projeto.objects.all()]) - - def queryset(self, request, queryset): - if self.value() is not None: - if self.value() == "SC": - queryset = queryset.filter(convenio=None) - elif self.value() == "CC": - queryset = queryset.exclude(convenio=None) - else: - queryset = queryset.filter(convenio__projeto_id=self.value()) - - return queryset.distinct("municipio__uf__nome", "nome") - - -class ExcluirConvenioFilter(admin.SimpleListFilter): - title = _("Excluir convênio da pesquisa") - parameter_name = "excluir_convenio" - - def lookups(self, request, model_admin): - return tuple([(p.pk, p.sigla) for p in Projeto.objects.all()]) - - def queryset(self, request, queryset): - if self.value() is None: - return queryset - else: - queryset = queryset.exclude( - convenio__projeto_id=self.value() - ).distinct("municipio__uf__nome", "nome") - return queryset - - class ServicoFilter(admin.SimpleListFilter): title = _("Serviço") parameter_name = "servico" diff --git a/sigi/apps/convenios/filters.py b/sigi/apps/convenios/filters.py new file mode 100644 index 0000000..494ce3a --- /dev/null +++ b/sigi/apps/convenios/filters.py @@ -0,0 +1,103 @@ +from django.contrib import admin +from django.utils.translation import gettext as _ +from sigi.apps.convenios.models import Projeto + + +class TipoProjetoFilter(admin.FieldListFilter): + parameter_name = "convenio" + + def __init__(self, field, request, params, model, model_admin, field_path): + super().__init__(field, request, params, model, model_admin, field_path) + + self.lookup_choices = self.lookups(request, model_admin) + if self.lookup_choices is None: + self.lookup_choices = () + + def lookups(self, request, model_admin): + return ( + ("SC", _("Sem nenhum convênio")), + ("CC", _("Com algum convênio")), + ) + tuple([(p.pk, p.sigla) for p in Projeto.objects.all()]) + + def value(self): + return self.used_parameters.get(self.parameter_name) + + def expected_parameters(self): + return [ + self.parameter_name, + ] + + def choices(self, changelist): + yield { + "selected": self.value() is None, + "query_string": changelist.get_query_string( + remove=[self.parameter_name] + ), + "display": _("All"), + } + for lookup, title in self.lookup_choices: + yield { + "selected": str(self.value()) == str(lookup), + "query_string": changelist.get_query_string( + {self.parameter_name: lookup} + ), + "display": title, + } + + def queryset(self, request, queryset): + value = self.value() + if value is not None: + if value == "SC": + queryset = queryset.filter(**{self.field_path: None}) + elif value == "CC": + queryset = queryset.exclude(**{self.field_path: None}) + else: + queryset = queryset.filter(**{self.field_path: value}) + + return queryset + + +class ExcluirTipoProjetoFilter(admin.FieldListFilter): + parameter_name = "excluir_convenio" + + def __init__(self, field, request, params, model, model_admin, field_path): + super().__init__(field, request, params, model, model_admin, field_path) + self.title = _(f"Excluir {self.title}") + + self.lookup_choices = self.lookups(request, model_admin) + if self.lookup_choices is None: + self.lookup_choices = () + + def lookups(self, request, model_admin): + return tuple([(p.pk, p.sigla) for p in Projeto.objects.all()]) + + def value(self): + return self.used_parameters.get(self.parameter_name) + + def expected_parameters(self): + return [ + self.parameter_name, + ] + + def choices(self, changelist): + yield { + "selected": self.value() is None, + "query_string": changelist.get_query_string( + remove=[self.parameter_name] + ), + "display": _("None"), + } + for lookup, title in self.lookup_choices: + yield { + "selected": str(self.value()) == str(lookup), + "query_string": changelist.get_query_string( + {self.parameter_name: lookup} + ), + "display": title, + } + + def queryset(self, request, queryset): + value = self.value() + if value is not None: + queryset = queryset.exclude(**{self.field_path: value}) + return queryset