From 50dd783b57bee39de2a9b45568afe7b9549f847c Mon Sep 17 00:00:00 2001 From: Johan Le Baut Date: Wed, 26 Jan 2022 15:57:46 +0100 Subject: [PATCH] frontend: enter commune --- address_finder/api.py | 17 ++++++- templates/landing_form.html | 98 ++++++++++++++++++++++++++++--------- templates/style.css | 4 +- webapp.py | 2 +- 4 files changed, 92 insertions(+), 29 deletions(-) diff --git a/address_finder/api.py b/address_finder/api.py index b40d90d..548ff33 100644 --- a/address_finder/api.py +++ b/address_finder/api.py @@ -2,6 +2,7 @@ import sqlite3 import sys import json from .model import Commune,FantoirVoie +import re # DB with addresses info DB_ADDRESSES_PATH_ENV="DB_ADDRESSES_PATH" @@ -30,15 +31,29 @@ class AddressFinder: con.row_factory = sqlite3.Row 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}=\"{communeNameOrZip}\" COLLATE nocase OR {DB_COL_COMMUNE_POSTE}=\"{communeNameOrZip}\"") + 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) con.close() for row in rows: commune=Commune( diff --git a/templates/landing_form.html b/templates/landing_form.html index 4192ca8..26bb0f3 100644 --- a/templates/landing_form.html +++ b/templates/landing_form.html @@ -5,7 +5,7 @@ - + @@ -43,40 +43,90 @@ +
-
+

Test d'éligibilité par adresse

- - +
+ + +
+
+
+ +
- + + diff --git a/templates/style.css b/templates/style.css index a1ff2a2..dbe2f84 100644 --- a/templates/style.css +++ b/templates/style.css @@ -28,9 +28,7 @@ body { #ptoHelp { color: rgb(192, 192, 192); } -.invalid-feedback { - display: block; -} + .sable .btn-ciel, .ciel .btn-sable{ padding: 8px; border-radius: 10px; diff --git a/webapp.py b/webapp.py index 17ffaf3..31f40c9 100644 --- a/webapp.py +++ b/webapp.py @@ -39,12 +39,12 @@ def show_result(): @app.route("/addresses/communes", methods=['GET']) def get_communes(): to_search=request.args.get('s') - print(to_search) communes=addressFinder.getCommunesFromNameOrZip(to_search) response = app.response_class( response=json.dumps(communes), mimetype='application/json' ) + print(response) return response @app.route("/addresses/fantoirvoies/", methods=['GET'])