Browse Source

Generaliza filtros por convênio/excluir convênio

pull/165/head
Sesostris Vieira 2 years ago
parent
commit
45adbb9fbe
  1. 10
      sigi/apps/casas/admin.py
  2. 39
      sigi/apps/casas/filters.py
  3. 103
      sigi/apps/convenios/filters.py

10
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),

39
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"

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