From d7373dc7506116217f3be3866cf0390af942e611 Mon Sep 17 00:00:00 2001 From: miguelzinh3 Date: Mon, 7 Aug 2023 14:32:07 -0300 Subject: [PATCH 1/3] fix(eleicao): remove zone from candidate form --- app/eleicao/csv/choices.py | 6 +++--- app/eleicao/forms/create.py | 6 +++--- .../migrations/0013_remove_candidate_zone.py | 17 +++++++++++++++++ app/eleicao/models.py | 2 +- app/eleicao/views.py | 2 ++ 5 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 app/eleicao/migrations/0013_remove_candidate_zone.py diff --git a/app/eleicao/csv/choices.py b/app/eleicao/csv/choices.py index 8137a50d..bc39c107 100644 --- a/app/eleicao/csv/choices.py +++ b/app/eleicao/csv/choices.py @@ -25,11 +25,11 @@ def get_choices(uf, city=None): for row in reader: if row["uf"] == uf: if city: - if city == row["city"]: + if city.upper() == row["city"].upper(): choices.append( - (row["neighborhood"], row["neighborhood"].capitalize()) + (row["neighborhood"].capitalize(), row["neighborhood"].capitalize()) ) else: - choices.append((row["city"], row["city"].capitalize())) + choices.append((row["city"].capitalize(), row["city"].capitalize())) return list(set(choices)) \ No newline at end of file diff --git a/app/eleicao/forms/create.py b/app/eleicao/forms/create.py index 62b15ae7..abc5b1af 100644 --- a/app/eleicao/forms/create.py +++ b/app/eleicao/forms/create.py @@ -12,7 +12,7 @@ class Meta: class Candidate2Form(forms.ModelForm): number = forms.IntegerField(label="Numero do candidato") - zone_id = forms.IntegerField() + # zone_id = forms.IntegerField() class Meta: model = Address @@ -20,7 +20,7 @@ class Meta: "state", "city", "neighborhood", - "zone_id", + # "zone_id", "number", ] @@ -30,7 +30,7 @@ def __init__(self, *args, **kwargs): # self.fields["state"].widget = s2forms.Select2Widget() self.fields["city"].widget = forms.Select() self.fields["neighborhood"].widget = forms.Select() - self.fields["zone_id"].widget = forms.Select() + # self.fields["zone_id"].widget = forms.Select() class Candidate3Form(forms.ModelForm): diff --git a/app/eleicao/migrations/0013_remove_candidate_zone.py b/app/eleicao/migrations/0013_remove_candidate_zone.py new file mode 100644 index 00000000..fdea901e --- /dev/null +++ b/app/eleicao/migrations/0013_remove_candidate_zone.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2 on 2023-08-07 17:06 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('eleicao', '0012_alter_address_neighborhood'), + ] + + operations = [ + migrations.RemoveField( + model_name='candidate', + name='zone', + ), + ] diff --git a/app/eleicao/models.py b/app/eleicao/models.py index e47764b9..0963d79f 100644 --- a/app/eleicao/models.py +++ b/app/eleicao/models.py @@ -89,7 +89,7 @@ class Candidate(models.Model): "Quero receber atualizações da campanha e do NOSSAS.", default=False ) themes = models.ManyToManyField(Theme) - zone = models.ForeignKey(PollingPlace, on_delete=models.CASCADE) + # zone = models.ForeignKey(PollingPlace, on_delete=models.CASCADE) place = models.ForeignKey(Address, on_delete=models.CASCADE) def __str__(self): diff --git a/app/eleicao/views.py b/app/eleicao/views.py index cc2dc8fd..cb463a33 100644 --- a/app/eleicao/views.py +++ b/app/eleicao/views.py @@ -77,6 +77,8 @@ def done(self, form_list, **kwargs): city = values.pop("city") neighborhood = values.pop("neighborhood") + import ipdb; ipdb.set_trace(); + values["place_id"] = ( Address.objects.filter(state=state, city=city, neighborhood=neighborhood) .first() From 66415cdde73815e8eb2540f5e9e7476fef870eb3 Mon Sep 17 00:00:00 2001 From: Adriele A Secundino Date: Mon, 7 Aug 2023 16:53:35 -0300 Subject: [PATCH 2/3] fix: change get activist to get_or_create --- app/eleicao/bonde_utils.py | 12 +++++++----- app/eleicao/views.py | 2 -- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/eleicao/bonde_utils.py b/app/eleicao/bonde_utils.py index 1df86d1c..b903d7e2 100644 --- a/app/eleicao/bonde_utils.py +++ b/app/eleicao/bonde_utils.py @@ -9,11 +9,13 @@ def create_form_entry(**form_data): email = form_data.get("email") name = form_data.get("name") - # Activist get_or_create - activist = Activist.objects.get(email=email) - - if not activist: - activist = Activist.objects.create(email=email, name=name) + + # Activist get_or_create + activist, created = Activist.objects.get_or_create(email=email) + + if created: + activist.name = name + activist.save() state = form_data.get("state") city = form_data.get("city") diff --git a/app/eleicao/views.py b/app/eleicao/views.py index cb463a33..cc2dc8fd 100644 --- a/app/eleicao/views.py +++ b/app/eleicao/views.py @@ -77,8 +77,6 @@ def done(self, form_list, **kwargs): city = values.pop("city") neighborhood = values.pop("neighborhood") - import ipdb; ipdb.set_trace(); - values["place_id"] = ( Address.objects.filter(state=state, city=city, neighborhood=neighborhood) .first() From b35e28eff1309519b552c0e0b37a8c06e46b4bd8 Mon Sep 17 00:00:00 2001 From: Adriele A Secundino Date: Tue, 8 Aug 2023 12:49:41 -0300 Subject: [PATCH 3/3] chore(eleicao): remove comments --- app/eleicao/forms/create.py | 29 +++++++++++++++-------------- app/eleicao/models.py | 1 - app/eleicao/views.py | 12 +++++------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/app/eleicao/forms/create.py b/app/eleicao/forms/create.py index abc5b1af..e5c82760 100644 --- a/app/eleicao/forms/create.py +++ b/app/eleicao/forms/create.py @@ -12,7 +12,6 @@ class Meta: class Candidate2Form(forms.ModelForm): number = forms.IntegerField(label="Numero do candidato") - # zone_id = forms.IntegerField() class Meta: model = Address @@ -20,7 +19,6 @@ class Meta: "state", "city", "neighborhood", - # "zone_id", "number", ] @@ -30,7 +28,6 @@ def __init__(self, *args, **kwargs): # self.fields["state"].widget = s2forms.Select2Widget() self.fields["city"].widget = forms.Select() self.fields["neighborhood"].widget = forms.Select() - # self.fields["zone_id"].widget = forms.Select() class Candidate3Form(forms.ModelForm): @@ -62,17 +59,21 @@ class Candidate6Form(forms.Form): agree = forms.BooleanField( label="Li e estou de acordo com os compromissos listados assim" ) + + class PlacesWidget(s2forms.ModelSelect2Widget): - - search_fields = [ - "places__state__icontains", - "places__city__icontains", - "places__neighborhood__icontains" - ] + search_fields = [ + "places__state__icontains", + "places__city__icontains", + "places__neighborhood__icontains", + ] + + class VoterForm(forms.ModelForm): - + zone = forms.ModelChoiceField( + queryset=PollingPlace.objects.all(), label="Onde você vota?", required=True + ) - zone = forms.ModelChoiceField(queryset= PollingPlace.objects.all(), label ="Onde você vota?", required=True) - class Meta: - model = Voter - fields = ["name", "email", "whatsapp", "zone"] \ No newline at end of file + class Meta: + model = Voter + fields = ["name", "email", "whatsapp", "zone"] diff --git a/app/eleicao/models.py b/app/eleicao/models.py index 0963d79f..38a93891 100644 --- a/app/eleicao/models.py +++ b/app/eleicao/models.py @@ -89,7 +89,6 @@ class Candidate(models.Model): "Quero receber atualizações da campanha e do NOSSAS.", default=False ) themes = models.ManyToManyField(Theme) - # zone = models.ForeignKey(PollingPlace, on_delete=models.CASCADE) place = models.ForeignKey(Address, on_delete=models.CASCADE) def __str__(self): diff --git a/app/eleicao/views.py b/app/eleicao/views.py index cc2dc8fd..148daa9a 100644 --- a/app/eleicao/views.py +++ b/app/eleicao/views.py @@ -38,7 +38,7 @@ def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: def get_queryset(self) -> QuerySet[Any]: qs = super().get_queryset() - + filter_state = self.request.GET.get("uf", None) if filter_state: return qs.filter(place__state__iexact=filter_state) @@ -56,6 +56,7 @@ class CandidateCreateView(SessionWizardView): Candidate5Form, Candidate6Form, ] + file_storage = settings.DEFAULT_FILE_STORAGE # model = Candidate # fields = "__all__" @@ -82,16 +83,11 @@ def done(self, form_list, **kwargs): .first() .id ) - # PollingPlace - # zone = values.pop("zone") - # polling_place = PollingPlace.objects.get(id=zone) obj = Candidate.objects.create(**values) - # obj.zone = polling_place obj.themes.set(themes) obj.save() - # Integrate with Bonde fe = create_form_entry(state=state, city=city, **values) @@ -116,6 +112,8 @@ def results_view(request): filter_zone = request.GET.get("zone", None) return render(request=request, template_name="eleicao/voter_results.html") + + class ResultsCandidateView(ListView): template_name = "eleicao/voter_results.html" model = Candidate @@ -128,4 +126,4 @@ def get_queryset(self) -> QuerySet[Any]: if filter_zone: return Candidate.objects.filter(zone=filter_zone) - return super().get_queryset() \ No newline at end of file + return super().get_queryset()