Axione-IPE-Viewer/webapp/main.py

115 lines
3.5 KiB
Python
Raw Normal View History

import configparser
import os
from typing import TypedDict
from flask import Flask, request, render_template, redirect
2023-03-06 15:10:55 +01:00
from eligibility_api.api_doc import start_swagger
2022-11-16 16:10:30 +01:00
from eligibility_api.elig_api_exceptions import FlaskExceptions
from eligibility_api.elig_api_routes import EligibilityApiRoutes
from ipe_fetcher import Liazo, Axione, Arcep, AreaCoordinates
2023-02-28 22:31:35 +01:00
from coordinates import adapt_coordinates_to_max_area, check_coordinates_args
2023-03-03 14:16:34 +01:00
from netwo.netwo import Netwo
2022-11-16 16:10:30 +01:00
2023-01-11 15:35:12 +01:00
LIAZO_MAX_X_INTERVAL = 0.0022
LIAZO_MAX_Y_INTERVAL = 0.0011
LIAZO_MAX_AREA = LIAZO_MAX_X_INTERVAL * LIAZO_MAX_Y_INTERVAL
2022-11-16 16:10:30 +01:00
2023-02-28 22:31:35 +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
2023-03-03 14:16:34 +01:00
netwo_api_key: str
2023-03-06 15:53:31 +01:00
netwo_aquilenet_fixed_recurring_price: float
2022-03-23 22:54:04 +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"),
2023-03-03 14:16:34 +01:00
"netwo_api_key": cfg.get("NETWO", "api_key"),
2023-03-06 15:53:31 +01:00
"netwo_aquilenet_fixed_recurring_price": float(
cfg.get("NETWO", "aquilenet_fixed_recurring_price") or 0.00
),
2023-02-28 22:31:35 +01:00
}
2022-03-23 22:54:04 +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()
2023-03-06 15:53:31 +01:00
netwo = Netwo(
cfg.get("netwo_api_key"), cfg.get("netwo_aquilenet_fixed_recurring_price")
)
2023-03-03 14:16:34 +01:00
elig_api_routes = EligibilityApiRoutes(app, axione, netwo)
2022-11-16 16:10:30 +01:00
elig_api_routes.add_routes()
elig_api_exceptions = FlaskExceptions(app)
elig_api_exceptions.add_exceptions()
2023-02-28 22:31:35 +01:00
@app.route("/", methods=["GET"])
def getMap():
return render_template("map.html")
2022-03-23 22:54:04 +01:00
@app.route("/eligdata", methods=["GET"])
def getEligData():
2023-02-28 22:31:35 +01:00
toto = 1
args = request.args
valid_args = True
processed_args = {}
2023-01-11 15:35:12 +01:00
try:
processed_args = check_coordinates_args(args)
except ValueError:
valid_args = False
2023-01-11 15:35:12 +01:00
if valid_args:
2023-02-28 22:31:35 +01:00
coordinates = adapt_coordinates_to_max_area(processed_args, LIAZO_MAX_AREA)
2022-03-23 22:54:04 +01:00
buildings = dict()
try:
2023-02-28 22:31:35 +01:00
buildings = arcep.get_area_buildings(coordinates, buildings)
buildings = axione.get_area_buildings(coordinates, buildings)
2022-03-23 22:54:04 +01:00
except ValueError as err:
print("Could not get Axione data for this area:", err)
2023-02-28 22:31:35 +01:00
buildings = liazo.get_area_buildings(coordinates, buildings)
2022-03-23 22:54:04 +01:00
2023-01-11 15:35:12 +01:00
return {"buildings": list(buildings.values())}
else:
return "Invalid bounding box coordinates", 400
2022-04-13 11:04:48 +02:00
2023-01-11 15:35:12 +01:00
@app.route("/eligdata/bounds", methods=["GET"])
def getEligDataBounds():
args = request.args
try:
processed_args = check_coordinates_args(args)
2023-02-28 22:31:35 +01:00
return {"bounds": adapt_coordinates_to_max_area(processed_args, LIAZO_MAX_AREA)}
2023-01-11 15:35:12 +01:00
except ValueError:
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
2023-02-28 22:31:35 +01:00
idImm = args["idImm"]
codePostal = args["codePostal"]
axioneOk = args["axione"]
liazoOk = args["liazo"]
2022-04-13 11:04:48 +02:00
pto_url = f"https://tools.aquilenet.fr/cgi-bin/recherchepto.cgi?refimmeuble={idImm}&cp={codePostal}&axione={axioneOk}&liazo={liazoOk}"
return redirect(pto_url)
2023-03-06 15:10:55 +01:00
# start_swagger(app)