From f81583d7bc0d8ebdbb02f85d08a12bf2ce477de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Fri, 27 May 2022 12:13:02 -0300 Subject: [PATCH] Coloca filtros em um modal --- sigi/apps/utils/filters.py | 25 ++++++++-- .../templates/admin/date_range_filter.html | 14 +++--- sigi/static/material/admin/js/filters.js | 26 +++++++++++ sigi/templates/admin/change_list.html | 46 ++++++++++++------- 4 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 sigi/static/material/admin/js/filters.js diff --git a/sigi/apps/utils/filters.py b/sigi/apps/utils/filters.py index 7bc0a18..eea3233 100644 --- a/sigi/apps/utils/filters.py +++ b/sigi/apps/utils/filters.py @@ -251,13 +251,32 @@ class DateRangeFilter(admin.FieldListFilter): "query_string": changelist.get_query_string( remove=self.lookup_kwargs ), - "form": self.get_date_form(self.used_parameters), + "form": self.get_date_form(self.used_parameters, changelist), } ] - def get_date_form(self, context={}): + def get_date_form(self, context={}, changelist=None): date_fields = { - name: forms.DateField(required=False) for name in self.lookup_kwargs + name: forms.DateField( + required=False, + label=(_("De") if "__gte" in name else _("Até")), + widget=forms.DateInput( + attrs={ + "class": "datepicker admin_filter", + "placeholder": ( + _("De") if "__gte" in name else _("Até") + ), + "data-clear": changelist.get_query_string( + remove=[ + name, + ] + ) + if changelist + else "", + } + ), + ) + for name in self.lookup_kwargs } DateForm = type("DateForm", (forms.Form,), date_fields) diff --git a/sigi/apps/utils/templates/admin/date_range_filter.html b/sigi/apps/utils/templates/admin/date_range_filter.html index c3ee1ec..9afb7b2 100644 --- a/sigi/apps/utils/templates/admin/date_range_filter.html +++ b/sigi/apps/utils/templates/admin/date_range_filter.html @@ -1,13 +1,11 @@ {% load i18n %} {% with choice=choices.0 %}
-
- - - {% trans 'All' %} - {% for field in choice.form %} - - {% endfor %} -
+ +

{% trans 'All' %}

+ {% for field in choice.form %} + {{ field }} + + {% endfor %}
{% endwith %} \ No newline at end of file diff --git a/sigi/static/material/admin/js/filters.js b/sigi/static/material/admin/js/filters.js new file mode 100644 index 0000000..1ffe579 --- /dev/null +++ b/sigi/static/material/admin/js/filters.js @@ -0,0 +1,26 @@ +$("#apply-filters").on("click", function () { + var query = []; + var retorno = decodeURIComponent(window.location.search).replace("?", "").split("&"); + var remove = []; + var filters = $(".admin_filter"); + for (i = 0; i < filters.length; i++) { + var element = $(filters[i]); + var filter; + if (element.attr("type") == "text") { + var cleaner = decodeURIComponent(element.attr("data-clear")).replace("?", "").split("&"); + if (element.val() == "") { + filter = [] + } else { + filter = [element.attr("name") + '=' + decodeURIComponent(element.val())]; + } + filter = filter.concat(cleaner); + } else { + filter = decodeURIComponent(element.val()).replace("?", "").split("&"); + } + remove = remove.concat(retorno.filter(x => !filter.includes(x))); + query = query.concat(filter.filter(x => !retorno.includes(x))); + } + 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 0acd048..667a7c4 100644 --- a/sigi/templates/admin/change_list.html +++ b/sigi/templates/admin/change_list.html @@ -30,7 +30,7 @@