-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.py
More file actions
155 lines (107 loc) · 4.16 KB
/
server.py
File metadata and controls
155 lines (107 loc) · 4.16 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
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
from functools import wraps
from flask import Flask, jsonify, request, render_template, send_from_directory
from data.access import ApiError, database
from settings import CURRENT_YEAR, CURRENT_TERM
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
return response
# Flask config
application = Flask(
__name__,
# template_folder='frontend/templates',
# static_folder='frontend/static'
)
application.config['JSON_SORT_KEYS'] = False
application.url_map.strict_slashes = False
application.after_request(add_cors_headers)
# @application.route('/docs/<path:filename>')
# def static_docs(filename):
# return send_from_directory('docs', filename)
# @application.route('/')
# def idx():
# return render_template('index.html')
def campus_api(path: str, methods=None):
def decorator(func):
@application.route(f'/<campus>/{path}', methods=(methods or ['GET']))
@wraps(func)
def api(campus, *args, **kwargs):
year = request.args.get('year') or CURRENT_YEAR
quarter = request.args.get('quarter') or CURRENT_TERM
try:
try:
db = database.load(campus, year, quarter)
ret = func(db, *args, **kwargs)
except FileNotFoundError:
raise ApiError(
404,
'Data for requested campus, year, and quarter combo does not exist.'
)
if ret is None or (isinstance(ret, list) and len(ret) == 0):
raise ApiError(404, 'No results')
except ApiError as e:
return jsonify({'error': e.message}), e.status
return jsonify(ret), 200
return api
return decorator
def campus_multi_term_api(path: str, methods=None):
def decorator(func):
@application.route(f'/<campus>/{path}', methods=(methods or ['GET']))
@wraps(func)
def api(campus, *args, **kwargs):
try:
try:
db = database.load_multi_db(campus)
ret = func(db, *args, **kwargs)
except FileNotFoundError:
raise ApiError(
404,
'Data for requested campus does not exist.'
)
if ret is None or (isinstance(ret, list) and len(ret) == 0):
raise ApiError(404, 'No results')
except ApiError as e:
return jsonify({'error': e.message}), e.status
return jsonify(ret), 200
return api
return decorator
@application.route('/<campus>')
def api_campus(campus):
try:
ret = database.campus_info(campus)
except ApiError as e:
return jsonify({'error': e.message}), e.status
return jsonify(ret), 200
@campus_multi_term_api('instructors/<instructor>')
def api_one_instructor(db, instructor):
return database.one_instructor(db, instructor)
@campus_api('courses')
def api_courses(db):
return database.all_courses(db)
@campus_api('classes')
def api_classes(db):
return database.all_classes(db)
@campus_api('classes/<crn>')
def api_classes_by_crn(db, crn):
return database.one_class_by_crn(db, int(crn))
@campus_api('depts')
def api_depts(db):
return database.all_depts(db)
@campus_api('depts/<dept>')
def api_dept(db, dept):
return database.one_dept(db, dept)
@campus_api('depts/<dept>/classes')
def api_dept_classes(db, dept):
return database.all_classes_in_dept(db, dept)
@campus_api('depts/<dept>/courses')
def api_dept_courses(db, dept):
return database.all_courses_in_dept(db, dept)
@campus_api('depts/<dept>/courses/<course>')
def api_dept_course(db, dept, course):
return database.one_course(db, dept, course)
@campus_api('depts/<dept>/courses/<course>/classes')
def api_dept_course_classes(db, dept, course):
return database.all_classes_in_course(db, dept, course)
if __name__ == '__main__':
application.run(host='0.0.0.0', port=5001, debug=True, threaded=True)