-
Notifications
You must be signed in to change notification settings - Fork 0
/
busboard.py
278 lines (206 loc) · 7.61 KB
/
busboard.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import time
import datetime
from flask import Flask, jsonify, request, session, g, redirect, url_for, \
render_template, flash
import requests as outbound
import xml.etree.ElementTree as ET
#Create the app
app = Flask(__name__)
app.config.from_object('busboard_config')
# Now that we've got config, get models
from Models import Member, Route, Stop, HotStop, RouteStopAssociation, \
Direction, RouteDirectionAssociation, database
database.init(app.config['DATABASE'], user=app.config['USERNAME'],
passwd=app.config['PASSWORD'])
database.connect()
def create_tables():
Member.create_table()
Route.create_table()
Stop.create_table()
HotStop.create_table()
RouteStopAssociation.create_table()
def build_nextrip_routes():
url = app.config['NEXTRIP_BASE_URL'] + 'Routes'
response = outbound.get(url)
root = ET.fromstring(response.text)
for nt_route in root:
Route.create(providerid=nt_route.findtext(app.config['NEXTRIP_XML_PREFIX'] + 'ProviderID'),
route=nt_route.findtext(app.config['NEXTRIP_XML_PREFIX'] + 'Route'),
description=nt_route.findtext(app.config['NEXTRIP_XML_PREFIX'] + 'Description'))
return response.text
@app.route('/build_stops')
def build_stops_for_routes():
routes = Route.select()
for route in routes:
rda = RouteDirectionAssociation.select().where(
RouteDirectionAssociation.route == route
)
url = app.config['NEXTRIP_BASE_URL'] + 'Stops/' \
+ route.route + '/' \
+ str(rda[0].direction.value)
print url
# Get the stops
stop_response = outbound.get(url)
xml = ET.fromstring(stop_response.text)
for child in xml:
abbr = child.findtext(app.config['NEXTRIP_XML_PREFIX'] + 'Value')
name = child.findtext(app.config['NEXTRIP_XML_PREFIX'] + 'Text')
try:
stop = Stop.get(Stop.abbr == abbr)
except Stop.DoesNotExist:
stop = Stop.create(abbr=abbr, name=name)
RouteStopAssociation.create(route=route, stop=stop)
return 'Yay!'
@app.route('/build_directions')
def build_directions_for_routes():
routes = Route.select()
for route in routes:
url = app.config['NEXTRIP_BASE_URL'] + 'Directions/' + route.route
d_response = outbound.get(url)
xml = ET.fromstring(d_response.text)
for child in xml:
value = child.findtext(app.config['NEXTRIP_XML_PREFIX'] + 'Value')
direction = Direction.get(Direction.value == value)
RouteDirectionAssociation.create(route=route, direction=direction)
if not Member.table_exists():
create_tables()
def get_template_vars():
return g.template_vars
@app.before_request
def before_request():
g.db = database
g.db.connect()
g.template_vars = {'logged_in': False}
if session.get('member_id'):
g.template_vars['logged_in'] = True
@app.after_request
def after_request(response):
g.db.close()
return response
def get_member_from_credentials(form):
try:
member = Member.get((Member.username == form['username'])
& (Member.password == form['password']))
print form['username']
print form['password']
return member
except Member.DoesNotExist:
return False
@app.route('/')
@app.route('/login/', methods=['GET', 'POST'])
def login():
if session.get('member_id'):
return redirect(url_for('show_hotstops'))
message = 'Welcome to HotStop!'
if request.method == 'POST':
member = get_member_from_credentials(request.form)
print member
if member:
session['member_id'] = member.id
message = 'Welcome back, ' + member.username
return redirect(url_for('show_hotstops'))
else:
message = 'We could not find an account with those credentials.'
return redirect(url_for('update_member'))
print request.method
flash(message)
return render_template('login.html', vars=get_template_vars())
@app.route('/member/', methods=['GET', 'POST', 'PUT', 'DELETE'])
def update_member():
print request.method
if request.method == 'POST':
# Is this seat taken?
try:
member = Member.get(
Member.username == request.form['username'])
message = "The username '" + member.username + \
"' is already in use."
destination = 'update_member'
#Create a new member
except Member.DoesNotExist:
new_member = Member()
for field, value in request.form.items():
setattr(new_member, field, value)
new_member.save()
message = 'Welcome to the party, ' + new_member.username
destination = 'login'
flash(message)
return redirect(url_for(destination))
elif request.method == 'GET':
return render_template('member.html', vars=g.template_vars)
@app.route('/hotstops/', methods=['GET', 'POST'])
@app.route('/hotstops/<int:hs_id>', methods=['DELETE', 'PUT'])
def show_hotstops(hs_id=None):
print hs_id
# No riff-raff
if not session.get('member_id'):
return redirect(url_for('login'))
# Show the goods
if request.method == 'GET':
hotstops = HotStop.select().where(
HotStop.member == session.get('member_id'))
if (hotstops.exists()):
hs_list = []
for hs in hotstops:
hs_list.append(hs.to_display_dict())
print hs_list
else:
hs_list = []
g.template_vars['title'] = 'HotStops'
g.template_vars['hotstops'] = hs_list
if request.is_xhr:
return jsonify({'hotstops': hs_list})
return render_template('hotstops.html', vars=get_template_vars())
elif request.method == 'POST':
# Add a HotStop for the logged-in member
new_hs = HotStop()
new_hs.member = Member.get(Member.id == session.get('member_id'))
for field, value in request.form.items():
setattr(new_hs, field, value)
new_hs.added_on = datetime.datetime.now()
new_hs.save()
hs_list = []
for hs in new_hs.member.hotstops:
hs_list.append(hs.to_display_dict())
return jsonify({'hotstops': hs_list})
elif request.method == 'DELETE':
query = HotStop.delete().where(HotStop.id == hs_id)
query.execute()
return jsonify({'deleted': hs_id})
else:
# Update
pass
@app.route('/routes/')
def routes():
routes = Route.select().dicts()
j_routes = {}
for route in routes:
del route['updated']
j_routes[str(route['id'])] = route
return jsonify(**j_routes)
@app.route('/stops/<int:route_id>/<int:direction>')
def stops(route_id, direction):
stops = Stop.select().dicts()\
.join(RouteStopAssociation)\
.join(Route)\
.where(Route.id == route_id)\
# Probably need to rebuild RSA table for ordering.
j_stops = {}
for stop in stops:
del stop['updated']
j_stops[str(stop['id'])] = stop
return jsonify(**j_stops)
@app.route('/directions/<int:route_id>')
def directions(route_id):
route = Route.get(Route.id == route_id)
avail_directions = route.available_directions()
j_directions = {}
for direction in avail_directions:
j_directions[str(direction['id'])] = direction
return jsonify(**j_directions)
@app.route('/logout')
def logout():
session.pop('member_id', None)
return redirect(url_for('login'))
if __name__ == '__main__':
app.run()