From 3acd1dd34978504ab45dc982543b0ba96bef3886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Baylac-Jacqu=C3=A9?= Date: Sat, 12 Feb 2022 11:50:57 +0100 Subject: [PATCH] Adapt backend to new fantoir data schema We updated the fantoir DB schema. See https://git.alternativebit.fr/NinjaTrappeur/fast-fantoir/src/branch/master for more informations. --- README.md | 6 +++++ address_finder/api.py | 34 +++------------------------- scripts/import-laposte-insee.sh | 29 ++++++++++++++++++++++++ templates/landing_form.html | 40 +++++++++++++++++---------------- 4 files changed, 59 insertions(+), 50 deletions(-) create mode 100755 scripts/import-laposte-insee.sh diff --git a/README.md b/README.md index d276094..d26f959 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,9 @@ curl -v http://127.0.0.1:5000/addresses/communes?s=29530 # Chercher les voies d'une commune via son code insee curl -v http://127.0.0.1:5000/addresses/fantoirvoies/29036 ``` + +### Jeux de données + +#### Insee + +Importez ce CSV https://www.data.gouv.fr/fr/datasets/base-officielle-des-codes-postaux/ dans la base de données diff --git a/address_finder/api.py b/address_finder/api.py index 57015ff..b2025a6 100644 --- a/address_finder/api.py +++ b/address_finder/api.py @@ -85,15 +85,13 @@ class AddressFinder: 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}\"") + f"SELECT trim(libelle), rivoli_with_key from fantoir WHERE full_insee=\"{communeInseeCode}\" AND libelle like \"%{voieSearch}%\"") except sqlite3.OperationalError as err: print("Error querying DB : {0}".format(err), file=sys.stderr) return [] - data_raw = cur.fetchone() + data_raw = cur.fetchall() con.close() - # Get JSON payload - # Check if a search limit is specified, make sure it is an integer if limit is not None: try: @@ -104,33 +102,7 @@ class AddressFinder: 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") - - # Apply search filter if any - if voieSearch is not None: - # Can match multiple words, for example "avenue noe" matches "avenue de noes" - regexSearch = r".*" - for expr in voieSearch.split(' '): - regexSearch += r"(?=" + expr + r").*" - regexSearch += r".*" - fantoir_voies_filtered = [] - nb_match = 0 - for voie in fantoir_dict: - for libelle in voie['libelle']: - if re.search(regexSearch, libelle, re.IGNORECASE): - fantoir_voies_filtered.append(voie) - nb_match += 1 - break - if limit is not None and nb_match >= limit: - break - fantoir_dict = fantoir_voies_filtered - else: - # Apply search limit if any - fantoir_dict = fantoir_dict[:limit] + fantoir_dict = dict(data_raw) else: print("Did not found any data matching Insee code " + str(communeInseeCode)) diff --git a/scripts/import-laposte-insee.sh b/scripts/import-laposte-insee.sh new file mode 100755 index 0000000..88c8b28 --- /dev/null +++ b/scripts/import-laposte-insee.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -eau -o pipefail + +if [ "$#" -ne 2 ]; then + echo "Usage: import-laposte-insee.sh path-to-laposte-insee-CSV fantoir-sqlite-db-path" + echo "" + echo "ERROR: Missing laposte CSV." + echo "You can download it at https://www.data.gouv.fr/fr/datasets/base-officielle-des-codes-postaux/" + exit 1 +fi + +tmpDir=$(mktemp -d) +clean_tmp () { + rm -r "${tmpDir}" +} +trap clean_tmp EXIT +tmpSql="${tmpDir}"/import-laposte-hexasmal.sql + +echo "Importing laposte/insee hexasmal data into the fantoir db." + +cat >"${tmpSql}" <

AQUILENET

- +
- +
- +

Test d'éligibilité par PTO

- + Où trouver mon numéro de PTO ?
@@ -39,10 +39,10 @@ Responsive image
-
+
- +
@@ -54,7 +54,7 @@ - +
@@ -65,7 +65,7 @@ - +

@@ -98,8 +98,8 @@ $("#communes").empty(); communes=data communes.forEach(commune => { - $("#communes").append(""); + $("#communes").append(""); }); } if (communes.length == 1) { @@ -127,14 +127,16 @@ success: function (data, status, xhr) { if (JSON.stringify(data) !== JSON.stringify(voies)){ $("#voies").empty(); - voies=data - voies.forEach(voie => { - $("#voies").append(""); - }); + voies=Object.entries(data); + let voie; + for (i in voies) { + voie = voies[i] + $("#voies").append(""); + } if (voies.length == 1) { $('#btnTestAdresse').collapse('show'); - voie=voies[0].libelle[0] + voie=voies[0].libelle[0] } else { $('#btnTestAdresse').collapse('hide'); } @@ -150,11 +152,11 @@ $('#voieInput').trigger('input') $('#voieInput').trigger('keyup') }); - + $('#communeInput').on('keyup', function () { fillCommunes($(this).val()); }); - + $('#communeInput').on('input', function() { if ($(this).val() === '') { $('#voieForm').collapse('hide'); @@ -168,7 +170,7 @@ $('#btnTestAdresse').collapse('hide'); } }); - + });