From f47829b048a7fd753e315c4ac1d61e63989d5fb3 Mon Sep 17 00:00:00 2001 From: miguelzinh3 Date: Fri, 10 Nov 2023 15:51:42 -0300 Subject: [PATCH] feat: refactor candidate list plugin --- app/eleicao/cms_apps.py | 2 +- app/eleicao/cms_plugins.py | 64 ++++++++++++++----- app/eleicao/forms/filters.py | 10 +++ app/eleicao/models.py | 5 ++ app/eleicao/places/views.py | 4 ++ app/eleicao/static/js/widgets/cep-fields.js | 14 ++++ .../filters/candidate_list_filter.html | 40 ++++++++---- .../eleicao/plugins/candidate_list.html | 6 +- 8 files changed, 110 insertions(+), 35 deletions(-) diff --git a/app/eleicao/cms_apps.py b/app/eleicao/cms_apps.py index 3d463e3a..65c4af76 100644 --- a/app/eleicao/cms_apps.py +++ b/app/eleicao/cms_apps.py @@ -7,4 +7,4 @@ class EleicaoApphook(CMSApp): name = "Eleição do ano" def get_urls(self, page=None, language=None, **kwargs): - return ["eleicao.urls"] \ No newline at end of file + return ["eleicao.urls"] diff --git a/app/eleicao/cms_plugins.py b/app/eleicao/cms_plugins.py index 2a578f18..12fc7d78 100644 --- a/app/eleicao/cms_plugins.py +++ b/app/eleicao/cms_plugins.py @@ -6,13 +6,14 @@ from .bonde_utils import create_form_entry -from .places.views import get_choices +from .places.views import get_choices, get_choices_places from .models import ( Candidate, CandidateStatusChoices, EleicaoCarousel, VoterFormPluginModel, + EleicaoCandidateList ) from .forms.filters import CandidateListFilter from .forms.create import VoterForm @@ -50,6 +51,7 @@ def render(self, context, instance, placeholder): class EleicaoCandidateListPlugin(CMSPluginBase): name = "Lista de candidaturas" module = "A Eleição do Ano" + model = EleicaoCandidateList render_template = "eleicao/plugins/candidate_list.html" per_page = 20 cache = False @@ -57,22 +59,50 @@ class EleicaoCandidateListPlugin(CMSPluginBase): def render(self, context, instance, placeholder): ctx = super().render(context, instance, placeholder) request = ctx.get("request") - - form = CandidateListFilter(request.GET) - - # 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_city == "all": - filter_city = None - if filter_state: - ctx["filter_state"] = filter_state - qs = qs.filter(place__state__iexact=filter_state) - form.fields["city"].widget.choices = [("all","Todas as cidades")] + get_choices(filter_state) - if filter_city: - ctx["filter_city"] = filter_city - qs = qs.filter(place__city__iexact=filter_city) + + if not instance.city: + form = CandidateListFilter(request.GET) + + # 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_city == "all": + filter_city = None + + if filter_state: + ctx["filter_state"] = filter_state + qs = qs.filter(place__state__iexact=filter_state) + form.fields["city"].widget.choices = [("all","Todas as cidades")] + get_choices(filter_state) + + if filter_city: + ctx["filter_city"] = filter_city + qs = qs.filter(place__city__iexact=filter_city) + + del form.fields["place"] + else: + form = CandidateListFilter(request.GET, initial={"uf": instance.state, "city": instance.city}) + + # Filtered List + qs = Candidate.objects.filter(place__state=instance.state, place__city=instance.city, status=CandidateStatusChoices.published) + + filter_place = request.GET.get("place", None) + + if filter_place == "all": + filter_place = None + + if instance.city: + form.fields["place"].widget.choices = [("all","Todas as regiões")] + get_choices_places(instance.state, instance.city) + + if filter_place: + ctx["filter_place"] = filter_place + qs = qs.filter(place__place__iexact=filter_place) + + del form.fields["uf"] + del form.fields["city"] + ctx["form"] = form diff --git a/app/eleicao/forms/filters.py b/app/eleicao/forms/filters.py index 69c266db..6a4b183b 100644 --- a/app/eleicao/forms/filters.py +++ b/app/eleicao/forms/filters.py @@ -32,6 +32,16 @@ class CandidateListFilter(forms.Form): ) ) + place = forms.CharField( + label="Região", + widget=forms.Select( + attrs={ + "data-cep-fields": "place", + "data-cep-url": reverse_lazy("eleicao:cep"), + } + ) + ) + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) diff --git a/app/eleicao/models.py b/app/eleicao/models.py index 6a7d8f00..72aa7831 100644 --- a/app/eleicao/models.py +++ b/app/eleicao/models.py @@ -164,6 +164,11 @@ class Meta: verbose_name_plural = "Eleitores" +class EleicaoCandidateList(CMSPlugin): + city = models.CharField("Cidade", max_length=120, blank=True, null=True) + state = models.CharField("Estado", max_length=2, blank=True, null=True, choices=lazy(get_states, list)()) + + class EleicaoCarousel(CMSPlugin): title = models.CharField("Título", max_length=120) description = models.CharField("Descrição", max_length=120, null=True, blank=True) diff --git a/app/eleicao/places/views.py b/app/eleicao/places/views.py index e32f633d..aae4e0e6 100644 --- a/app/eleicao/places/views.py +++ b/app/eleicao/places/views.py @@ -20,6 +20,10 @@ def get_choices(uf, city=None): return list(sorted(set(choices))) +def get_choices_places(uf, city): + return list(PollingPlace.objects.filter(state=uf, city=city).values_list("place", "place")) + + def fetch_cep(request): state = request.GET.get("state") city = request.GET.get("city") diff --git a/app/eleicao/static/js/widgets/cep-fields.js b/app/eleicao/static/js/widgets/cep-fields.js index 7fecdef8..08faa8e6 100644 --- a/app/eleicao/static/js/widgets/cep-fields.js +++ b/app/eleicao/static/js/widgets/cep-fields.js @@ -42,6 +42,20 @@ }); }); }); + + var place + $placeField.on("change", (evt) => { + place = evt.target.value; + const url = $placeField.data("cep-url"); + + $.get(url + "?place=" + place, (data) => { + $placeField.empty() + $placeField.append('') + $.each(data.choices, (index, value) => { + $placeField.append('") + }); + }); + }); } }); }(window.jQuery)); \ No newline at end of file diff --git a/app/eleicao/templates/eleicao/filters/candidate_list_filter.html b/app/eleicao/templates/eleicao/filters/candidate_list_filter.html index ec6bf7bb..b8e50ec3 100644 --- a/app/eleicao/templates/eleicao/filters/candidate_list_filter.html +++ b/app/eleicao/templates/eleicao/filters/candidate_list_filter.html @@ -1,15 +1,27 @@ -
-
- - {{ form.uf }} -
-
- - {{ form.city }} -
+ + {% if form.city %} +
+ + {{ form.uf }} +
+ {% endif %} + {% if form.city %} +
+ + {{ form.city }} +
+ {% endif %} + {% if form.place %} +
+ + {{ form.place }} +
+ {% endif %} -
+ \ No newline at end of file diff --git a/app/eleicao/templates/eleicao/plugins/candidate_list.html b/app/eleicao/templates/eleicao/plugins/candidate_list.html index 9632d281..437b0e4f 100644 --- a/app/eleicao/templates/eleicao/plugins/candidate_list.html +++ b/app/eleicao/templates/eleicao/plugins/candidate_list.html @@ -26,7 +26,7 @@

Conheça as candidaturas

{% if is_paginated %}