-
Notifications
You must be signed in to change notification settings - Fork 0
/
v0.py
605 lines (502 loc) · 18.4 KB
/
v0.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
import uuid
import datetime
from flask_limiter import util
from flask import current_app, json
from os import environ as env
from flask_limiter.util import get_remote_address
from common.helpers import respond
from flask import Blueprint, abort, jsonify, request
from werkzeug.exceptions import HTTPException
from flask_cors import CORS
from marshmallow.exceptions import ValidationError
# from bson import json_util
# from bson.objectid import ObjectId
import http.client
from common.db_schema import School as SchoolDB, db, BellSchedule as BellScheduleDB
from sqlalchemy import create_engine
from common.helpers import *
from common.constants import APIScopes, HTTP_DATE_FORMAT
from common.schemas import SchoolSchema, BellScheduleSchema
from common.services import auth0management
import common.exceptions
#
# App Setup
#
DB_HOST = env.get("DB_HOST")
DB_USERNAME = env.get("DB_USERNAME")
DB_PASSWORD = env.get("DB_PASSWORD")
blueprint = Blueprint('v0', __name__)
flex_url = "http://localhost:3000" if env.get("FLASK_ENV") == 'development' else "classclock-*-moralcode.vercel.app"
CORS(blueprint, origins=["https://web.classclock.app", "https://beta.web.classclock.app", flex_url], allow_headers=[
"Accept", "Authorization", "Content-Type"])
# supports_credentials=True)
# @blueprint.route("/schools", methods=['GET'])
# @cross_origin(headers=["Content-Type", "Authorization"])
# @cross_origin(headers=["Access-Control-Allow-Origin", "http://localhost:5000"])
# @requires_auth
# def get_schools():
# """Get a list of every publicly accessible ClassClock school
# ---
# responses:
# '200':
# description: A list of every publicly accessible ClassClock school
# content:
# application/json:
# ...
# '400':
# description: Unauthorized for some reason such as an invalid access token or incorrect scopes
# """
# @blueprint.route("/school/<string:identifier>", methods=['GET'])
# @cross_origin(headers=["Content-Type", "Authorization"])
# @cross_origin(headers=["Access-Control-Allow-Origin", "http://localhost:5000"])
# @requires_auth
# def get_school_by_id(identifier):
# """
# Get information about a single school
# ---
# parameters:
# - name: identifier
# in: path
# type: string
# required: true
# description: the hexadecimal identifier of the school you are requesting
# responses:
# 200:
# description: data for a single school
# '400':
# description: Unauthorized for some reason such as an invalid access token or incorrect scopes
# default:
# description: error payload
# """
@blueprint.route("/ping", strict_slashes=False, methods=['GET'])
def ping():
""" Returns the text "pong" as a connectivity check
---
responses:
200:
description: the text "pong"
"""
return "pong"
# TODO: add a search parameter
@blueprint.route("/schools", strict_slashes=False, methods=['GET'])
@check_headers
def list_schools():
""" Returns a list of schools
---
responses:
200:
description: A list of schools
content:
application/json:
schema:
$ref: '#/definitions/School'
"""
school_list = []
schools = SchoolDB.query.filter_by(soft_deleted=False).all()
return respond(SchoolSchema(exclude=('soft_deleted',)).dump(schools, many=True))
@blueprint.route("/school/<string:school_id>", strict_slashes=False, methods=['GET'])
@check_headers
def get_school(school_id):
""" Returns a single school
---
responses:
200:
description: A single school object
schema:
$ref: '#/definitions/School'
parameters:
- in: path
name: school_id
schema:
type: string
length: 32
required: true
- in: header
name: If-Modified-Since
schema:
type: string
format: date
required: false
"""
school = SchoolDB.query.filter_by(id=school_id, soft_deleted=False).first()
#double check this
if school is None:
raise Oops("No school was found with the specified id.",
404, title="Resource Not Found")
if 'If-Modified-Since' in request.headers:
since = datetime.strptime(request.headers.get('If-Modified-Since'), HTTP_DATE_FORMAT)
# TODO: make this a more robust check
if school.last_modified == since:
return respond(code=304) #Not Modified
return respond(SchoolSchema(exclude=('soft_deleted',)).dump(school))
@blueprint.route("/school", strict_slashes=False, methods=['POST'])
@check_headers
@requires_auth(permissions=[APIScopes.CREATE_SCHOOL])
@requires_admin
def create_school():
""" Creates a new school
---
parameters:
- name: school
in: body
type: object
schema:
$ref: '#/definitions/School'
security:
- ApiKeyAuth: []
responses:
200:
description: A list of schools
schema:
$ref: '#/definitions/School'
"""
# if len(list_owned_school_ids()) > 0:
# raise Oops(
# "Authorizing user is already the owner of another school", 401)
data = get_request_body(request)
if data is None:
raise Oops("Invalid or non-JSON request body provided.", 400)
new_object = None
try:
#Numbers, booleans, strings, and ``None`` are considered invalid input to `Schema.load
new_object = SchoolSchema().load(data, session=db.session)
except ValidationError as err:
# print(err.messages) # => {"email": ['"foo" is not a valid email address.']}
# print(err.valid_data)
return respond(err.messages, code=400)
# if new_object.errors != {}:
# return handle_marshmallow_errors(new_object.errors)
db.session.add(new_object)
db.session.commit()
#TODO: need to verify that the insert worked?
return respond(SchoolSchema(exclude=('soft_deleted',)).dump(new_object))
@blueprint.route("/school/<string:school_id>", strict_slashes=False, methods=['PATCH'])
@check_headers
@requires_auth(permissions=[APIScopes.EDIT_SCHOOL])
@requires_admin
def update_school(school_id):
"""
updates a school
---
security:
- ApiKeyAuth: []
parameters:
- in: path
name: school_id
schema:
type: string
length: 32
required: true
- in: body
name: school
schema:
$ref: '#/definitions/School'
required: true
"""
data = get_request_body(request)
# if new_object.errors != {}:
# return handle_marshmallow_errors(new_object.errors)
school = SchoolDB.query.filter_by(id=school_id, soft_deleted=False).first()
if school is None:
raise Oops("No records could be updated because none were found",
404, title="No Records Found")
else:
check_ownership(school)
# check modification times
# this needs to happen after the school is retreived from the DB for comparison
if 'If-Unmodified-Since' in request.headers:
since = datetime.datetime.strptime(request.headers.get('If-Unmodified-Since'), HTTP_DATE_FORMAT)
trap_object_modified_since(school.last_modified, since)
try:
updated_school = SchoolSchema().load(data, session=db.session, instance=school)
except ValidationError as err:
# print(err.messages) # => {"email": ['"foo" is not a valid email address.']}
# print(err.valid_data)
return respond(err.messages, code=400)
db.session.commit()
#TODO: need to verify that the update worked?
return respond(SchoolSchema(exclude=('soft_deleted',)).dump(school))
@blueprint.route("/school/<string:school_id>", strict_slashes=False, methods=['DELETE'])
@check_headers
@requires_auth(permissions=[APIScopes.DELETE_SCHOOL, APIScopes.DELETE_BELL_SCHEDULE])
@requires_admin
def delete_school(school_id):
"""
deletes a school
---
security:
- ApiKeyAuth: []
parameters:
- in: path
name: school_id
schema:
type: string
length: 32
required: true
- in: header
name: If-Unmodified-Since
schema:
type: string
format: date
required: false
"""
school = SchoolDB.query.filter_by(id=school_id, soft_deleted=False).first()
if school is None:
raise Oops("No records could be deleted because none were found",
404, title="No Records Found")
else:
check_ownership(school)
# check modification times
# this needs to happen after the school is retreived from the DB for comparison
if 'If-Unmodified-Since' in request.headers:
since = datetime.datetime.strptime(request.headers.get('If-Unmodified-Since'), HTTP_DATE_FORMAT)
trap_object_modified_since(school.last_modified, since)
db.session.delete(school)
db.session.commit()
# should this just archive the school? or delete it and all related records?
# sqlalchemy can be set to cascade deletes (i think).
return None, 204
@blueprint.route("/bellschedules", strict_slashes=False, methods=['GET'])
@check_headers
@requires_auth#(permissions=[APIScopes.DELETE_SCHOOL, APIScopes.DELETE_BELL_SCHEDULE])
@requires_admin
def list_owned_bellschedules():
"""
gets a list of bell schedules that are part of schools that the current user owns
---
security:
- ApiKeyAuth: []
responses:
200:
description: A list of bell schedules
schema:
$ref: '#/definitions/BellSchedule'
"""
#if get_api_user_id() not in school.owner_id
schedules = BellScheduleDB.query.join(BellScheduleDB.school).filter(SchoolDB.owner_id==get_api_user_id(), SchoolDB.soft_deleted==False, BellScheduleDB.soft_deleted==False)
return respond(BellScheduleSchema(exclude=('school_id','soft_deleted')).dump(schedules, many=True))
#TODO: add filtering for return values to reduce size of response. i.e. filter dates by after today, exclude meeting times if they havent changed
@blueprint.route("/bellschedules/<string:school_id>", strict_slashes=False, methods=['GET'])
@check_headers
def list_bellschedules(school_id):
"""
gets a list of bell schedules
---
parameters:
- in: path
name: school_id
schema:
type: string
length: 32
required: true
responses:
200:
description: A list of bell schedules
schema:
$ref: '#/definitions/BellSchedule'
"""
schedules = BellScheduleDB.query.filter_by(school_id=school_id, soft_deleted=False)
return respond(BellScheduleSchema(exclude=('school_id',)).dump(schedules, many=True))
@blueprint.route("/bellschedule/<string:bell_schedule_id>", strict_slashes=False, methods=['GET'])
@check_headers
def get_bellschedule(bell_schedule_id):
"""
gets a single bell schedule
---
parameters:
- in: path
name: bell_schedule_id
schema:
type: string
length: 32
required: true
- in: header
name: If-Modified-Since
schema:
type: string
format: date
required: false
responses:
200:
description: A single of bell schedule
schema:
$ref: '#/definitions/BellSchedule'
"""
schedule = BellScheduleDB.query.filter_by(
id=bell_schedule_id, soft_deleted=False).first()
#double check this
if schedule is None:
raise Oops("No bell schedule was found with the specified id.",
404, title="Resource Not Found")
if 'If-Modified-Since' in request.headers:
since = datetime.strptime(request.headers.get('If-Modified-Since'), HTTP_DATE_FORMAT)
# TODO: make this a more robust check
if schedule.last_modified == since:
return respond(code=304) #Not Modified
return respond(BellScheduleSchema(exclude=('soft_deleted',)).dump(schedule))
@blueprint.route("/bellschedule", strict_slashes=False, methods=['POST'])
@check_headers
@requires_auth(permissions=[APIScopes.CREATE_BELL_SCHEDULE])
@requires_admin
def create_bellschedule():
"""
Create a new bell schedule
---
security:
- ApiKeyAuth: []
parameters:
- in: body
name: schedule
schema:
$ref: '#/definitions/BellSchedule'
required: true
"""
request_data = get_request_body(request)
school_id= request_data["school_id"]
# get school_id from a data parameter
school = SchoolDB.query.filter_by(id=school_id).first()
check_ownership(school)
new_schedule = BellScheduleSchema().load(request_data, session=db.session)
school.schedules.append(new_schedule)
db.session.commit()
return respond(BellScheduleSchema(exclude=('school_id','soft_deleted')).dump(new_schedule))
@blueprint.route("/bellschedule/<string:bell_schedule_id>", strict_slashes=False, methods=['PATCH'])
@check_headers
@requires_auth(permissions=[APIScopes.EDIT_BELL_SCHEDULE])
@requires_admin
def update_bellschedule(bell_schedule_id):
"""
Updates a bell schedule
---
security:
- ApiKeyAuth: []
parameters:
- in: path
name: bell_schedule_id
schema:
type: string
length: 32
required: true
- in: body
name: schedule
schema:
$ref: '#/definitions/BellSchedule'
required: true
- in: header
name: If-Unmodified-Since
schema:
type: string
format: date
required: false
"""
schedule = BellScheduleDB.query.filter_by(id=bell_schedule_id).first()
school = SchoolDB.query.filter_by(id=schedule.school_id).first()
if school is not None:
check_ownership(school)
if 'If-Unmodified-Since' in request.headers:
since = datetime.datetime.strptime(request.headers.get('If-Unmodified-Since'), HTTP_DATE_FORMAT)
trap_object_modified_since(school.last_modified, since)
data = get_request_body(request)
# remove ID from request body if provided because for some reason, the exclude parameter isnt working or may not be correctly getting passed down to the nested/plucked fields
if data['id']:
del data['id']
try:
updated_schedule = BellScheduleSchema(exclude=('id', 'creation_date')).load(
data, session=db.session, instance=schedule)
except ValidationError as err:
# print(err.messages) # => {"email": ['"foo" is not a valid email address.']}
# print(err.valid_data)
return respond(err.messages, code=400)
db.session.commit()
return respond(BellScheduleSchema(exclude=('school_id','soft_deleted')).dump(schedule))
@blueprint.route("/bellschedule/<string:bell_schedule_id>", methods=['DELETE'])
@check_headers
@requires_auth(permissions=[APIScopes.DELETE_BELL_SCHEDULE])
@requires_admin
def delete_bellschedule(bell_schedule_id):
"""
deletes a bell schedule
---
security:
- ApiKeyAuth: []
parameters:
- in: path
name: bell_schedule_id
schema:
type: string
length: 32
required: true
- in: header
name: If-Unmodified-Since
schema:
type: string
format: date
required: false
"""
schedule = BellScheduleDB.query.filter_by(id=bell_schedule_id, soft_deleted=False).first()
school = SchoolDB.query.filter_by(id=schedule.school_id).first()
if school is not None:
check_ownership(school)
if 'If-Unmodified-Since' in request.headers:
since = datetime.datetime.strptime(request.headers.get('If-Unmodified-Since'), HTTP_DATE_FORMAT)
trap_object_modified_since(school.last_modified, since)
schedule.soft_deleted = True
# db.session.delete(schedule)
db.session.commit()
return respond("success", code=204)
#
# Routes
#
# register_api(api, School, "v0", name_of_optional_param="school_id")
# register_api(api, BellSchedule, "v0", url_prefix="/school/<string:school_id>",
# name_of_optional_param="bell_schedule_id")
@blueprint.before_request
def before():
current_app.logger.info(request.method + " " + request.path)
@blueprint.after_request
def after_request(response):
response.headers['Content-Type'] = 'application/json'
if response.status_code != 200:
current_app.logger.info( "Handled request with HTTP status: " + str(response.status_code))
if response.status_code > 399:
current_app.logger.info(str(response.get_data()))
return response
#
#
# Error Handler Section
#
#
# override default rate limit exceeded error and return a JSON response instead
# https://flask-limiter.readthedocs.io/en/stable/#custom-rate-limit-exceeded-responses
@blueprint.errorhandler(429)
def ratelimit_handler(e):
current_app.logger.warning(e)
current_app.logger.warning("User at address " + get_remote_address() + "exceeded rate limit of " + e.description)
return respond(
make_error_object(429, title="Ratelimit Exceeded",
message="ratelimit of " + e.description + " exceeded"),
code=429
)
@blueprint.errorhandler(AuthError)
def handle_auth_error(e):
return respond(
make_error_object(e.status_code, message=e.error), code=e.status_code
)
@blueprint.errorhandler(Oops)
def handle_error(e):
if e.title is not None:
return respond(
make_error_object(e.status_code, message=e.message, title=e.title), code=e.status_code
)
else:
return respond(
make_error_object(e.status_code, message=e.message), code=e.status_code
)
# @blueprint.errorhandler(Exception)
# def generic_exception_handler(e):
# # "We're sorry, but the electrons that were tasked with handling your request became terribly misguided and forgot what it is that they were supposed to be doing. Our team of scientists in the Electron Amnesia Recovery Ward is currently nursing them back to health; if you have any information about what it is these electrons were supposed to be doing at the time of this incident, please contact the maintainer of this service."
# print("an exception occurred")
# print(e)
# return respond(
# make_error_object(500), code=500, headers={'Content-Type': 'application/json'}
# )