diff --git a/address_finder/api.py b/address_finder/api.py index 7409822..b69a4c4 100644 --- a/address_finder/api.py +++ b/address_finder/api.py @@ -54,7 +54,7 @@ class AddressFinder: print("Error querying DB : {0}".format(err), file=sys.stderr) return [] - communesMap=dict() + communesMap = dict() for row in cur.fetchall(): row_obj = dict(row) commune = Commune( @@ -62,12 +62,12 @@ class AddressFinder: nom=row_obj[DB_COL_COMMUNE_NAME], codeZip=row_obj[DB_COL_COMMUNE_POSTE]) # This way we avoid duplicates - communesMap[commune["codeInsee"]]= commune + communesMap[commune["codeInsee"]] = commune con.close() return list(communesMap.values()) - def getCommuneFantoirVoies(self, communeInseeCode: str) -> list[FantoirVoie]: + def getCommuneFantoirVoies(self, communeInseeCode: str, voieSearch: str = None) -> list[FantoirVoie]: # Extract data from DB con = sqlite3.connect(self.dbPath) @@ -92,6 +92,19 @@ class AddressFinder: data_dict = json.loads(data.get("value")) # In extracted JSON data, the interesting payload is behind "value" key fantoir_dict = data_dict.get("value") + if voieSearch is not None: + regexSearch = r".*" + for expr in voieSearch.split(' '): + regexSearch += r"(?=" + expr + r").*" + regexSearch += r".*" + fantoir_voies_filtered = [] + for voie in fantoir_dict: + for libelle in voie['libelle']: + if re.search(regexSearch, libelle, re.IGNORECASE): + fantoir_voies_filtered.append(voie) + break + fantoir_dict = fantoir_voies_filtered + else: print("Did not found any data matching Insee code " + str(communeInseeCode)) diff --git a/templates/landing_form.html b/templates/landing_form.html index c2bcc1b..e6b0c1a 100644 --- a/templates/landing_form.html +++ b/templates/landing_form.html @@ -25,10 +25,10 @@