2022-04-13 11:04:48 +02:00
|
|
|
from flask import Flask, request, render_template, redirect
|
2022-02-21 15:28:19 +01:00
|
|
|
|
2022-02-22 12:48:50 +01:00
|
|
|
from typing import TypedDict
|
|
|
|
import configparser
|
|
|
|
import sqlite3
|
|
|
|
import os
|
2022-04-12 23:17:40 +02:00
|
|
|
from ipe_fetcher import Liazo,Axione,Arcep,AreaCoordinates
|
2022-11-16 16:10:30 +01:00
|
|
|
from eligibility_api.elig_api_exceptions import FlaskExceptions
|
|
|
|
from eligibility_api.elig_api_routes import EligibilityApiRoutes
|
|
|
|
|
|
|
|
|
2022-02-22 12:48:50 +01:00
|
|
|
class Config(TypedDict):
|
2022-03-23 22:54:04 +01:00
|
|
|
axione_ipe_path: str
|
2022-04-12 23:17:40 +02:00
|
|
|
axione_ipe_db_name: str
|
|
|
|
arcep_ipe_path: str
|
|
|
|
arcep_ipe_db_name: str
|
2022-03-23 22:54:04 +01:00
|
|
|
|
2022-02-22 12:48:50 +01:00
|
|
|
|
|
|
|
def parseConfig() -> Config:
|
2022-03-23 22:54:04 +01:00
|
|
|
cfg_path = os.environ.get("CONFIG", "/etc/ftth-ipe-map/conf.ini")
|
|
|
|
cfg = configparser.ConfigParser()
|
|
|
|
with open(cfg_path, "r") as f:
|
|
|
|
cfg.read_file(f)
|
2022-04-12 20:39:33 +02:00
|
|
|
return {
|
|
|
|
"axione_ipe_path": cfg.get("DB", "axione_ipe_path"),
|
2022-04-12 23:17:40 +02:00
|
|
|
"axione_ipe_db_name": cfg.get("DB", "axione_ipe_db_name"),
|
|
|
|
"arcep_ipe_path": cfg.get("DB", "arcep_ipe_path"),
|
|
|
|
"arcep_ipe_db_name": cfg.get("DB", "arcep_ipe_db_name"),
|
2022-04-12 20:39:33 +02:00
|
|
|
}
|
2022-03-23 22:54:04 +01:00
|
|
|
|
2022-02-22 12:48:50 +01:00
|
|
|
|
2022-02-21 15:28:19 +01:00
|
|
|
app = Flask(__name__)
|
2022-03-23 22:54:04 +01:00
|
|
|
cfg: Config = parseConfig()
|
|
|
|
|
2022-04-12 23:17:40 +02:00
|
|
|
axione = Axione(cfg.get("axione_ipe_path"), cfg.get("axione_ipe_db_name"))
|
|
|
|
arcep = Arcep(cfg.get("arcep_ipe_path"), cfg.get("arcep_ipe_db_name"))
|
2022-03-23 22:54:04 +01:00
|
|
|
liazo = Liazo()
|
2022-11-16 16:10:30 +01:00
|
|
|
elig_api_routes = EligibilityApiRoutes(app, axione)
|
|
|
|
elig_api_routes.add_routes()
|
|
|
|
elig_api_exceptions = FlaskExceptions(app)
|
|
|
|
elig_api_exceptions.add_exceptions()
|
2022-02-21 15:28:19 +01:00
|
|
|
|
2022-02-22 12:48:50 +01:00
|
|
|
@app.route("/", methods=["GET"])
|
2022-02-21 15:28:19 +01:00
|
|
|
def getMap():
|
|
|
|
return render_template("map.html")
|
2022-02-21 17:21:21 +01:00
|
|
|
|
2022-03-23 22:54:04 +01:00
|
|
|
|
2022-02-22 12:48:50 +01:00
|
|
|
@app.route("/eligdata", methods=["GET"])
|
2022-02-21 17:21:21 +01:00
|
|
|
def getEligData():
|
|
|
|
args = request.args
|
2022-02-22 12:48:50 +01:00
|
|
|
valid_args = True
|
|
|
|
processed_args = {}
|
2022-10-14 13:56:31 +02:00
|
|
|
for k in ["swx", "swy", "nex", "ney"]:
|
2022-02-22 12:48:50 +01:00
|
|
|
valid_args = valid_args and k in args
|
|
|
|
if valid_args:
|
|
|
|
try:
|
|
|
|
processed_args[k] = float(args[k])
|
|
|
|
except ValueError:
|
|
|
|
valid_args = False
|
|
|
|
if valid_args:
|
2022-10-14 13:56:31 +02:00
|
|
|
# Need to narrow coordinates for Liazo API call
|
|
|
|
|
|
|
|
# computes center
|
|
|
|
centerx = (processed_args['swx'] + processed_args['nex']) / 2
|
|
|
|
centery = (processed_args['swy'] + processed_args['ney']) / 2
|
|
|
|
|
|
|
|
narrow_x = 0.0022
|
|
|
|
narrow_y = 0.0011
|
|
|
|
|
|
|
|
narrow_coordinates = AreaCoordinates(
|
|
|
|
swx=centerx - narrow_x,
|
|
|
|
swy=centery - narrow_y,
|
|
|
|
nex=centerx + narrow_x,
|
|
|
|
ney=centery + narrow_y,
|
2022-04-12 23:17:40 +02:00
|
|
|
)
|
2022-03-23 22:54:04 +01:00
|
|
|
buildings = dict()
|
|
|
|
try:
|
2022-04-12 23:17:40 +02:00
|
|
|
buildings = arcep.getAreaBuildings(narrow_coordinates, buildings)
|
|
|
|
buildings = axione.getAreaBuildings(narrow_coordinates, buildings)
|
2022-03-23 22:54:04 +01:00
|
|
|
except ValueError as err:
|
|
|
|
print("Could not get Axione data for this area:", err)
|
|
|
|
|
2022-04-12 23:17:40 +02:00
|
|
|
buildings = liazo.getAreaBuildings(narrow_coordinates, buildings)
|
2022-03-23 22:54:04 +01:00
|
|
|
|
|
|
|
return {"buildings": buildings}
|
2022-02-21 17:21:21 +01:00
|
|
|
else:
|
2022-02-22 12:48:50 +01:00
|
|
|
return "Invalid bounding box coordinates", 400
|
2022-04-13 11:04:48 +02:00
|
|
|
|
|
|
|
@app.route("/eligtest/ftth", methods=["GET"])
|
|
|
|
def testFtth():
|
|
|
|
args = request.args
|
|
|
|
idImm=args['idImm']
|
|
|
|
codePostal=args['codePostal']
|
|
|
|
axioneOk=args['axione']
|
|
|
|
liazoOk=args['liazo']
|
|
|
|
pto_url = f"https://tools.aquilenet.fr/cgi-bin/recherchepto.cgi?refimmeuble={idImm}&cp={codePostal}&axione={axioneOk}&liazo={liazoOk}"
|
|
|
|
return redirect(pto_url)
|