Skip to content

Commit

Permalink
feat: refactor candidate list plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelzinh3 committed Nov 10, 2023
1 parent 77f2ffa commit f47829b
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/eleicao/cms_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class EleicaoApphook(CMSApp):
name = "Eleição do ano"

def get_urls(self, page=None, language=None, **kwargs):
return ["eleicao.urls"]
return ["eleicao.urls"]
64 changes: 47 additions & 17 deletions app/eleicao/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,29 +51,58 @@ 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

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

Expand Down
10 changes: 10 additions & 0 deletions app/eleicao/forms/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions app/eleicao/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions app/eleicao/places/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 14 additions & 0 deletions app/eleicao/static/js/widgets/cep-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<option value="">----</option>')
$.each(data.choices, (index, value) => {
$placeField.append('<option value="' + value[0] + '">' + value[1] + "</option>")
});
});
});
}
});
}(window.jQuery));
40 changes: 26 additions & 14 deletions app/eleicao/templates/eleicao/filters/candidate_list_filter.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
<form class="flex flex-wrap gap-4 items-end pb-12 w-full filters">
<div class="form-control">
<label class="label">
<span class="font-bold label-text">{{ form.uf.label }}</span>
</label>
{{ form.uf }}
</div>
<div class="form-control">
<label class="label">
<span class="font-bold label-text">{{ form.city.label }}</span>
</label>
{{ form.city }}
</div>
<form method="get" action="" class="flex flex-wrap gap-4 items-end pb-12 w-full filters">
{% if form.city %}
<div class="form-control">
<label class="label">
<span class="font-bold label-text">{{ form.uf.label }}</span>
</label>
{{ form.uf }}
</div>
{% endif %}
{% if form.city %}
<div class="form-control">
<label class="label">
<span class="font-bold label-text">{{ form.city.label }}</span>
</label>
{{ form.city }}
</div>
{% endif %}
{% if form.place %}
<div class="form-control">
<label class="label">
<span class="font-bold label-text">{{ form.place.label }}</span>
</label>
{{ form.place }}
</div>
{% endif %}
<input class="btn btn-primary" type="submit" value="Buscar" />
</form>
</form>
6 changes: 3 additions & 3 deletions app/eleicao/templates/eleicao/plugins/candidate_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h1 class="mb-3 text-4xl md:text-6xl">Conheça as candidaturas</h1>
{% if is_paginated %}
<ul class="flex py-6 mx-auto text-xl font-bold text-center join">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}{% if filter_state %}&uf={{filter_state}}{% endif %}" class="join-item btn">&laquo;</a></li>
<li><a href="?page={{ page_obj.previous_page_number }}{% if filter_state %}&uf={{filter_state}}{% endif %}{% if filter_city %}&city={{filter_city}}{% endif %}{% if filter_place %}&place={{filter_place}}{% endif %}" class="join-item btn">&laquo;</a></li>
{% else %}
<li class="join-item btn btn-disabled"><span>&laquo;</span></li>
{% endif %}
Expand All @@ -35,12 +35,12 @@ <h1 class="mb-3 text-4xl md:text-6xl">Conheça as candidaturas</h1>
{% if page_obj.number == page_num %}
<li><button class="join-item btn btn-md btn-active">{{ page_num }} </button></li>
{% elif page_num >= page_obj.number|add:"-2" and page_num <= page_obj.number|add:"2" %}
<li><a href="?page={{ page_num }}{% if filter_state %}&uf={{filter_state}}{% endif %}{% if filter_city %}&city={{filter_city}}{% endif %}" class="join-item btn btn-md">{{ page_num }}</a></li>
<li><a href="?page={{ page_num }}{% if filter_state %}&uf={{filter_state}}{% endif %}{% if filter_city %}&city={{filter_city}}{% endif %}{% if filter_place %}&place={{filter_place}}{% endif %}" class="join-item btn btn-md">{{ page_num }}</a></li>
{% endif %}
{% endfor %}

{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}{% if filter_state %}&uf={{filter_state}}{% endif %}{% if filter_city %}&city={{filter_city}}{% endif %}" class="join-item btn">&raquo;</a></li>
<li><a href="?page={{ page_obj.next_page_number }}{% if filter_state %}&uf={{filter_state}}{% endif %}{% if filter_city %}&city={{filter_city}}{% endif %}{% if filter_place %}&place={{filter_place}}{% endif %}" class="join-item btn">&raquo;</a></li>
{% else %}
<li class="join-item btn btn-disabled"><span>&raquo;</span></li>
{% endif %}
Expand Down

0 comments on commit f47829b

Please sign in to comment.