axione-elig-test/webapp.py
2022-02-13 12:12:58 +01:00

74 lines
2.4 KiB
Python

import os
from flask import Flask, render_template, request, escape
import json
from config.config import parse_config
from axione_api.api import query_axione_pto, parse_response, query_axione_fantoir
from address_finder.api import AddressFinder
def load_config():
cfg_path = os.environ.get("CONFIG", "/etc/ftth-elig/conf.ini")
print(f'Reading the "{cfg_path}" config file')
cfg = parse_config(cfg_path)
cfg.debug = True if "DEBUG" in os.environ else False
if cfg.debug:
print("===================")
print("DEBUG_MODE")
print("No requests will be performed")
print("We'll inject some dummy data instead")
print("===================")
print("")
return cfg
cfg = load_config()
addressFinder = AddressFinder(cfg.db_insee_communes_sqlite_path, cfg.db_fantoir_voies_sqlite_path)
app = Flask(__name__)
@app.route("/", methods=['GET'])
def get_form():
return render_template("landing_form.html")
@app.route("/addresses/communes", methods=['GET'])
def get_communes():
to_search=request.args.get('s')
limit=request.args.get('limit')
communes=addressFinder.getCommunesFromNameOrZip(to_search,limit)
response = app.response_class(
response=json.dumps(communes),
mimetype='application/json'
)
return response
@app.route("/addresses/fantoirvoies/<codeInsee>", methods=['GET'])
def get_fantoir_voies(codeInsee):
to_search=request.args.get('s')
limit=request.args.get('limit')
fantoirVoies=addressFinder.getCommuneFantoirVoies(codeInsee,to_search,limit)
response = app.response_class(
response=json.dumps(fantoirVoies),
mimetype='application/json'
)
return response
@app.route("/test/address", methods=['POST'])
def test_address():
codeInsee = escape(request.form['codeInsee'])
numeroVoie = escape(request.form['numeroVoie'])
# Trimming rivoli's key
codeRivoli = escape(request.form['codeRivoli'])[:-1]
result = parse_response(query_axione_fantoir(cfg, codeInsee, codeRivoli, numeroVoie))
if cfg.debug:
print("===================")
print("Dummy response: ")
print(result)
print("===================")
return render_template("result.html", pto="", result=result)
@app.route("/test/pto", methods=['POST'])
def test_pto():
pto = escape(request.form['pto'])
result = parse_response(query_axione_pto(cfg, pto))
return render_template("result.html", pto=pto, result=result)