-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
657 lines (542 loc) · 22.3 KB
/
server.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
from collections import defaultdict
import json
import os
from functools import wraps
from time import time
import arrow
import flask
import gspread
import httplib2
from apiclient import discovery
from oauth2client import client
from ecessprivate.ecessdb import APP_CLIENT_ID, APP_CLIENT_SECRET
from ecessdb import get_drive_conn
app = flask.Flask(__name__)
SCOPE_USEREMAIL = "userinfo.email"
SCOPE_DRIVE = "drive"
TYPE_USER = "user"
TYPE_EDITOR = "editor"
SCOPES = {
TYPE_USER: [SCOPE_USEREMAIL],
TYPE_EDITOR: [SCOPE_DRIVE, 'https://spreadsheets.google.com/feeds',
SCOPE_USEREMAIL]
}
class SessKeys(object):
post_auth_redirect = "post_auth_redirect"
usertypes = "usertypes"
credentials = "credentials"
def authenticated(*usertypes):
"""Decorator for authentication with Google OAuth2
:param list usertype: Usertypes
"""
def oauthorized2(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
flask.session[SessKeys.post_auth_redirect] = flask.request.path
if SessKeys.usertypes not in flask.session:
flask.session[SessKeys.usertypes] = []
for usertype in usertypes:
if usertype not in flask.session[SessKeys.usertypes]:
flask.session[SessKeys.usertypes].append(usertype)
if SessKeys.credentials not in flask.session:
return flask.redirect(flask.url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(
flask.session[SessKeys.credentials])
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
return fn(credentials, *args, **kwargs)
return wrapped
return oauthorized2
def get_db(cache_period=180):
# top = flask._app_ctx_stack
# if not hasattr(top, 'drive_conn'):
# top.drive_conn = time(), get_drive_conn()
#
# t, gc = top.drive_conn
# if time() - t > cache_period:
# top.drive_conn = time(), get_drive_conn()
#
# return top.drive_conn[1]
return get_drive_conn()
def get_spreadsheet_fromsvc(name, cache_period=120):
return _get_spreadsheet(name, cache_period, gc=None)
def get_spreadsheet_fromusr(name, gc, cache_period=120):
return _get_spreadsheet(name, cache_period, gc)
def _get_spreadsheet(name, cache_period, gc=None):
"""Grabs and returns worksheet1 for given workbook name
Caches workbook (connection) for cache_period
:param gc: Must be provided when not using service credentials
to fetch a resource. If this is None, service credentials
will be used!
"""
# Disable caching for the moment because if connections get
# killed and the old worksheet still uses it, CannotSendRequest happens
# def get_sheet(top, gc):
# print("Fetching workbook {}...".format(name))
# gc = get_db() if gc is None else gc
# wks = gc.open(name).sheet1
# top.sheets[name] = (time(), wks)
#
# top = flask._app_ctx_stack
# if not hasattr(top, 'sheets'):
# top.sheets = {}
#
# if name not in top.sheets:
# get_sheet(top, gc)
# else:
# t, wks = top.sheets[name]
# if time() - t > cache_period:
# get_sheet(top, gc)
#
# return top.sheets[name][1]
gc = get_db() if gc is None else gc
wks = gc.open(name).sheet1
return wks
class NonUniqueIndexError(Exception):
pass
def sheet2dict(sheet, index_key, lower=True):
d = {}
rkeys = dict(enumerate(sheet.row_values(1)))
keys = sheet.row_values(1)
if index_key not in rkeys.values():
raise KeyError("{} does not exist in {}".format(index_key, sheet.title))
for entry in sheet.get_all_values()[1:]:
pk_val = entry[[i for i,key in enumerate(keys) if key==index_key][0]]
if pk_val in d:
raise NonUniqueIndexError(pk_val)
d[pk_val.lower() if lower else pk_val] = dict(zip(keys, entry))
return d
def sheet2lod(sheet):
keys = sheet.row_values(1)
return [dict(zip(keys, entry)) for entry in sheet.get_all_values()[1:]]
def _get_service(api, version, credentials):
http_auth = credentials.authorize(httplib2.Http())
service = discovery.build(api, version, http_auth)
return service
def get_drive_service(credentials):
return _get_service('drive', 'v2', credentials)
def get_plus_service(credentials):
return _get_service('plus', 'v1', credentials)
def get_oauth2_service(credentials):
return _get_service('oauth2', 'v2', credentials)
# @app.route('/')
# @authenticated(TYPE_USER)
# def index(credentials):
# # drive_service = get_drive_service(credentials)
# # files = drive_service.files().list().execute()
# # return json.dumps(get_plus_service(credentials).people().get(userId="me").execute())
# oauth2_service = get_oauth2_service(credentials)
# return json.dumps(oauth2_service.userinfo().get().execute())
@app.route('/')
def index():
return "This is an index page. If you were trying to do something" \
" else but ended up here, please email [email protected]."
@app.route('/student/register')
@authenticated(TYPE_USER)
def student_register(credentials):
FORM_URL = "https://docs.google.com/forms/d/" \
"1TUjrEqJbVIMILbItA8WG1vSIhL5VNTn3-H7sQfqzJdY/" \
"viewform?entry.511477521={google_email}"
oauth2_service = get_oauth2_service(credentials)
google_email = oauth2_service.userinfo().get().execute()["email"]
return flask.redirect(FORM_URL.format(google_email=google_email))
def _wkskeys(wks):
return {v: k for k, v in enumerate(wks.row_values(1))}
def _get_free_lockers():
lockers = get_spreadsheet_fromsvc("Lockers", cache_period=120)
lockers_keys = _wkskeys(lockers)
locker_sales = get_spreadsheet_fromsvc("Locker_Rentals", cache_period=30)
locker_sales_keys = _wkskeys(locker_sales)
rentable = {entry[lockers_keys["Number"]] for entry in
lockers.get_all_values()[1:]
if entry[lockers_keys["Type"]] == "Rentable"}
all_rentable = rentable.copy()
used_locker_numbers = set()
doubly_used = []
invalid_entries = []
for entry in locker_sales.get_all_values()[1:]:
locker_number = entry[locker_sales_keys["Locker_Number"]]
if locker_number in used_locker_numbers:
doubly_used.append(entry)
elif locker_number and locker_number not in all_rentable:
invalid_entries.append(entry)
if (
locker_number in rentable and
entry[locker_sales_keys["Locker_Number"]] != "Yes"
):
rentable.remove(locker_number)
used_locker_numbers.add(locker_number)
res = list(sorted(rentable, key=lambda x: int(x)))
res.extend([
"<br><br>"
"Doubly-used locker numbers",
])
res.extend(doubly_used)
res.extend([
"<br><br>",
"Invalid entries",
])
res.extend(invalid_entries)
return "\n<br>".join(map(str, res))
def _cache_free_lockers(cache_period=30):
top = flask._app_ctx_stack
if not hasattr(top, 'free_lockers'):
top.free_lockers = None
if top.free_lockers is None or time() - top.free_lockers[0] > cache_period:
top.free_lockers = time(), _get_free_lockers()
return top.free_lockers[1]
@app.route('/student/availablelockers')
def available_lockers():
return _cache_free_lockers()
@app.route('/student/seattle/signup')
@authenticated(TYPE_USER)
def seattle_signup(credentials):
oauth2_service = get_oauth2_service(credentials)
google_email = oauth2_service.userinfo().get().execute()["email"]
not_registered = _check_not_registered(google_email)
if not_registered is not None:
return not_registered
FORM_URL = "https://docs.google.com/forms/d/" \
"1dW7sqp0lc7nAGhpFYKki5KCqNYDSbEAdAe-0eMBd9eM/" \
"viewform?entry.694442738={google_email}"
return flask.redirect(FORM_URL.format(google_email=google_email))
@app.route('/student/sv2016/signup')
@authenticated(TYPE_USER)
def sv2016_signup(credentials):
oauth2_service = get_oauth2_service(credentials)
google_email = oauth2_service.userinfo().get().execute()["email"]
FORM_URL = "https://docs.google.com/forms/d/" \
"1ZE_sXC7KOqDzOfx0vXQt3gMvEovGWPbfiVR0BYww2kA/" \
"viewform?entry.1122073806={google_email}"
return flask.redirect(FORM_URL.format(google_email=google_email))
@app.route('/orderjacket')
@authenticated(TYPE_USER)
def orderjacket(credentials):
oauth2_service = get_oauth2_service(credentials)
google_email = oauth2_service.userinfo().get().execute()["email"]
FORM_URL = "https://docs.google.com/forms/d/" \
"1lzNxBIZ8LWzOyXyqJ54SyFBi3JpLzrDmZcPCq7VnbJ8" \
"/viewform?entry.345019471={google_email}"
return flask.redirect(FORM_URL.format(google_email=google_email))
def _check_not_registered(google_email):
# Check if they're registered
wks = get_spreadsheet_fromsvc("ECESS 2015W Student Contact Form (Responses)")
keys = {v: k for k, v in enumerate(wks.row_values(1))}
for entry in wks.get_all_values()[1:]:
if entry[keys["Google_Email"]].lower() == google_email.lower():
return None
else:
return "You don't seem to be in our database yet! Please visit " \
"<a href=\"{0}\" target=\"_blank\">{0}</a> to fill out your " \
"contact information first. Then simply refresh the page to " \
"continue." \
"".format(
flask.url_for("student_register", _external=True)
)
@app.route('/student/rentalocker')
@authenticated(TYPE_USER)
def rentalocker(credentials):
oauth2_service = get_oauth2_service(credentials)
google_email = oauth2_service.userinfo().get().execute()["email"]
not_registered = _check_not_registered(google_email)
if not_registered is not None:
return not_registered
# Check if they have a locker sales entry
wks = get_spreadsheet_fromsvc("[ECESS] MCLD Locker Rental 2015W1 (Responses)")
locker_form_keys = {v: k for k, v in enumerate(wks.row_values(1))}
for locker_form_entry in wks.get_all_values()[1:]:
if locker_form_entry[locker_form_keys["Google_Email"]].lower() == google_email.lower():
payment_type = locker_form_entry[locker_form_keys["Payment_Method"]]
break
else:
FORM_URL = "https://docs.google.com/forms/d/" \
"1ixLqNKOggJqdasJ1u5QgQQA9bpLXpKO8F9XIHDKwy-0/" \
"viewform?entry.1882898146={google_email}"
return flask.redirect(FORM_URL.format(google_email=google_email))
# Present their status
res = [
"Your ID is {}".format(google_email),
"",
"Step 1 (Rental Request Form): Complete! We have received your form."
]
wks = get_spreadsheet_fromsvc("Locker_Rentals")
keys = {v: k for k, v in enumerate(wks.row_values(1))}
for entry in wks.get_all_values()[1:]:
if (
entry[keys["Google_Email"]] == google_email and
entry[keys["Term"]] == "2015W1"
):
payment_status = entry[keys["Paid"]]
if payment_status == "Not_Paid":
if payment_type == "Cash":
res.append("Step 2 (Payment): Waiting for your payment; please"
" visit MCLD 434 to pay with cash! Cost is"
" $11.")
elif payment_type == "PayPal_Invoice":
res.append("Step 2 (Payment): We need to send you a PayPal Invoice; "
"you should receive it soon so that you "
"are able to pay for your locker.")
elif payment_status == "Invoice_Sent":
res.append("Step 2 (Payment): A PayPal Invoice has been sent to your "
" email. Please promptly pay this invoice so that"
" we can assign you a locker number.")
elif payment_status == "Payment_Received":
res.append("Step 2 (Payment): We have successfully received your "
"payment!")
locker_number = entry[keys["Locker_Number"]]
if locker_number:
res.append("Step 3 (Locker Assignment): Your locker has been assigned. Your locker"
" is #{}".format(locker_number))
else:
res.append("Step 3 (Locker Assignment): We have not yet determined your locker "
"number. Please check back in a bit!")
return "\n<br>".join(res)
else:
res.append("Step 1a: We have received your locker rental request. If"
" there are any available lockers for you, we'll try "
"to process it as soon as possible!")
return "\n<br>".join(res)
@app.route('/admin/seattle/review')
@authenticated(TYPE_EDITOR)
def admin_seattle_review(credentials):
return _admin_seattle_review(credentials, spreadsheet="Seattle Trip 2015 Sign-Up (Responses)")
@app.route('/admin/seattle/confreview')
@authenticated(TYPE_EDITOR)
def admin_seattle_confreview(credentials):
return _admin_seattle_review(credentials, spreadsheet="Confirmed Attendees")
def _admin_seattle_review(credentials, spreadsheet="Seattle Trip 2015 Sign-Up (Responses)"):
gc = get_drive_conn(credentials)
try:
seattle_form = sheet2dict(get_spreadsheet_fromusr(
spreadsheet,
gc=gc
), "Google_Email")
contact_form = sheet2dict(get_spreadsheet_fromusr(
"ECESS 2015W Student Contact Form (Responses)",
gc=gc
), "Google_Email")
except gspread.SpreadsheetNotFound:
return "Unauthorized" # TODO return a 401 here
l = []
stats = {
"Dept": defaultdict(int),
"Year": defaultdict(int)
}
names = []
for gmail, entry in seattle_form.items():
try:
usr = contact_form[gmail]
except KeyError:
print("Invalid email: {}".format(gmail))
continue
l.append((entry, usr))
stats["Dept"]["{}_{}".format(usr["Dept"], usr["Program"])] += 1
stats["Year"][usr["Academic_Year"]] += 1
names.append(usr["Full_Legal_Name"])
res = [json.dumps(stats, indent=4).replace(
"\n", "<br>")]
res += ["<br>".join(json.dumps(d, indent=4).replace(
"\n", "<br>")
for d in
t)
for t in l]
res += ["<br>".join(names)]
return "\n<br><br><br>".join(res)
@app.route('/admin/invoicestosend')
@authenticated(TYPE_EDITOR)
def invoices_to_send(credentials):
gc = get_drive_conn(credentials)
try:
locker_rentals = sheet2lod(get_spreadsheet_fromusr(
"Locker_Rentals",
gc=gc
))
locker_form = sheet2dict(get_spreadsheet_fromusr(
"[ECESS] MCLD Locker Rental 2015W1 (Responses)",
gc=gc
), "Google_Email")
contact_form = sheet2dict(get_spreadsheet_fromusr(
"ECESS 2015W Student Contact Form (Responses)",
gc=gc
), "Google_Email")
except gspread.SpreadsheetNotFound:
return "Unauthorized" # TODO return a 401 here
l = []
for entry in locker_rentals:
gmail = entry["Google_Email"]
form_entry = locker_form.get(gmail)
if form_entry is None:
# l.append("Could not find {} in rental form responses.".format(gmail))
continue
payment_type = form_entry["Payment_Method"]
if payment_type == "PayPal_Invoice" and entry["Paid"] == "Not_Paid":
l.append(
"Email_Address: {}, Google_Email: {}".format(
contact_form[gmail]["Email_Address"],
contact_form[gmail]["Google_Email"]
))
return "\n<br>".join(l)
@app.route('/admin/lockerqueue')
@authenticated(TYPE_EDITOR)
def locker_queue(credentials):
gc = get_drive_conn(credentials)
try:
_locker_rentals = sheet2lod(get_spreadsheet_fromusr(
"Locker_Rentals",
gc=gc
))
locker_rentals = defaultdict(list)
for lr in _locker_rentals:
locker_rentals[lr["Google_Email"].lower()].append(lr)
locker_form = sheet2lod(get_spreadsheet_fromusr(
"[ECESS] MCLD Locker Rental 2015W1 (Responses)",
gc=gc
))
contact_form = sheet2dict(get_spreadsheet_fromusr(
"ECESS 2015W Student Contact Form (Responses)",
gc=gc
), "Google_Email")
except gspread.SpreadsheetNotFound:
return "Unauthorized" # TODO return a 401 here
d = {
"pre_150_ece_renewal": [],
"ece": [],
"non_ece": [],
"no_contact_email": [],
"unpaid_over_4d_no_email": []
}
for i, entry in enumerate(locker_form):
gmail = entry["Google_Email"].lower()
try:
email = contact_form[gmail]["Email_Address"]
except KeyError:
email = None
dln = entry["Desired_Locker_Number"]
# TODO XXX Handle multiple terms
if gmail not in locker_rentals:
contact_user = contact_form.get(gmail)
if contact_user is None:
d["no_contact_email"].append(gmail)
continue
if contact_form[gmail]["Dept"] == "ECE":
if i < 150 and entry["Renewal"] == "Yes":
d["pre_150_ece_renewal"].append("{} {}".format(gmail, dln))
else:
d["ece"].append(gmail)
else:
d["non_ece"].append(gmail)
else:
for lr_entry in locker_rentals[gmail]:
if lr_entry["Warning_Email_Sent"] != "Yes" \
and lr_entry["Paid"] == "Not_Paid":
try:
parsed = arrow.get(entry["Timestamp"], "M/DD/YYYY HH:mm:ss")
diff = (arrow.utcnow() - parsed).days
print(diff)
if diff >= 4:
d["unpaid_over_4d_no_email"]\
.append("{}".format(email))
except arrow.parser.ParserError as e:
print("{}: {}".format(entry["Timestamp"], e))
l = []
l.append("<br><br>== Pre-150 ECE Renewals ==<br>")
l.extend(d["pre_150_ece_renewal"])
l.append("<br><br>== ECE students ==<br>")
l.extend(d["ece"])
l.append("<br><br>== Non-ECE Students ==<br>")
l.extend(d["non_ece"])
l.append("<br><br>== These students' Google_Emails are not on the Contact sheet, i.e., the"
"y have not filled out the Contact form ==<br>")
l.extend(d["no_contact_email"])
l.append("<br><br>== Warning Emails to send ==<br>")
l.extend(d["unpaid_over_4d_no_email"])
return "\n<br>".join(l)
@app.route("/admin/lockertenants")
@authenticated(TYPE_EDITOR)
def locker_tenants(credentials):
gc = get_drive_conn(credentials)
try:
_locker_rentals = sheet2lod(get_spreadsheet_fromusr(
"Locker_Rentals",
gc=gc
))
contact_form = sheet2dict(get_spreadsheet_fromusr(
"ECESS 2015W Student Contact Form (Responses)",
gc=gc
), "Google_Email")
except gspread.SpreadsheetNotFound:
return "Unauthorized" # TODO
l = []
for entry in _locker_rentals:
locker_number = entry["Locker_Number"]
try:
legal_name = contact_form[entry["Google_Email"]]["Full_Legal_Name"]
except KeyError:
continue
if locker_number:
l.append(
"{} {}".format(str(locker_number).zfill(3),
legal_name)
)
return "\n<br>".join(sorted(l))
@app.route('/oauth2callback')
def oauth2callback():
usertypes = flask.session[SessKeys.usertypes]
scopes = [scope for usertype, scopes in SCOPES.items()
for scope in scopes if usertype in usertypes]
scope_urls = ['https://www.googleapis.com/auth/{}'.format(scope)
if not scope.startswith("http") else scope
for scope in scopes]
print("Authenticating with scopes: {}".format(scope_urls))
flow = client.OAuth2WebServerFlow(
client_id=APP_CLIENT_ID,
client_secret=APP_CLIENT_SECRET,
scope=scope_urls,
redirect_uri=flask.url_for('oauth2callback', _external=True)
)
if 'code' not in flask.request.args:
auth_uri = flow.step1_get_authorize_url()
return flask.redirect(auth_uri)
else:
auth_code = flask.request.args.get('code')
credentials = flow.step2_exchange(auth_code)
flask.session[SessKeys.credentials] = credentials.to_json()
return flask.redirect(flask.session[SessKeys.post_auth_redirect])
class ReverseProxied(object):
'''Wrap the application in this middleware and configure the
front-end server to add these headers, to let you quietly bind
this to a URL other than / and to an HTTP scheme that is
different than what is used locally.
In nginx:
location /myprefix {
proxy_pass http://192.168.0.1:5001;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Script-Name /myprefix;
}
:param app: the WSGI application
http://flask.pocoo.org/snippets/35/
'''
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
if script_name:
environ['SCRIPT_NAME'] = script_name
path_info = environ['PATH_INFO']
if path_info.startswith(script_name):
environ['PATH_INFO'] = path_info[len(script_name):]
scheme = environ.get('HTTP_X_SCHEME', '')
if scheme:
environ['wsgi.url_scheme'] = scheme
return self.app(environ, start_response)
if __name__ == '__main__':
import uuid
app.secret_key = str(uuid.uuid4())
app.debug = os.getenv("FLASK_DEBUG") == "1"
if app.debug:
print("WARNING: DEBUG MODE IS ENABLED!")
app.config["PROPAGATE_EXCEPTIONS"] = True
app.wsgi_app = ReverseProxied(app.wsgi_app)
app.run(threaded=True)