-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
216 lines (180 loc) · 5.67 KB
/
api.py
File metadata and controls
216 lines (180 loc) · 5.67 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
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
from flask import Flask, render_template
import logging
from versions import v0
from flask_limiter import Limiter
from flasgger import Swagger
from common.helpers import get_request_origin_identifier, make_error_object, respond
from common.db_schema import db
from common.schemas import *
from flasgger import APISpec, Schema, Swagger, fields
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
import os, sys, getpass
from os import environ as env
from werkzeug.exceptions import HTTPException
#default values are only needed for some of these
db_username = env.get("DB_USERNAME")
db_password = env.get("DB_PASSWORD")
db_host = os.getenv("DB_HOST", "localhost")
db_name = os.getenv("DB_NAME", "classclock")
if sys.argv[1] in ("setup", "demo"):
if not env.get("DB_CONNECTION_URL"):
if not db_username:
db_username = input('database username: ')
if not db_password:
db_password = getpass.getpass('database password (input not shown): ')
db_connection_string=os.getenv("DB_CONNECTION_URL",
'mysql+mysqlconnector://{user}:{pw}@{url}/{db}'.format(
user=db_username,
pw=db_password,
url=db_host,
db=db_name
)
)
app = Flask(__name__)
limiter = Limiter(app, default_limits=[
"25/hour", "5/minute"], key_func=get_request_origin_identifier, headers_enabled=True)
app.register_blueprint(v0.blueprint, url_prefix='/v0')
# Create an APISpec
#info: {
spec = APISpec(
title="ClassClock API",
version="0.1",
openapi_version='2.0',
plugins=[
FlaskPlugin(),
MarshmallowPlugin(),
],
options= {
"description": "The development version of the ClassClock API",
"servers": [
{
"url": "https://api.classclock.app/v0",
"description": "ClassClock API Server"
},
{
"url": "https://localhost:8000/v0",
"description": "Dev server"
}
],
"externalDocs": {
"description": "This API might loosely follow the JSON:API specofocation",
"url": "https://jsonapi.org"
}#,
# "contact": {
# # "responsibleOrganization": "ME",
# # "responsibleDeveloper": "Me",
# # "email": "me@me.com",
# # "url": "www.me.com",
# },
# "termsOfService": "http://me.com/terms",
}
)
template = spec.to_flasgger(
app,
definitions=[SchoolSchema, BellScheduleSchema],
paths=[]
)
swagger = Swagger(app, config={
"headers": [
],
"specs": [
{
"endpoint": 'apispec_v1',
"route": '/apispec_v1.json',
"rule_filter": lambda rule: True, # all in
"model_filter": lambda tag: True, # all in
}
],
"static_url_path": "/flasgger_static",
# "static_folder": "static", # must be set by user
"swagger_ui": True,
"specs_route": "/v0/docs/",
"basePath": "/v0",
"host": "api.classclock.app", # overrides localhost:500
"schemes": [
"https"
],
"securityDefinitions": {
"ApiKeyAuth": {
"type": "apiKey",
"in": "header",
"name": "Authentication"
}
}
},
template=template)
app.config.update(SQLALCHEMY_DATABASE_URI=db_connection_string,DEBUG=True, SQLALCHEMY_TRACK_MODIFICATIONS=False)
db.init_app(app)
if sys.argv[1] == "setup":
# https://stackoverflow.com/a/46541219
with app.app_context():
db.create_all()
db.session.commit()
print("Done.")
exit(0)
elif sys.argv[1] == "demo":
# https://stackoverflow.com/a/46541219
with app.app_context():
from common.db_schema import *
import datetime
import time
def today_plus(sch, num_days):
return BellScheduleDate(school_id = sch, date=datetime.date.today() + datetime.timedelta(days=num_days))
s = School(owner_id="1234567890", full_name="Demonstration High School", acronym="DHS")
sc1 = BellSchedule(
school_id = s.id,
name="Even Day",
display_name= "Even",
dates=[
today_plus(s.id, 0),
today_plus(s.id, 2),
today_plus(s.id, 4)
]
)
sc2 = BellSchedule(
school_id = s.id,
name="Odd Day",
display_name="Odd",
dates=[
today_plus(s.id, 1),
today_plus(s.id, 3)
]
)
s.schedules = [sc1,sc2]
sc1.meeting_times = [
BellScheduleMeetingTime(
schedule_id =sc1.id,
name="First Period",
start_time=datetime.time(hour=8, minute=25, second=0, microsecond=0),
end_time=datetime.time(9,25)
)
]
sc2.meeting_times = [
BellScheduleMeetingTime(
schedule_id =sc2.id,
name="First Period",
start_time=datetime.time(8,45),
end_time=datetime.time(9,45)
)
]
db.session.add(s)
db.session.commit()
print("Done.")
exit(0)
@app.errorhandler(HTTPException)
def handle_HTTP_error(e):
return respond(
make_error_object(
e.code, title=e.name, message=e.description),
code=e.code
)
@app.route("/", methods=['GET'])
def home():
return render_template('home.html')
if __name__ == "__main__":
app.run()
else:
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)