Browse Source

Substitui sigi.apps.utils.filters.EmptyFilter por django.contrib.admin.EmptyFieldListFilter

pull/167/head
Sesóstris Vieira 1 year ago
parent
commit
4de96807ac
  1. 3
      sigi/apps/casas/admin.py
  2. 5
      sigi/apps/eventos/admin.py
  3. 71
      sigi/apps/utils/filters.py

3
sigi/apps/casas/admin.py

@ -25,7 +25,6 @@ from sigi.apps.servicos.models import Servico
from sigi.apps.servicos.filters import ServicoAtivoFilter
from sigi.apps.servidores.models import Servidor
from sigi.apps.utils import asciify_q_param
from sigi.apps.utils.filters import EmptyFilter
from sigi.apps.utils.mixins import (
ReturnMixin,
CartExportReportMixin,
@ -364,7 +363,7 @@ class OrgaoAdmin(CartExportReportMixin, admin.ModelAdmin):
("convenio__projeto_id", ExcluirTipoProjetoFilter),
ServicoFilter,
"inclusao_digital",
("email", EmptyFilter),
("email", admin.EmptyFieldListFilter),
)
ordering = ("municipio__uf__nome", "nome")
fieldsets = (

5
sigi/apps/eventos/admin.py

@ -39,7 +39,7 @@ from sigi.apps.eventos.models import (
)
from sigi.apps.eventos.forms import EventoAdminForm, SelecionaModeloForm
from sigi.apps.utils import abreviatura
from sigi.apps.utils.filters import EmptyFilter, DateRangeFilter
from sigi.apps.utils.filters import DateRangeFilter
from sigi.apps.utils.mixins import (
CartExportMixin,
LabeledResourse,
@ -708,7 +708,7 @@ class EventoAdmin(CartExportMixin, admin.ModelAdmin):
list_filter = (
"status",
"publicar",
("num_processo", EmptyFilter),
("num_processo", admin.EmptyFieldListFilter),
"tipo_evento",
"tipo_evento__categoria",
"casa_anfitria__municipio__uf",
@ -716,6 +716,7 @@ class EventoAdmin(CartExportMixin, admin.ModelAdmin):
("data_inicio", DateRangeFilter),
"virtual",
"solicitante",
("moodle_courseid", admin.EmptyFieldListFilter),
)
date_hierarchy = "data_inicio"
autocomplete_fields = (

71
sigi/apps/utils/filters.py

@ -32,77 +32,6 @@ class AlphabeticFilter(admin.SimpleListFilter):
)
class EmptyFilter(admin.FieldListFilter):
EMPTY_STRING = _("Em branco")
NOT_EMPTY_STRING = _("Preenchido")
def __init__(self, field, request, params, model, model_admin, field_path):
self.model = model
self.model_admin = model_admin
self.parameter_name = f"{field_path}__empty"
if (not field.null) and (not field.blank):
raise NotEmptyableField(
f"Field {field.name} cannot be empty nor null"
)
super().__init__(
field, request, params, model, model_admin, field_path
)
def lookups(self):
return (
("1", self.EMPTY_STRING),
("0", self.NOT_EMPTY_STRING),
)
def value(self):
return self.used_parameters.get(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 value, display in self.lookups():
yield {
"selected": self.value() == value,
"query_string": changelist.get_query_string(
{self.parameter_name: value}
),
"display": display,
}
def expected_parameters(self):
return [
self.parameter_name,
]
def queryset(self, request, queryset):
val = self.value()
if val is None:
return queryset
val = bool(int(val))
filter = Q()
if self.field.null:
filter = filter | Q(**{f"{self.field_path}__isnull": val})
if self.field.blank:
if val:
filter = filter | Q(**{f"{self.field_path}__exact": ""})
else:
filter = filter | ~Q(**{f"{self.field_path}__exact": ""})
return queryset.filter(filter)
class RangeFilter(admin.FieldListFilter):
num_faixas = 4
parameter_name = None

Loading…
Cancel
Save