forked from qalearning/airportsv1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
62 lines (51 loc) · 1.88 KB
/
application.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
from flask import Flask
from flask import abort
from flask import g
from flask import render_template
import db
import time
from geopy.distance import vincenty
database = db.DB()
application = Flask(__name__)
query_count = 0
@application.before_request
def before_request():
g.start = time.time()
@application.after_request
def after_request(response):
global query_count
diff_seconds = time.time() - g.start
diff_miliseconds = diff_seconds * 1000
if (response.response.__class__ is list):
diag = "Execution time: %.2fms | Database queries: %s" % (diff_miliseconds ,query_count)
new_response = response.response[0].replace('__DIAGNOSTICS__', diag)
response.set_data(new_response)
return response
@application.route("/")
def home():
query_increment()
cities = database.query("SELECT id, name, icon, latitude, longitude from city;")
return render_template('main.html', cities=cities)
@application.route("/<city_id>")
def city(city_id):
query_increment()
cities = database.query("SELECT id, name, icon, latitude, longitude from city;")
city = filter(lambda c: c['id'] == city_id, cities)
if len(city) < 1:
abort(404)
latlng = (city[0]['latitude'], city[0]['longitude'])
query_increment()
airports = database.query("SELECT name, latitude, longitude from airport;")
for airport in airports:
airport['distance'] = vincenty((airport['latitude'], airport['longitude']), latlng).miles
closest = sorted(airports, key=lambda x: x['distance'])[:5]
return render_template('main.html', cities=cities, airports=closest)
def query_increment():
global query_count
query_count = query_count + 1
# run the app.
if __name__ == "__main__":
# Setting debug to True enables debug output. This line should be
# removed before deploying a production app.
application.debug = True
application.run()