Skip to content

Commit

Permalink
feat(votepeloclima): refactor read_csv_file function
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelzinh3 committed Jul 15, 2024
1 parent aa05b7b commit 4bc89ac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/votepeloclima/candidature/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.urls import reverse_lazy

from .fields import ValidateOnceReCaptchaField
from .utils import get_ufs, get_choices
from .locations_utils import get_ufs, get_choices


class CaptchaForm(forms.Form):
Expand Down
34 changes: 34 additions & 0 deletions app/votepeloclima/candidature/locations_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.conf import settings
from pathlib import Path
import csv
from typing import List

def read_csv_file(file_path: Path) -> List[dict]:
with open(file_path) as f:
reader = csv.DictReader(f)
reader.fieldnames = [field.strip() for field in reader.fieldnames]
return [row for row in reader]

def get_states(column_label="Nome_UF"):
csv_filename = Path(settings.BASE_DIR) / "votepeloclima/candidature/csv/places.csv"
rows = read_csv_file(csv_filename)
states = set()
for row in rows:
uf = row["UF"].strip()
state_name = row[column_label].strip()
states.add((uf, state_name))
return sorted(list(states), key=lambda x: x[1])

def get_ufs():
return get_states(column_label="Nome_UF")

def get_choices(uf):
csv_filename = Path(settings.BASE_DIR) / "votepeloclima/candidature/csv/places.csv"
rows = read_csv_file(csv_filename)
choices = []
for row in rows:
if row["UF"].strip() == uf:
city_code = row["Código Município Completo"].strip()
city_name = row["Nome_Município"].strip()
choices.append((city_code, city_name))
return sorted(choices, key=lambda x: x[1])
30 changes: 0 additions & 30 deletions app/votepeloclima/candidature/utils.py

This file was deleted.

2 changes: 1 addition & 1 deletion app/votepeloclima/candidature/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from contrib.oauth.utils import send_confirmation_email
from .models import CandidatureFlow, CandidatureFlowStatus, Candidature
from .forms import register_form_list, InitialForm, FlagForm, AppointmentForm
from .utils import get_choices
from .locations_utils import get_choices


class RegisterView(NamedUrlSessionWizardView):
Expand Down

0 comments on commit 4bc89ac

Please sign in to comment.