-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
126 lines (106 loc) · 3.69 KB
/
app.py
File metadata and controls
126 lines (106 loc) · 3.69 KB
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
"""
Flask routing module to generate pages and API endpoints
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import json
import os
from flask import Flask, jsonify, make_response, render_template, request
from models import db, Stop
app = Flask(__name__)
if os.environ.get('DBASE_URL'):
db_url = os.environ.get('DBASE_URL')
else:
db_url = 'sqlite:///{p}'.format(
p=os.path.join(os.path.dirname(__file__), 'stops.sqlite')
)
app.config['SQLALCHEMY_DATABASE_URI'] = db_url
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
@app.route('/', methods=['GET', 'POST'])
def index():
"""App route function for homepage."""
return render_template('index.html')
@app.route('/about')
def about():
"""App route function for about page."""
return render_template('about.html')
@app.route('/universities')
def universities():
"""App route function for universities page."""
return render_template('universities.html')
@app.route('/universities/data')
def cleaner_universities():
"""Provide endpoint for data/cleaner_universities.json."""
with open(os.path.join(os.path.dirname(__file__), 'data', 'cleaner_universities.json')) as f:
data = json.load(f)
return jsonify(data)
@app.route('/sw.js')
def sworker():
"""Serve the app's service worker from here."""
with open(os.path.join(os.path.dirname(__file__), 'sw.js')) as f:
contents = f.read()
resp = make_response(contents)
resp.mimetype = 'text/javascript'
return resp
@app.route('/stops', methods=['GET'])
def get_stops():
"""Get stops given latitude and longitude."""
keys = ('lat', 'lon')
missing = []
user_info = {}
for k in keys:
try:
user_info[k] = float(request.args.get(k))
except Exception as excp:
if k not in request.args:
missing.append(k)
print(excp)
user_info[k] = None
if any(not v for v in user_info.values()):
message = 'Coordinates could not be parsed properly.'
if missing:
if len(missing) == 1:
message += ' {m} is missing from the query string.'.format(m=' and '.join(missing))
else:
message += ' Both {m} are missing from the query string.'.format(
m=' and '.join(missing))
message += ' Make sure you\'re using the query stops?lat=value&lon=value.'
message += ' Also, the values should be floating point values.'
return jsonify(**{
'status': 'bad',
'message': message,
'stops': []
})
stops = []
for stop in Stop.query.all():
if stop.within_distance(user_info) and stop.wheelchair_boarding == 1:
stops.append(stop.serialize())
status = 'good' if stops else 'bad'
if not stops:
message = 'Could not find stops for the given coordinates.'
else:
message = '{cnt} stops found'.format(cnt=len(stops))
return jsonify(**{
'status': status,
'message': message,
'stops': stops
})
@app.route('/stop/<stop_id>', methods=['GET'])
def get_stop(stop_id):
"""Get an MBTA stop by its assigned ID, such as /stop/5."""
match = [stop.serialize() for stop in Stop.query.filter_by(stop_id=stop_id).all()]
status = 'good' if match else 'bad'
if match:
message = 'A stop matching the given ID is found'
else:
message = 'No stop found for the provided ID.'
return jsonify(**{
'status': status,
'message': message,
'stops': match
})
if __name__ == '__main__':
app.run(
host=os.environ.get('APP_HOST') or '0.0.0.0',
port=os.environ.get('APP_PORT') or 5000
)