From 2d4b428093d3daa8a639fe77104ea7e44a50af3a Mon Sep 17 00:00:00 2001 From: Johan Le Baut Date: Wed, 26 Jan 2022 21:36:28 +0100 Subject: [PATCH] remove duplicates communes --- address_finder/api.py | 106 +++++++++++++++++++----------------- templates/landing_form.html | 3 +- templates/style.css | 1 + webapp.py | 1 - 4 files changed, 58 insertions(+), 53 deletions(-) diff --git a/address_finder/api.py b/address_finder/api.py index 548ff33..7409822 100644 --- a/address_finder/api.py +++ b/address_finder/api.py @@ -1,25 +1,27 @@ import sqlite3 import sys import json -from .model import Commune,FantoirVoie +from .model import Commune, FantoirVoie import re # DB with addresses info -DB_ADDRESSES_PATH_ENV="DB_ADDRESSES_PATH" -DB_ADDRESSES_DEFAULT_PATH="/etc/fantoir.sqlite" +DB_ADDRESSES_PATH_ENV = "DB_ADDRESSES_PATH" +DB_ADDRESSES_DEFAULT_PATH = "/etc/fantoir.sqlite" # Table for insee codes -DB_TABLE_INSEE_NAME="insee" -DB_COL_COMMUNE_INSEE="Code_commune_INSEE" -DB_COL_COMMUNE_NAME="Nom_commune" -DB_COL_COMMUNE_POSTE="Code_postal" +DB_TABLE_INSEE_NAME = "insee" +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:" +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): @@ -32,63 +34,67 @@ class AddressFinder: cur = con.cursor() communes: list[Commune] = [] - communeSearch = communeNameOrZip - zipSearch = communeNameOrZip - searchOpertor = "OR" - - regexCommuneAndZip = r"[0-9]{5} .+" # For example: '33000 BO' - if re.match(regexCommuneAndZip, communeNameOrZip): - print("MATCH") - splitSearch = communeNameOrZip.split(' ') - zipSearch = splitSearch[0] - communeSearch = ' '.join(splitSearch[1:]) - searchOpertor = "AND" - try: - if communeNameOrZip is None: - cur.execute(f"SELECT * from \"{DB_TABLE_INSEE_NAME}\"") - else: - cur.execute(f"SELECT * from \"{DB_TABLE_INSEE_NAME}\" WHERE {DB_COL_COMMUNE_NAME} LIKE \"%{communeSearch}%\" COLLATE nocase {searchOpertor} {DB_COL_COMMUNE_POSTE} LIKE \"{zipSearch}%\"") + if communeNameOrZip is None: + cur.execute(f"SELECT * from \"{DB_TABLE_INSEE_NAME}\"") + else: + communeSearch = communeNameOrZip + zipSearch = communeNameOrZip + searchOpertor = "OR" + + regexCommuneAndZip = r"[0-9]{5} .+" # For example: '33000 BO' + if re.match(regexCommuneAndZip, communeNameOrZip): + splitSearch = communeNameOrZip.split(' ') + zipSearch = splitSearch[0] + communeSearch = ' '.join(splitSearch[1:]) + searchOpertor = "AND" + cur.execute( + f"SELECT * from \"{DB_TABLE_INSEE_NAME}\" WHERE {DB_COL_COMMUNE_NAME} LIKE \"%{communeSearch}%\" COLLATE nocase {searchOpertor} {DB_COL_COMMUNE_POSTE} LIKE \"{zipSearch}%\"") except sqlite3.OperationalError as err: - print("Error querying DB : {0}".format(err), file=sys.stderr) - return [] - rows = [dict(row) for row in cur.fetchall()] - print(rows) + print("Error querying DB : {0}".format(err), file=sys.stderr) + return [] + + communesMap=dict() + for row in cur.fetchall(): + row_obj = dict(row) + commune = Commune( + codeInsee=row_obj[DB_COL_COMMUNE_INSEE], + nom=row_obj[DB_COL_COMMUNE_NAME], + codeZip=row_obj[DB_COL_COMMUNE_POSTE]) + # This way we avoid duplicates + communesMap[commune["codeInsee"]]= commune + con.close() - for row in rows: - commune=Commune( - codeInsee=row[DB_COL_COMMUNE_INSEE], - nom=row[DB_COL_COMMUNE_NAME], - codeZip=row[DB_COL_COMMUNE_POSTE]) - communes.append(commune) - return communes + return list(communesMap.values()) def getCommuneFantoirVoies(self, communeInseeCode: str) -> list[FantoirVoie]: - + # Extract data from DB con = sqlite3.connect(self.dbPath) con.row_factory = sqlite3.Row cur = con.cursor() try: - cur.execute(f"SELECT value from \"{DB_TABLE_FANTOIR_NAME}\" WHERE {DB_COL_FANTOIR_INSEE}=\"{DB_FANTOIR_INSEE_KEY_SUFFIX}{communeInseeCode}\"") + cur.execute( + f"SELECT value from \"{DB_TABLE_FANTOIR_NAME}\" WHERE {DB_COL_FANTOIR_INSEE}=\"{DB_FANTOIR_INSEE_KEY_SUFFIX}{communeInseeCode}\"") except sqlite3.OperationalError as err: - print("Error querying DB : {0}".format(err), file=sys.stderr) - return [] + print("Error querying DB : {0}".format(err), file=sys.stderr) + return [] data_raw = cur.fetchone() con.close() - ## Get JSON payload - + # Get JSON payload + fantoir_dict = [] # Check if data where found if data_raw is not None: - data = dict(data_raw) - # Extract the data behind "value" which is a JSON structure - data_dict=json.loads(data.get("value")) - # In extracted JSON data, the interesting payload is behind "value" key - fantoir_dict = data_dict.get("value") + data = dict(data_raw) + # Extract the data behind "value" which is a JSON structure + data_dict = json.loads(data.get("value")) + # In extracted JSON data, the interesting payload is behind "value" key + fantoir_dict = data_dict.get("value") else: - print("Did not found any data matching Insee code " + str(communeInseeCode)) - + print("Did not found any data matching Insee code " + + str(communeInseeCode)) + # Return the json dump return fantoir_dict diff --git a/templates/landing_form.html b/templates/landing_form.html index 26bb0f3..c2bcc1b 100644 --- a/templates/landing_form.html +++ b/templates/landing_form.html @@ -52,7 +52,7 @@
- +
@@ -113,7 +113,6 @@ }); $('#communeInput').on('keyup', function () { - console.log("call ",$(this).val() ) fillCommunes($(this).val()); }); $('#communeInput').on('input', function() { diff --git a/templates/style.css b/templates/style.css index dbe2f84..bcfada8 100644 --- a/templates/style.css +++ b/templates/style.css @@ -27,6 +27,7 @@ body { } #ptoHelp { color: rgb(192, 192, 192); + text-decoration: underline; } .sable .btn-ciel, .ciel .btn-sable{ diff --git a/webapp.py b/webapp.py index 31f40c9..f87ab70 100644 --- a/webapp.py +++ b/webapp.py @@ -44,7 +44,6 @@ def get_communes(): response=json.dumps(communes), mimetype='application/json' ) - print(response) return response @app.route("/addresses/fantoirvoies/", methods=['GET'])