Skip to content

Commit

Permalink
fix(votepeloclima): refactor locations_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelzinh3 committed Jul 15, 2024
1 parent fac5e44 commit 3d8c0ac
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions app/votepeloclima/candidature/locations_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from django.conf import settings
from pathlib import Path
import csv
from typing import List
from typing import List, Tuple, Set

csv_filename = Path(settings.BASE_DIR) / "votepeloclima/candidature/csv/places.csv"

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"
def get_states(column_label="Nome_UF") -> List[Tuple[str, str]]:
rows = read_csv_file(csv_filename)
states = set()
for row in rows:
Expand All @@ -19,16 +20,18 @@ def get_states(column_label="Nome_UF"):
states.add((uf, state_name))
return sorted(list(states), key=lambda x: x[1])

def get_ufs():
def get_ufs() -> List[Tuple[str, str]]:
return get_states(column_label="Nome_UF")

def get_choices(uf):
csv_filename = Path(settings.BASE_DIR) / "votepeloclima/candidature/csv/places.csv"
def get_choices(uf: str) -> List[Tuple[str, str]]:
rows = read_csv_file(csv_filename)
choices = []
seen_cities = set()
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))
if city_name not in seen_cities:
choices.append((city_code, city_name))
seen_cities.add(city_name)
return sorted(choices, key=lambda x: x[1])

0 comments on commit 3d8c0ac

Please sign in to comment.