From e9d66759d94e892b0d083a3cb6ff34aaa665299d Mon Sep 17 00:00:00 2001 From: Mario Sergio Date: Tue, 8 Oct 2024 15:07:31 -0300 Subject: [PATCH] feat(vote): Included filters on sidebar --- .../votepeloclima/candidature/choices.py | 1 + .../votepeloclima/candidature/forms/filters.py | 15 +++++++++++++-- .../votepeloclima/candidature/views/filters.py | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/org_eleicoes/votepeloclima/candidature/choices.py b/app/org_eleicoes/votepeloclima/candidature/choices.py index 4255df0b..29bb9ada 100644 --- a/app/org_eleicoes/votepeloclima/candidature/choices.py +++ b/app/org_eleicoes/votepeloclima/candidature/choices.py @@ -113,6 +113,7 @@ class Education(models.TextChoices): doutorado_completo = "doutorado_completo", "Doutorado Completo" class ElectionStatus(models.TextChoices): + empty = "", "Selecione uma opção" eleita = "eleita", "Eleita/o" segundo_turno = "segundo_turno", "Segundo Turno" nao_eleita = "nao_eleito", "Não Eleita/o" \ No newline at end of file diff --git a/app/org_eleicoes/votepeloclima/candidature/forms/filters.py b/app/org_eleicoes/votepeloclima/candidature/forms/filters.py index 9c9b9ad1..c29539f1 100644 --- a/app/org_eleicoes/votepeloclima/candidature/forms/filters.py +++ b/app/org_eleicoes/votepeloclima/candidature/forms/filters.py @@ -6,7 +6,7 @@ # from django_select2.forms import Select2Widget from ..layout import NoCrispyField -from ..choices import Gender, Color, Sexuality +from ..choices import Gender, Color, Sexuality, ElectionStatus from ..fields import CepField, ButtonCheckboxSelectMultiple, ButtonRadioSelect from ..locations_utils import get_states, get_choices from ..models import Candidature @@ -68,6 +68,16 @@ def __init__(self, *args, **kwargs): class FilterFormSidebar(RemoveRequiredMixin, forms.ModelForm): + election_status = forms.ChoiceField( + label="Filtrar por", + choices=[ + ("second_round", "Candidaturas no 2º turno"), + ("all", "Todas as candidaturas"), + ("elected", "Candidaturas eleitas") + ], + widget=ButtonRadioSelect, + initial="second_round" # Define o filtro de 2º turno como padrão + ) proposes = forms.MultipleChoiceField( label="Propostas", widget=ButtonCheckboxSelectMultiple ) @@ -88,7 +98,7 @@ class FilterFormSidebar(RemoveRequiredMixin, forms.ModelForm): class Meta: model = Candidature - fields = ["proposes", "mandate_type", "gender", "color", "sexuality"] + fields = ["election_status", "proposes", "mandate_type", "gender", "color", "sexuality"] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -96,6 +106,7 @@ def __init__(self, *args, **kwargs): self.fields["proposes"].choices = self.get_proposes_choices() self.helper.layout = Layout( + NoCrispyField("election_status"), NoCrispyField("proposes"), NoCrispyField("mandate_type"), NoCrispyField("gender"), diff --git a/app/org_eleicoes/votepeloclima/candidature/views/filters.py b/app/org_eleicoes/votepeloclima/candidature/views/filters.py index e86a78ed..a3c043d4 100644 --- a/app/org_eleicoes/votepeloclima/candidature/views/filters.py +++ b/app/org_eleicoes/votepeloclima/candidature/views/filters.py @@ -3,6 +3,7 @@ from ..models import CandidatureFlowStatus, Candidature from ..forms.filters import FilterFactoryForm +from ..choices import ElectionStatus class CandidatureSearchView(ListView): @@ -77,6 +78,18 @@ def get_queryset(self): ) ) + election_status = cleaned_data.get("election_status", "second_round") + else: + election_status = "second_round" + + # Filtra com base no status da eleição + if election_status == "second_round": + # Filtra candidaturas que foram para o 2º turno + queryset = queryset.filter(election_results__status=ElectionStatus.segundo_turno) + elif election_status == "elected": + # Filtra candidaturas eleitas + queryset = queryset.filter(election_results__status=ElectionStatus.eleita) + return queryset def get_context_data(self, **kwargs):