remove duplicates communes

This commit is contained in:
Johan Le Baut 2022-01-26 21:36:28 +01:00
parent 50dd783b57
commit 2d4b428093
4 changed files with 58 additions and 53 deletions

View file

@ -20,6 +20,8 @@ DB_COL_FANTOIR_INSEE="key"
DB_FANTOIR_INSEE_KEY_SUFFIX = "keyv:" DB_FANTOIR_INSEE_KEY_SUFFIX = "keyv:"
# Utility to find an address # Utility to find an address
class AddressFinder: class AddressFinder:
def __init__(self, db_addresses_sqlite_path: str): def __init__(self, db_addresses_sqlite_path: str):
@ -32,36 +34,38 @@ class AddressFinder:
cur = con.cursor() cur = con.cursor()
communes: list[Commune] = [] communes: list[Commune] = []
try:
if communeNameOrZip is None:
cur.execute(f"SELECT * from \"{DB_TABLE_INSEE_NAME}\"")
else:
communeSearch = communeNameOrZip communeSearch = communeNameOrZip
zipSearch = communeNameOrZip zipSearch = communeNameOrZip
searchOpertor = "OR" searchOpertor = "OR"
regexCommuneAndZip = r"[0-9]{5} .+" # For example: '33000 BO' regexCommuneAndZip = r"[0-9]{5} .+" # For example: '33000 BO'
if re.match(regexCommuneAndZip, communeNameOrZip): if re.match(regexCommuneAndZip, communeNameOrZip):
print("MATCH")
splitSearch = communeNameOrZip.split(' ') splitSearch = communeNameOrZip.split(' ')
zipSearch = splitSearch[0] zipSearch = splitSearch[0]
communeSearch = ' '.join(splitSearch[1:]) communeSearch = ' '.join(splitSearch[1:])
searchOpertor = "AND" searchOpertor = "AND"
cur.execute(
try: 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:
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: except sqlite3.OperationalError as err:
print("Error querying DB : {0}".format(err), file=sys.stderr) print("Error querying DB : {0}".format(err), file=sys.stderr)
return [] return []
rows = [dict(row) for row in cur.fetchall()]
print(rows) communesMap=dict()
con.close() for row in cur.fetchall():
for row in rows: row_obj = dict(row)
commune = Commune( commune = Commune(
codeInsee=row[DB_COL_COMMUNE_INSEE], codeInsee=row_obj[DB_COL_COMMUNE_INSEE],
nom=row[DB_COL_COMMUNE_NAME], nom=row_obj[DB_COL_COMMUNE_NAME],
codeZip=row[DB_COL_COMMUNE_POSTE]) codeZip=row_obj[DB_COL_COMMUNE_POSTE])
communes.append(commune) # This way we avoid duplicates
return communes communesMap[commune["codeInsee"]]= commune
con.close()
return list(communesMap.values())
def getCommuneFantoirVoies(self, communeInseeCode: str) -> list[FantoirVoie]: def getCommuneFantoirVoies(self, communeInseeCode: str) -> list[FantoirVoie]:
@ -70,14 +74,15 @@ class AddressFinder:
con.row_factory = sqlite3.Row con.row_factory = sqlite3.Row
cur = con.cursor() cur = con.cursor()
try: 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: except sqlite3.OperationalError as err:
print("Error querying DB : {0}".format(err), file=sys.stderr) print("Error querying DB : {0}".format(err), file=sys.stderr)
return [] return []
data_raw = cur.fetchone() data_raw = cur.fetchone()
con.close() con.close()
## Get JSON payload # Get JSON payload
fantoir_dict = [] fantoir_dict = []
# Check if data where found # Check if data where found
@ -88,7 +93,8 @@ class AddressFinder:
# In extracted JSON data, the interesting payload is behind "value" key # In extracted JSON data, the interesting payload is behind "value" key
fantoir_dict = data_dict.get("value") fantoir_dict = data_dict.get("value")
else: 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 the json dump
return fantoir_dict return fantoir_dict

View file

@ -52,7 +52,7 @@
<form> <form>
<div class="form-group" id="communeForm"> <div class="form-group" id="communeForm">
<label class="form-label" for="communeInput">Commune</label> <label class="form-label" for="communeInput">Commune</label>
<input type="text" list="communes" class="form-control" id="communeInput" aria-describedby="communeHelp" placeholder="Nom de la commune ou code postal"> <input type="text" list="communes" class="form-control" id="communeInput" ondblclick="this.focus();this.select()" aria-describedby="communeHelp" placeholder="Nom de la commune ou code postal">
<datalist id="communes"> <datalist id="communes">
</datalist> </datalist>
</div> </div>
@ -113,7 +113,6 @@
}); });
$('#communeInput').on('keyup', function () { $('#communeInput').on('keyup', function () {
console.log("call ",$(this).val() )
fillCommunes($(this).val()); fillCommunes($(this).val());
}); });
$('#communeInput').on('input', function() { $('#communeInput').on('input', function() {

View file

@ -27,6 +27,7 @@ body {
} }
#ptoHelp { #ptoHelp {
color: rgb(192, 192, 192); color: rgb(192, 192, 192);
text-decoration: underline;
} }
.sable .btn-ciel, .ciel .btn-sable{ .sable .btn-ciel, .ciel .btn-sable{

View file

@ -44,7 +44,6 @@ def get_communes():
response=json.dumps(communes), response=json.dumps(communes),
mimetype='application/json' mimetype='application/json'
) )
print(response)
return response return response
@app.route("/addresses/fantoirvoies/<codeInsee>", methods=['GET']) @app.route("/addresses/fantoirvoies/<codeInsee>", methods=['GET'])