diff --git a/requirements/requirements.txt b/requirements/requirements.txt index d95a4ec..3d2dba5 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -15,6 +15,7 @@ uritemplate==4.1.1 weasyprint==58.0 XlsxWriter==3.1.2 Django==4.2.7 +django-admin-autocomplete-filter==0.7.1 django-auth-ldap==4.3.0 django-debug-toolbar==4.1.0 django-environ==0.10.0 diff --git a/sigi/apps/casas/admin.py b/sigi/apps/casas/admin.py index 60afc82..7c00019 100644 --- a/sigi/apps/casas/admin.py +++ b/sigi/apps/casas/admin.py @@ -24,7 +24,7 @@ from sigi.apps.parlamentares.models import Parlamentar 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.mixins import AsciifyQParameter from sigi.apps.utils.mixins import ( ReturnMixin, CartExportReportMixin, @@ -329,7 +329,7 @@ class FuncionarioAdmin(ReturnMixin, admin.ModelAdmin): @admin.register(Orgao) -class OrgaoAdmin(CartExportReportMixin, admin.ModelAdmin): +class OrgaoAdmin(AsciifyQParameter, CartExportReportMixin, admin.ModelAdmin): form = OrgaoForm resource_classes = [OrgaoExportResourseGeral, OrgaoExportResourceContato] inlines = ( @@ -446,7 +446,6 @@ class OrgaoAdmin(CartExportReportMixin, admin.ModelAdmin): ] def get_queryset(self, request): - asciify_q_param(request) queryset = super().get_queryset(request) return queryset.prefetch_related("gerentes_interlegis", "convenio_set") diff --git a/sigi/apps/contatos/admin.py b/sigi/apps/contatos/admin.py index 2710e8b..ad8d268 100644 --- a/sigi/apps/contatos/admin.py +++ b/sigi/apps/contatos/admin.py @@ -1,6 +1,6 @@ +from admin_auto_filters.filters import AutocompleteFilter from django.contrib import admin from django.utils.translation import gettext as _ - from sigi.apps.utils.filters import RangeFilter from sigi.apps.contatos.models import ( UnidadeFederativa, @@ -11,7 +11,7 @@ from sigi.apps.contatos.models import ( Contato, ) from sigi.apps.parlamentares.models import Senador -from sigi.apps.utils import queryset_ascii +from sigi.apps.utils.mixins import AsciifyQParameter from sigi.apps.utils.mixins import ( ReturnMixin, CartExportMixin, @@ -19,6 +19,11 @@ from sigi.apps.utils.mixins import ( ) +class MicrorregiaoFilter(AutocompleteFilter): + title = _("Microrregião") + field_name = "microrregiao" + + class UnidadeFederativaResource(LabeledResourse): class Meta: model = UnidadeFederativa @@ -75,7 +80,9 @@ class SenadorInline(admin.StackedInline): @admin.register(UnidadeFederativa) -class UnidadeFederativaAdmin(CartExportMixin, admin.ModelAdmin): +class UnidadeFederativaAdmin( + AsciifyQParameter, CartExportMixin, admin.ModelAdmin +): actions = None resource_classes = [UnidadeFederativaResource] list_display = ("codigo_ibge", "nome", "sigla", "regiao", "populacao") @@ -85,12 +92,11 @@ class UnidadeFederativaAdmin(CartExportMixin, admin.ModelAdmin): ("populacao", RangeFilter), ) search_fields = ("search_text", "codigo_ibge", "sigla", "regiao") - get_queryset = queryset_ascii inlines = (SenadorInline, MesorregiaoInline) @admin.register(Mesorregiao) -class MesorregiaoAdmin(admin.ModelAdmin): +class MesorregiaoAdmin(AsciifyQParameter, admin.ModelAdmin): actions = None list_display = ("codigo_ibge", "uf", "nome") list_display_links = ("codigo_ibge", "nome") @@ -102,17 +108,16 @@ class MesorregiaoAdmin(admin.ModelAdmin): "uf__sigla", ) autocomplete_fields = ("uf",) - get_queryset = queryset_ascii inlines = (MicrorregiaoInline,) @admin.register(Microrregiao) -class MicrorregiaoAdmin(admin.ModelAdmin): +class MicrorregiaoAdmin(AsciifyQParameter, admin.ModelAdmin): search_fields = ("search_text",) @admin.register(Municipio) -class MunicipioAdmin(CartExportMixin, admin.ModelAdmin): +class MunicipioAdmin(AsciifyQParameter, CartExportMixin, admin.ModelAdmin): actions = None resource_classes = [MunicipioResource] list_display = ( @@ -120,6 +125,8 @@ class MunicipioAdmin(CartExportMixin, admin.ModelAdmin): "codigo_tse", "nome", "uf", + "get_regiao", + "microrregiao", "is_capital", "populacao", "is_polo", @@ -136,8 +143,8 @@ class MunicipioAdmin(CartExportMixin, admin.ModelAdmin): ("populacao", RangeFilter), "uf__regiao", "uf", + MicrorregiaoFilter, ) - get_queryset = queryset_ascii fieldsets = ( ( None, @@ -169,6 +176,10 @@ class MunicipioAdmin(CartExportMixin, admin.ModelAdmin): autocomplete_fields = ("uf", "microrregiao") search_fields = ("search_text", "codigo_ibge", "codigo_tse", "uf__sigla") + @admin.display(description=_("Região"), ordering="uf__regiao") + def get_regiao(self, obj): + return obj.uf.get_regiao_display() + @admin.register(Telefone) class TelefoneAdmin(admin.ModelAdmin): diff --git a/sigi/apps/convenios/admin.py b/sigi/apps/convenios/admin.py index b186222..693d980 100644 --- a/sigi/apps/convenios/admin.py +++ b/sigi/apps/convenios/admin.py @@ -17,7 +17,7 @@ from sigi.apps.convenios.models import ( Tramitacao, Gescon, ) -from sigi.apps.utils import queryset_ascii, asciify_q_param +from sigi.apps.utils.mixins import AsciifyQParameter from sigi.apps.servidores.models import Servidor from sigi.apps.casas.admin import ConveniosInline, GerentesInterlegisFilter from sigi.apps.utils.mixins import ( @@ -114,7 +114,9 @@ class ProjetoAdmin(admin.ModelAdmin): @admin.register(Convenio) -class ConvenioAdmin(ReturnMixin, CartExportReportMixin, admin.ModelAdmin): +class ConvenioAdmin( + AsciifyQParameter, ReturnMixin, CartExportReportMixin, admin.ModelAdmin +): fieldsets = ( ( None, @@ -286,10 +288,6 @@ class ConvenioAdmin(ReturnMixin, CartExportReportMixin, admin.ModelAdmin): del actions["delete_selected"] return actions - def get_queryset(self, request): - asciify_q_param(request) - return super().get_queryset(request) - @admin.register(EquipamentoPrevisto) class EquipamentoPrevistoAdmin(admin.ModelAdmin): diff --git a/sigi/apps/eventos/admin.py b/sigi/apps/eventos/admin.py index f99e1aa..cf0bc08 100644 --- a/sigi/apps/eventos/admin.py +++ b/sigi/apps/eventos/admin.py @@ -57,6 +57,7 @@ from sigi.apps.servidores.models import Servidor from sigi.apps.utils import abreviatura from sigi.apps.utils.filters import DateRangeFilter from sigi.apps.utils.mixins import ( + AsciifyQParameter, CartExportMixin, CartExportReportMixin, LabeledResourse, @@ -381,7 +382,7 @@ class TipoEventoAdmin(admin.ModelAdmin): @admin.register(Solicitacao) -class SolicitacaoAdmin(CartExportMixin, admin.ModelAdmin): +class SolicitacaoAdmin(AsciifyQParameter, CartExportMixin, admin.ModelAdmin): resource_class = SolicitacaoResource list_display = ( "casa", @@ -851,7 +852,7 @@ class ModeloDeclaracaoAdmin(admin.ModelAdmin): @admin.register(Evento) -class EventoAdmin(CartExportReportMixin, admin.ModelAdmin): +class EventoAdmin(AsciifyQParameter, CartExportReportMixin, admin.ModelAdmin): form = EventoAdminForm resource_class = EventoResource fieldsets = ( diff --git a/sigi/apps/utils/__init__.py b/sigi/apps/utils/__init__.py index 6b89a69..c340708 100644 --- a/sigi/apps/utils/__init__.py +++ b/sigi/apps/utils/__init__.py @@ -39,18 +39,6 @@ def to_ascii(txt, codif="utf-8"): ) -def asciify_q_param(request): - if "q" in request.GET: - request.GET._mutable = True - request.GET["q"] = to_ascii(request.GET["q"]) - request.GET._mutable = False - - -def queryset_ascii(self, request): - asciify_q_param(request) - return django_admin.ModelAdmin.get_queryset(self, request) - - def field_label(name, model): name = name.split("__") try: diff --git a/sigi/apps/utils/mixins.py b/sigi/apps/utils/mixins.py index 33dccb9..5d4b999 100644 --- a/sigi/apps/utils/mixins.py +++ b/sigi/apps/utils/mixins.py @@ -16,7 +16,7 @@ from import_export.admin import ImportMixin, ExportMixin from import_export.fields import Field from import_export.forms import ExportForm from import_export.signals import post_export -from sigi.apps.utils import field_label +from sigi.apps.utils import field_label, to_ascii class ValueField(Field): @@ -100,7 +100,7 @@ class CartExportMixin(ExportMixin): qs = super().get_queryset(request) if self._cart_viewing_name in request.session: ids = request.session.get(self._cart_session_name, []) - qs = qs.filter(id__in=ids) + qs = qs.filter(pk__in=ids) return qs def get_actions(self, request): @@ -307,3 +307,14 @@ class ReturnMixin: if self._return_path: return HttpResponseRedirect(self._return_path) return response + +class AsciifyQParameter: + def asciify_q_param(self, request): + if "q" in request.GET: + request.GET._mutable = True + request.GET["q"] = to_ascii(request.GET["q"]) + request.GET._mutable = False + + def get_queryset(self, request): + self.asciify_q_param(request) + return super().get_queryset(request) diff --git a/sigi/settings.py b/sigi/settings.py index 0dfdf97..130a4ab 100644 --- a/sigi/settings.py +++ b/sigi/settings.py @@ -60,6 +60,7 @@ INSTALLED_APPS = [ "rest_framework", "material", "material.admin", + "admin_auto_filters", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", diff --git a/sigi/static/material/admin/js/filters.js b/sigi/static/material/admin/js/filters.js index 1ffe579..f4971a5 100644 --- a/sigi/static/material/admin/js/filters.js +++ b/sigi/static/material/admin/js/filters.js @@ -3,6 +3,7 @@ $("#apply-filters").on("click", function () { var retorno = decodeURIComponent(window.location.search).replace("?", "").split("&"); var remove = []; var filters = $(".admin_filter"); + var dal_filters = $("[id$=dal-filter]") for (i = 0; i < filters.length; i++) { var element = $(filters[i]); var filter; @@ -20,6 +21,17 @@ $("#apply-filters").on("click", function () { remove = remove.concat(retorno.filter(x => !filter.includes(x))); query = query.concat(filter.filter(x => !retorno.includes(x))); } + + for (i = 0; i < dal_filters.length; i++) { + var element = $(dal_filters[i]); + var filter = []; + if (element.val() != "") { + filter = [element.serialize()]; + } + remove = remove.concat(retorno.filter(x => x.includes(element.attr("name")))); + query = query.concat(filter); + } + query = query.concat(retorno.filter(x => !remove.includes(x))); query = query.filter(x => x != ""); window.location.href = "?" + (query.join("&")); diff --git a/sigi/templates/admin/change_list.html b/sigi/templates/admin/change_list.html index 667a7c4..ac326c6 100644 --- a/sigi/templates/admin/change_list.html +++ b/sigi/templates/admin/change_list.html @@ -100,15 +100,7 @@ $(document).ready(function(){ $('.fixed-action-btn').floatingActionButton(); M.Tooltip.init($('.tooltipped'), {}); - document.addEventListener('DOMContentLoaded', function() { - M.Modal.init($(".modal"), {}); - }); - - // Or with jQuery - - $(document).ready(function(){ - $('.modal').modal(); - }); + M.Modal.init($(".modal"), {dismissible: false}); }); {% endblock footer %} diff --git a/sigi/templates/django-admin-autocomplete-filter/autocomplete-filter.html b/sigi/templates/django-admin-autocomplete-filter/autocomplete-filter.html new file mode 100644 index 0000000..5816205 --- /dev/null +++ b/sigi/templates/django-admin-autocomplete-filter/autocomplete-filter.html @@ -0,0 +1,9 @@ +{% load i18n %} + +