diff --git a/address_finder/api.py b/address_finder/api.py index b2025a6..212c62e 100644 --- a/address_finder/api.py +++ b/address_finder/api.py @@ -14,22 +14,17 @@ DB_COL_COMMUNE_INSEE = "Code_commune_INSEE" DB_COL_COMMUNE_NAME = "Nom_commune" DB_COL_COMMUNE_POSTE = "Code_postal" -# Table for Fantoir voies (code Rivoli) -DB_TABLE_FANTOIR_NAME = "keyv" -DB_COL_FANTOIR_INSEE = "key" -DB_FANTOIR_INSEE_KEY_SUFFIX = "keyv:" - # Utility to find an address - - class AddressFinder: - def __init__(self, db_addresses_sqlite_path: str): - self.dbPath = db_addresses_sqlite_path - print("DB addresses Path : " + self.dbPath) + def __init__(self, db_insee_communes_sqlite_path: str, db_fantoir_voies_sqlite_path: str): + self.dbCommunesPath = db_insee_communes_sqlite_path + self.dbFantoirPath = db_fantoir_voies_sqlite_path + print("DB insee communes Path : " + self.dbCommunesPath) + print("DB Fantoir voies Path : " + self.dbFantoirPath) def getCommunesFromNameOrZip(self, communeNameOrZip: str, limit: int = None) -> list[Commune]: - con = sqlite3.connect(self.dbPath) + con = sqlite3.connect(self.dbCommunesPath) con.row_factory = sqlite3.Row cur = con.cursor() @@ -80,25 +75,29 @@ class AddressFinder: def getCommuneFantoirVoies(self, communeInseeCode: str, voieSearch: str = None, limit: int = None) -> list[FantoirVoie]: # Extract data from DB - con = sqlite3.connect(self.dbPath) + con = sqlite3.connect(self.dbFantoirPath) con.row_factory = sqlite3.Row cur = con.cursor() + if voieSearch is None: + voieSearch='' + + # Check if a search limit is specified, make sure it is an integer + select_limit = "" + if limit is not None: + try: + select_limit = f"LIMIT {int(limit)}" + except ValueError: + print("Error, limit arg not a valid int: ", limit) + try: cur.execute( - f"SELECT trim(libelle), rivoli_with_key from fantoir WHERE full_insee=\"{communeInseeCode}\" AND libelle like \"%{voieSearch}%\"") + f"SELECT trim(libelle), rivoli_with_key from fantoir WHERE full_insee=\"{communeInseeCode}\" AND libelle like \"%{voieSearch}%\" {select_limit}") except sqlite3.OperationalError as err: print("Error querying DB : {0}".format(err), file=sys.stderr) return [] data_raw = cur.fetchall() con.close() - # Check if a search limit is specified, make sure it is an integer - if limit is not None: - try: - limit = int(limit) - except ValueError: - print("Error, limit arg not a valid int: ", limit) - fantoir_dict = [] # Check if data where found if data_raw is not None: diff --git a/address_finder/model.py b/address_finder/model.py index da84fb0..b1a9218 100644 --- a/address_finder/model.py +++ b/address_finder/model.py @@ -6,12 +6,5 @@ class Commune(TypedDict): codeZip: str class FantoirVoie(TypedDict): - id: str - dateAjout: int - libelle: list[str] - typeVoie:str - codeCommune: str - codeFantoir: str - cleRivoli: str - nomCommune: str - predecesseur: bool + libelle: str + rivoli_with_key: int diff --git a/config/config.py b/config/config.py index 9473c92..f55d4d0 100644 --- a/config/config.py +++ b/config/config.py @@ -2,11 +2,12 @@ import configparser class Config: - def __init__(self, username, password, source_addr, db_addresses_sqlite_path): + def __init__(self, username, password, source_addr, db_insee_communes_sqlite_path, db_fantoir_voies_sqlite_path): self.username = username self.password = password self.source_addr = source_addr - self.db_addresses_sqlite_path = db_addresses_sqlite_path + self.db_insee_communes_sqlite_path = db_insee_communes_sqlite_path + self.db_fantoir_voies_sqlite_path = db_fantoir_voies_sqlite_path self.debug = False @@ -17,5 +18,6 @@ def parse_config(cfgPath): username = cfg.get("API", "username") passwd = cfg.get("API", "password") source_addr = cfg.get("API", "source_addr") - db_addresses_sqlite_path = cfg.get("ADDRESSES","db_addresses_sqlite_path") - return Config(username, passwd, source_addr,db_addresses_sqlite_path) + db_insee_communes_sqlite_path = cfg.get("ADDRESSES","db_insee_communes_sqlite_path") + db_fantoir_voies_sqlite_path = cfg.get("ADDRESSES","db_fantoir_voies_sqlite_path") + return Config(username, passwd, source_addr,db_insee_communes_sqlite_path, db_fantoir_voies_sqlite_path) diff --git a/templates/landing_form.html b/templates/landing_form.html index 50093bc..b0702cd 100644 --- a/templates/landing_form.html +++ b/templates/landing_form.html @@ -52,7 +52,7 @@
- +
@@ -63,7 +63,7 @@
- +
diff --git a/webapp.py b/webapp.py index f732c0a..1d1d0fe 100644 --- a/webapp.py +++ b/webapp.py @@ -22,7 +22,7 @@ def load_config(): cfg = load_config() -addressFinder = AddressFinder(cfg.db_addresses_sqlite_path) +addressFinder = AddressFinder(cfg.db_insee_communes_sqlite_path, cfg.db_fantoir_voies_sqlite_path) app = Flask(__name__)