fantoir voie: limite de recherche et recherche ''

This commit is contained in:
Johan Le Baut 2022-02-12 18:39:08 +01:00
parent 01c8f2b37b
commit 3f7d4b6331
5 changed files with 30 additions and 36 deletions

View File

@ -14,22 +14,17 @@ 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:"
# Utility to find an address
class AddressFinder:
def __init__(self, db_addresses_sqlite_path: str):
self.dbPath = db_addresses_sqlite_path
print("DB addresses Path : " + self.dbPath)
def __init__(self, db_insee_communes_sqlite_path: str, db_fantoir_voies_sqlite_path: str):
self.dbCommunesPath = db_insee_communes_sqlite_path
self.dbFantoirPath = db_fantoir_voies_sqlite_path
print("DB insee communes Path : " + self.dbCommunesPath)
print("DB Fantoir voies Path : " + self.dbFantoirPath)
def getCommunesFromNameOrZip(self, communeNameOrZip: str, limit: int = None) -> list[Commune]:
con = sqlite3.connect(self.dbPath)
con = sqlite3.connect(self.dbCommunesPath)
con.row_factory = sqlite3.Row
cur = con.cursor()
@ -80,25 +75,29 @@ class AddressFinder:
def getCommuneFantoirVoies(self, communeInseeCode: str, voieSearch: str = None, limit: int = None) -> list[FantoirVoie]:
# Extract data from DB
con = sqlite3.connect(self.dbPath)
con = sqlite3.connect(self.dbFantoirPath)
con.row_factory = sqlite3.Row
cur = con.cursor()
if voieSearch is None:
voieSearch=''
# Check if a search limit is specified, make sure it is an integer
select_limit = ""
if limit is not None:
try:
select_limit = f"LIMIT {int(limit)}"
except ValueError:
print("Error, limit arg not a valid int: ", limit)
try:
cur.execute(
f"SELECT trim(libelle), rivoli_with_key from fantoir WHERE full_insee=\"{communeInseeCode}\" AND libelle like \"%{voieSearch}%\"")
f"SELECT trim(libelle), rivoli_with_key from fantoir WHERE full_insee=\"{communeInseeCode}\" AND libelle like \"%{voieSearch}%\" {select_limit}")
except sqlite3.OperationalError as err:
print("Error querying DB : {0}".format(err), file=sys.stderr)
return []
data_raw = cur.fetchall()
con.close()
# Check if a search limit is specified, make sure it is an integer
if limit is not None:
try:
limit = int(limit)
except ValueError:
print("Error, limit arg not a valid int: ", limit)
fantoir_dict = []
# Check if data where found
if data_raw is not None:

View File

@ -6,12 +6,5 @@ class Commune(TypedDict):
codeZip: str
class FantoirVoie(TypedDict):
id: str
dateAjout: int
libelle: list[str]
typeVoie:str
codeCommune: str
codeFantoir: str
cleRivoli: str
nomCommune: str
predecesseur: bool
libelle: str
rivoli_with_key: int

View File

@ -2,11 +2,12 @@ import configparser
class Config:
def __init__(self, username, password, source_addr, db_addresses_sqlite_path):
def __init__(self, username, password, source_addr, db_insee_communes_sqlite_path, db_fantoir_voies_sqlite_path):
self.username = username
self.password = password
self.source_addr = source_addr
self.db_addresses_sqlite_path = db_addresses_sqlite_path
self.db_insee_communes_sqlite_path = db_insee_communes_sqlite_path
self.db_fantoir_voies_sqlite_path = db_fantoir_voies_sqlite_path
self.debug = False
@ -17,5 +18,6 @@ def parse_config(cfgPath):
username = cfg.get("API", "username")
passwd = cfg.get("API", "password")
source_addr = cfg.get("API", "source_addr")
db_addresses_sqlite_path = cfg.get("ADDRESSES","db_addresses_sqlite_path")
return Config(username, passwd, source_addr,db_addresses_sqlite_path)
db_insee_communes_sqlite_path = cfg.get("ADDRESSES","db_insee_communes_sqlite_path")
db_fantoir_voies_sqlite_path = cfg.get("ADDRESSES","db_fantoir_voies_sqlite_path")
return Config(username, passwd, source_addr,db_insee_communes_sqlite_path, db_fantoir_voies_sqlite_path)

View File

@ -52,7 +52,7 @@
<form method="post" action="/test/address">
<div class="form-group" id="communeForm">
<label class="form-label" for="communeInput">Commune</label>
<input type="text" list="communes" name="commune" class="form-control" id="communeInput" ondblclick="this.focus();this.select()" aria-describedby="communeHelp" placeholder="Nom de la commune ou code postal">
<input autocomplete="off" type="text" list="communes" name="commune" 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>
</div>
@ -63,7 +63,7 @@
</div>
<div class="col-sm-9 my-1">
<label class="form-label" for="voieInput">Nom de voie</label>
<input type="text" name="voie" list="voies" class="form-control" id="voieInput" aria-describedby="voieHelp" placeholder="Nom de voie">
<input autocomplete="off" type="text" name="voie" list="voies" class="form-control" id="voieInput" aria-describedby="voieHelp" placeholder="Nom de voie">
<datalist id="voies">
</datalist>
</div>

View File

@ -22,7 +22,7 @@ def load_config():
cfg = load_config()
addressFinder = AddressFinder(cfg.db_addresses_sqlite_path)
addressFinder = AddressFinder(cfg.db_insee_communes_sqlite_path, cfg.db_fantoir_voies_sqlite_path)
app = Flask(__name__)