axione-elig-test/webapp.py

67 lines
2.1 KiB
Python
Raw Normal View History

import os
from flask import Flask, render_template, request, escape
2021-11-02 15:21:34 +01:00
import json
2021-11-02 15:21:34 +01:00
from config.config import parse_config
from axione_api.api import query_axione_pto, parse_response
2021-11-02 15:21:34 +01:00
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()
2021-11-02 15:21:34 +01:00
addressFinder = AddressFinder(cfg.db_addresses_sqlite_path)
app = Flask(__name__)
@app.route("/", methods=['GET'])
def get_form():
return render_template("landing_form.html")
2021-11-02 15:21:34 +01:00
@app.route("/addresses/communes", methods=['GET'])
def get_communes():
to_search=request.args.get('s')
2021-11-10 23:22:46 +01:00
communes=addressFinder.getCommunesFromNameOrZip(to_search)
2021-11-02 15:21:34 +01:00
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):
2022-01-27 23:53:19 +01:00
to_search=request.args.get('s')
fantoirVoies=addressFinder.getCommuneFantoirVoies(codeInsee,to_search)
2021-11-02 15:21:34 +01:00
response = app.response_class(
response=json.dumps(fantoirVoies),
mimetype='application/json'
)
2022-01-27 23:53:19 +01:00
return response
@app.route("/test/address", methods=['POST'])
def test_address():
2022-01-28 10:26:34 +01:00
commune = escape(request.form['commune'])
numeroVoie = escape(request.form['numeroVoie'])
voie = escape(request.form['voie'])
2022-01-27 23:53:19 +01:00
# result = parse_response(query_axione_pto(cfg, pto))
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
@app.route("/test/pto", methods=['POST'])
def test_pto():
2022-01-28 10:26:34 +01:00
pto = escape(request.form['pto'])
result = parse_response(query_axione_pto(cfg, pto))
return render_template("result.html", pto=pto, result=result)