-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
143 lines (109 loc) · 4.54 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
AGPL-3.0 License
Author: Francisco Javier Gañán
This is the entry point of the web app.
All the modules are imported to and accessed from this file.
"""
from flask import (
current_app,
render_template,
request,
redirect,
jsonify,
send_from_directory,
Flask,
)
from flask_babel import Babel, gettext
from utils.problem_definiton import AddressFormatConversion
from app.routes.addresses import addresses_blueprint
from app.routes.loaders import loaders_blueprint
from app.routes.save import save_blueprint
from app.routes.routes import routes_blueprint
from app.routes.simulation import simulation_blueprint
app = Flask(__name__, template_folder="app/templates", static_folder="app/static")
app.config.from_object("app.config.Config")
# To get the best matching language
def get_locale():
"""
Get the locale that best matches the user's preferred language.
This function uses the `accept_languages` attribute from the `request`
object to determine the best matching locale based on the user's preferred language.
It compares the user's preferred language against the available languages defined
in the `LANGUAGES` configuration dictionary.
Returns:
str: The locale that best matches the user's preferred language."""
return request.accept_languages.best_match(["en"]) # app.config["LANGUAGES"].keys()
babel = Babel(app, locale_selector=get_locale)
# Blueprint for addresses related routes
app.register_blueprint(addresses_blueprint, url_prefix="")
# Blueprint for loaders
app.register_blueprint(loaders_blueprint, url_prefix="")
# Blueprint for routes to save information
app.register_blueprint(save_blueprint, url_prefix="")
# Blueprint for routes to generate routes
app.register_blueprint(routes_blueprint, url_prefix="")
# Blueprint for routes to manage simulation
app.register_blueprint(simulation_blueprint, url_prefix="")
@app.route("/images/<filename>")
def custom_image(filename):
"""
This function retrieves and returns a custom image file from the "app/static/images" directory.
Args:
filename (str): The name of the image file to be retrieved.
Returns:
str: The retrieved custom image file.
Example:
>>> custom_image("image.jpg")"""
return send_from_directory("app/static/images", filename)
@app.route("/reset", methods=["DELETE"])
def reset():
"""
Resets the problem definition, solver definiton, and routes.
This function resets the problem definition, solver definition,
and routes by clearing the respective data structures.
It sets the 'addresses', 'start_nodes', 'end_nodes' to empty lists, and 'n_vehicles' to 1.
It also sets the 'simulation' flag to False.
Returns:
jsonify: A JSON response indicating the success of the reset operation."""
current_app.problem_data = {} # Reset problem definition
current_app.solver_data = {} # Reset solver definiton
current_app.routes = {} # Reset the routes
current_app.problem_data["addresses"] = []
current_app.problem_data["start_nodes"] = []
current_app.problem_data["end_nodes"] = []
current_app.problem_data["n_vehicles"] = 1
current_app.simulation = False
return jsonify(success=True, message=gettext("Reset done"))
@app.route("/maps-api-key", methods=["GET"])
def get_maps_api_key():
"""
Gets the Google Maps API KEY from the configuration file.
"""
return jsonify({"apiKey": app.config["API_KEY"]})
@app.route("/", methods=["POST", "GET"])
def main():
"""
Initialize the main function of the app.
This function initializes variables global to the app context, sets the map center coordinates,
sets up an AddressFormatConversion object, and calls the reset function to initialize variables.
When a POST request is made, the function redirects to the root URL.
If an error occurs, it returns an error message.
Otherwise, it renders the index.html template with the map center coordinates and the API key.
Args:
None
Returns:
None"""
current_app.map_center = [
[37.38602323347123, -5.99311606343879]
] # Center of the map
current_app.converter = AddressFormatConversion(
app.config["API_KEY"],
app.config["GEOCODE_API_URL"],
app.config["DISTANCE_MATRIX_API_URL"],
)
reset() # Initialize variables
if request.method == "POST":
return redirect("/")
return render_template("index.html", coordinates=current_app.map_center)
if __name__ == "__main__":
app.run(host=app.config["APP_HOST"], port=app.config["APP_PORT"])