From 26600d37bbb04b6bf067d02ffc8834f1e7efcc42 Mon Sep 17 00:00:00 2001 From: miguelzinh3 Date: Tue, 19 Sep 2023 17:41:43 -0300 Subject: [PATCH] feat(eleicao): add city filter --- app/eleicao/cms_plugins.py | 4 ++++ app/eleicao/forms/filters.py | 17 +++++++++++++++-- app/eleicao/static/js/widgets/cep-fields.js | 6 +++--- .../eleicao/filters/candidate_list_filter.html | 11 +++++++++-- .../eleicao/plugins/candidate_list.html | 14 +++++++++++++- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app/eleicao/cms_plugins.py b/app/eleicao/cms_plugins.py index 6528b4844..4a33dbbe1 100644 --- a/app/eleicao/cms_plugins.py +++ b/app/eleicao/cms_plugins.py @@ -61,9 +61,13 @@ def render(self, context, instance, placeholder): # Filtered List qs = Candidate.objects.filter(status=CandidateStatusChoices.published) filter_state = request.GET.get("uf", None) + filter_city = request.GET.get("city", None) if filter_state: ctx["filter_state"] = filter_state qs = qs.filter(place__state__iexact=filter_state) + if filter_city: + ctx["filter_city"] = filter_city + qs = qs.filter(place__city__iexact=filter_city) page_number = request.GET.get("page", 1) p = Paginator(qs, self.per_page) diff --git a/app/eleicao/forms/filters.py b/app/eleicao/forms/filters.py index 589f3bba8..e1430ee77 100644 --- a/app/eleicao/forms/filters.py +++ b/app/eleicao/forms/filters.py @@ -3,6 +3,8 @@ from django.utils.safestring import mark_safe from django.utils.functional import lazy +from django.urls import reverse_lazy + # from django_select2 import forms as s2forms from ..csv.choices import get_states @@ -14,8 +16,19 @@ class CandidateListFilter(forms.Form): choices=lazy(get_states, list)(), widget=forms.Select( attrs={ - "class": "w-full max-w-xs select select-bordered rounded-lg bg-[#EFEFEF] border-[#E0E0E0]", - "onchange": "this.form.submit()" + # "class": "w-full max-w-xs select select-bordered rounded-lg bg-[#EFEFEF] border-[#E0E0E0]", + "data-cep-fields": "state", + "data-cep-url": reverse_lazy("eleicao:cep"), + } + ) + ) + + city = forms.CharField( + label="Cidade", + widget=forms.Select( + attrs={ + "data-cep-fields": "city", + "data-cep-url": reverse_lazy("eleicao:cep"), } ) ) diff --git a/app/eleicao/static/js/widgets/cep-fields.js b/app/eleicao/static/js/widgets/cep-fields.js index 82b6b4eb8..e1afe7d1c 100644 --- a/app/eleicao/static/js/widgets/cep-fields.js +++ b/app/eleicao/static/js/widgets/cep-fields.js @@ -7,9 +7,9 @@ const $placeField = $('[data-cep-fields="place"]'); if ($stateField.length) { - $stateField.select2({ placeholder: 'Selecione seu estado' }); - $cityField.select2({ placeholder: 'Selecione sua cidade' }); - $placeField.select2({ placeholder: 'Selecione o CT' }); + $stateField.select2({ dropdownAutoWidth: true, width: 'auto', placeholder: 'Selecione seu estado' }); + $cityField.select2({ dropdownAutoWidth: true, width: 'auto', placeholder: 'Selecione sua cidade' }); + $placeField.select2({ dropdownAutoWidth: true, width: 'auto', placeholder: 'Selecione o CT' }); var uf; $stateField.on("change", (evt) => { diff --git a/app/eleicao/templates/eleicao/filters/candidate_list_filter.html b/app/eleicao/templates/eleicao/filters/candidate_list_filter.html index ee2689f69..28398b55f 100644 --- a/app/eleicao/templates/eleicao/filters/candidate_list_filter.html +++ b/app/eleicao/templates/eleicao/filters/candidate_list_filter.html @@ -1,8 +1,15 @@ -
-
+ +
{{ form.uf }}
+
+ + {{ form.city }} +
+ diff --git a/app/eleicao/templates/eleicao/plugins/candidate_list.html b/app/eleicao/templates/eleicao/plugins/candidate_list.html index 222846d5b..36f384079 100644 --- a/app/eleicao/templates/eleicao/plugins/candidate_list.html +++ b/app/eleicao/templates/eleicao/plugins/candidate_list.html @@ -1,3 +1,9 @@ +{% load sekizai_tags static %} + +{% addtoblock "css" %} + +{% endaddtoblock %} +
@@ -43,4 +49,10 @@

Conheça as candidaturas

Não encontrou as candidaturas do seu bairro? Seja voluntário(a) e fortaleça a campanha no seu território.

-
\ No newline at end of file + + +{% addtoblock "js" %} + + + +{% endaddtoblock %} \ No newline at end of file