forked from ADE-Scheduler/ADE-Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
501 lines (422 loc) · 15 KB
/
app.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
# Python imports
import base64
import configparser
import distutils
import os
import traceback
import warnings
from datetime import timedelta
# External imports
import authlib.jose as jose
from authlib.integrations.flask_client import OAuth
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
# Flask imports
from flask import Flask, flash, g, redirect, render_template, request, session, url_for
from flask_babel import Babel, gettext
from flask_compress import Compress
from flask_jsglue import JSGlue
from flask_login import (
LoginManager,
current_user,
login_required,
user_logged_in,
user_logged_out,
)
from flask_mail import Mail, Message, email_dispatched
from flask_migrate import Migrate
from flask_session import Session
# External imports
from jsmin import jsmin
from lxml.etree import XMLSyntaxError
from requests.exceptions import ConnectionError, HTTPError
from werkzeug.exceptions import InternalServerError
# Backend imports
import backend.ade_api as ade
import backend.cookies as cookies
import backend.manager as mng
import backend.mixins as mxn
import backend.models as md
import backend.schedules as schd
import backend.security as scty
import backend.servers as srv
import backend.track_usage as tu
import backend.uclouvain_apis as ucl
# CLI commands
from cli import (
cli_api_usage,
cli_client,
cli_external_calendars,
cli_mails,
cli_plots,
cli_redis,
cli_roles,
cli_schedules,
cli_sql,
cli_usage,
cli_users,
)
# Views imports
from views import utils as utl
from views.account import account
from views.admin import admin
from views.api import api
from views.calendar import calendar
from views.classroom import classroom
from views.contact import contact
from views.contribute import contribute
from views.help import help as _help
from views.security import security
from views.whatisnew import whatisnew
# Change current working directory to main directory
os.chdir(os.path.dirname(os.path.realpath(__file__)))
# Setup app
app = Flask(__name__, template_folder="static/dist/html")
## Optionally enable profiling
app.config["PROFILE"] = (
bool(distutils.util.strtobool(os.environ["PROFILE"]))
if "PROFILE" in os.environ
else False
)
if app.config["PROFILE"]:
from werkzeug.middleware.profiler import ProfilerMiddleware
profile_dir = "profile"
if os.path.exists(profile_dir):
if not os.path.isdir(profile_dir):
warnings.warn(
f"You cannot save the profiling to {profile_dir} since it is a file. It must be a directory."
)
profile_dir = None
else:
os.mkdir(profile_dir)
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, profile_dir=profile_dir)
## Register blueprints
app.register_blueprint(security)
app.register_blueprint(calendar, url_prefix="/calendar")
app.register_blueprint(account, url_prefix="/account")
app.register_blueprint(classroom, url_prefix="/classroom")
app.register_blueprint(_help, url_prefix="/help")
app.register_blueprint(contact, url_prefix="/contact")
app.register_blueprint(api, url_prefix="/api")
app.register_blueprint(whatisnew, url_prefix="/whatisnew")
app.register_blueprint(contribute, url_prefix="/contribute")
app.register_blueprint(admin, url_prefix="/admin")
app.config["SECRET_KEY"] = os.environ["FLASK_SECRET_KEY"]
app.config["SALT"] = os.environ["FLASK_SALT"]
jsglue = JSGlue(app)
# Register new commands
app.cli.add_command(cli_sql.sql)
app.cli.add_command(cli_redis.redis)
app.cli.add_command(cli_roles.roles)
app.cli.add_command(cli_client.client)
app.cli.add_command(cli_schedules.schedules)
app.cli.add_command(cli_usage.usage)
app.cli.add_command(cli_users.users)
app.cli.add_command(cli_api_usage.api_usage)
app.cli.add_command(cli_plots.plots)
app.cli.add_command(cli_mails.mails)
app.cli.add_command(cli_external_calendars.extcals)
# Load REDIS TTL config
redis_ttl_config = configparser.ConfigParser()
redis_ttl_config.read(".redis-config-ttl.cfg")
mode = os.environ["FLASK_ENV"] # production of development
if mode not in redis_ttl_config:
raise ValueError(f"Redis TTL config file is missing `{mode}` mode")
redis_ttl_config = srv.parse_redis_ttl_config(redis_ttl_config[mode])
app.config["REDIS_TTL"] = redis_ttl_config
# Setup the API Manager
app.config["ADE_API_CREDENTIALS"] = {
"url": os.environ["ADE_URL"],
"data": os.environ["ADE_DATA"],
"Authorization": os.environ["ADE_AUTHORIZATION"],
}
app.config["ADE_FAKE_API"] = (
bool(distutils.util.strtobool(os.environ["ADE_FAKE_API"]))
if "ADE_FAKE_API" in os.environ
else False
)
manager = mng.Manager(
ade.Client(app.config["ADE_API_CREDENTIALS"])
if not app.config["ADE_FAKE_API"]
else ade.FakeClient(app.config["ADE_API_CREDENTIALS"]),
srv.Server(host="localhost", port=6379),
md.db,
redis_ttl_config,
)
app.config["MANAGER"] = manager
# Setup Flask-Mail
app.config["MAIL_SERVER"] = os.environ["MAIL_SERVER"]
app.config["MAIL_PORT"] = 587
app.config["MAIL_USE_TLS"] = True
app.config["MAIL_USERNAME"] = os.environ["MAIL_USERNAME"]
app.config["MAIL_PASSWORD"] = os.getenv("MAIL_PASSWORD", None)
app.config["MAIL_DEFAULT_SENDER"] = os.environ["MAIL_USERNAME"]
app.config[
"MAIL_MAX_EMAILS"
] = 30 # Better for avoiding errors from [email protected]
app.config["ADMINS"] = [os.environ["MAIL_ADMIN"]]
# Allows compression of text assets
# Production server has integrated compression support
if app.env == "development":
compress = Compress(app)
for optional, default in [("MAIL_DISABLE", False), ("MAIL_SEND_ERRORS", True)]:
if optional in os.environ:
app.config[optional] = bool(distutils.util.strtobool(os.environ[optional]))
else:
app.config[optional] = default
def log_mail_message(message, app):
"""If mails are disabled, their content will be outputted in the debug output"""
app.logger.debug(
f"A mail was supposed to be send:\n"
f"[SUBJECT]:\n{message.subject}\n"
f"[BODY]:\n{message.body}\n"
f"[END]"
)
if app.config["MAIL_DISABLE"]:
app.config["MAIL_DEBUG"] = True
app.config["MAIL_SUPPRESS_SEND"] = True
email_dispatched.connect(log_mail_message)
app.config["MAIL_MANAGER"] = Mail(app)
# Setup Flask-SQLAlchemy
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ["ADE_DB_PATH"]
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
manager.database.init_app(app)
migrate = Migrate(app, manager.database)
# Setup Flask-Login
app.config["USE_SESSION_FOR_NEXT"] = True
login = LoginManager(app)
login.login_view = "security.login"
login.login_message = None
login.anonymous_user = mxn.AnonymousUser
app.config["LOGIN_MANAGER"] = login
# Setup UCLouvain OAuth2
oauth = OAuth(app, fetch_token=scty.fetch_token, update_token=scty.update_token)
oauth.register(
# Client identification
name="uclouvain",
client_id=os.environ["UCLOUVAIN_CLIENT_ID"],
client_secret=os.environ["UCLOUVAIN_CLIENT_SECRET"],
api_base_url=ucl.API.BASE_URL,
# Access token
access_token_url=ucl.API.TOKEN_URL,
access_token_params=None,
# Authorization
authorize_url=ucl.API.AUTHORIZE_URL,
authorize_params=None,
)
app.config["UCLOUVAIN_MANAGER"] = oauth.create_client("uclouvain")
# Setup Flask-Session
app.config["SESSION_TYPE"] = "redis"
app.config["SESSION_REDIS"] = manager.server
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(**redis_ttl_config["user_session"])
app.config["SESSION_MANAGER"] = Session(app)
# Setup Flask-Babel
app.config["LANGUAGES"] = ["en", "fr"]
app.config["BABEL_TRANSLATION_DIRECTORIES"] = "translations"
babel = Babel(app)
# Setup Fernet encryption / decryption
password = app.config["SECRET_KEY"].encode()
salt = app.config["SALT"].encode()
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=390000,
)
key = base64.urlsafe_b64encode(kdf.derive(password))
app.config["FERNET"] = Fernet(key)
# Jinja filter for autoversionning
@app.template_filter("autoversion")
def autoversion_filter(filename):
fullpath = os.path.join("", filename[1:])
try:
timestamp = str(os.path.getmtime(fullpath))
except OSError:
return filename
newfilename = "{0}?{1}".format(filename, timestamp)
return newfilename
# Babel's locale "getter/setter"
@babel.localeselector
def get_locale():
if session.get("locale") is None:
session["locale"] = "fr"
return session["locale"]
app.jinja_env.globals.update(get_locale=get_locale)
@app.route("/locale/<locale>")
def set_locale(locale):
if locale in app.config["LANGUAGES"]:
session["locale"] = locale
return redirect(request.args.get("next"))
# Write jsglue.min.js
@app.before_first_request
def before_first_request():
if not os.path.exists("static/dist"):
os.makedirs("static/dist")
with open("static/dist/jsglue.min.js", "w") as f:
f.write(jsmin(jsglue.generate_js()))
@app.before_request
def before_request():
tu.before_request()
@app.after_request
def after_request(response):
# Check if a token has been refreshed
token = g.pop("token", None)
if token:
response = cookies.set_oauth_token(token, response)
return tu.after_request(response)
# Flask-Login's user loader
@login.user_loader
def load_user(fgs):
return md.User.query.filter_by(fgs=fgs).first()
# Reset current schedule on user logout
@user_logged_out.connect_via(app)
def when_user_logged_out(sender, user):
# When pressing confirmation link, somehow this function is triggered
# without an initialised session...
utl.init_session()
if session["current_schedule"].id is not None:
if (
not user.is_anonymous
): # Fixes problem whem confirmation link logs out but not account was actually logged in
user.set_last_schedule_id(session["current_schedule"].id)
session["current_schedule"] = schd.Schedule(manager.get_default_project_id())
session["current_schedule_modified"] = False
# Load previous "current schedule" on user login
@user_logged_in.connect_via(app)
def when_user_logged_in(sender, user):
if user.last_schedule_id is not None:
if (
session.get("current_schedule") is None
or session["current_schedule"].is_empty()
):
schedule = user.get_schedule(id=user.last_schedule_id)
if schedule:
session["current_schedule"] = schedule.data
# Main page
@app.route("/")
def welcome():
if not session.get("previous_user"):
utl.init_session()
g.track_var["new user"] = "+1"
session["previous_user"] = True
return redirect(url_for("calendar.index"))
# Migration route
@app.route("/migrate/<token>")
@login_required
def migrate(token):
# Decode token, fetch corresponding old user
try:
claims = jose.jwt.decode(token, app.config["SECRET_KEY"])
except jose.errors.DecodeError:
flash(
gettext("Oops, it looks like you provided an invalid migration token."),
"error",
)
return redirect(url_for("calendar.index"))
email = claims["email"]
old_user = md.OldUser.query.filter_by(email=email).first()
if old_user is None or not old_user.is_active:
flash(
gettext(
"Either this account does not exist or the data has already been migrated."
),
"error",
)
return redirect(url_for("calendar.index"))
# Add old user's schedules to current_user
for s in old_user.schedules:
current_user.schedules.append(s)
# All done, deactivate old user
old_user.confirmed_at = None
md.db.session.commit()
flash(
gettext("Success: your data has been migrated to your new account !"), "success"
)
return redirect(url_for("calendar.index"))
# Error handlers
@app.errorhandler(HTTPError)
@app.errorhandler(ConnectionError)
def api_request_failed(e):
"""
This catches the HTTPError raised when doing `resp.raise_for_status()`.
Typically catches the ADE/UCLouvain API errors. This shouldn't be handled
here, but acts as a failsafe in case we fail to do so elsewhere.
These kind of failures are considered serious, thus we return a "website
is down" page. (but could be improved/discussed)
"""
return render_template("errorhandler/500.html", ade=True), 500
@app.errorhandler(XMLSyntaxError)
def handle_empty_ade_responses(e):
"""
Sometimes (actually pretty often...) the API returns an empty response. Sigh.
However, it does seem to resolve itself by trying again...
In the meanwhile, flash an error message asking the user to try again.
"""
err_text = gettext(
"Hum... it looks like there's been a bug with the ADE API. Please try again and if the problem persists, do not hesitate to contact us !"
)
if request.accept_mimetypes.best == "application/json":
return err_text, 500
else:
flash(err_text)
return redirect(url_for("calendar.index")), 500
@app.errorhandler(InternalServerError)
def handle_exception(e):
if not app.debug and app.config["MAIL_SEND_ERRORS"]:
error = e.original_exception
error_request = f"{request.path} [{request.method}]"
error_module = error.__class__.__module__
if error_module is None:
error_name = error.__class__.__name__
else:
error_name = f"{error_module}.{error.__class__.__name__}"
error_details = str(traceback.format_exc())
msg = Message(
subject=f"ADE Scheduler Failure: {error_name}",
body=f"Exception on {error_request}: {str(error)}\n\n{error_details}",
recipients=app.config["ADMINS"],
)
app.config["MAIL_MANAGER"].send(msg)
if request.accept_mimetypes.best == "application/json":
return gettext("An unknown error has occurred"), 500
else:
return render_template("errorhandler/500.html"), 500
@app.errorhandler(403) # FORBIDDEN
def forbidden(e):
if request.accept_mimetypes.best == "application/json":
return gettext("Access to this resource is forbidden."), e.code
else:
return (
render_template(
"errorhandler/403.html", message="403 Forbidden access ヽ(ಠ_ಠ) ノ"
),
e.code,
)
@app.errorhandler(404) # URL NOT FOUND
@app.errorhandler(405) # METHOD NOT ALLOWED
def page_not_found(e):
if request.accept_mimetypes.best == "application/json":
return gettext("Resource not found"), e.code
else:
return (
render_template(
"errorhandler/404.html", message=gettext("404 Page not found :(")
),
e.code,
)
# Shell context default exports
@app.shell_context_processor
def make_shell_context():
return {
"db": md.db,
"Role": md.Role,
"Schedule": md.Schedule,
"Link": md.Link,
"User": md.User,
"oUser": md.OldUser,
"Role": md.Role,
"Usage": md.Usage,
"Api": md.ApiUsage,
"mng": app.config["MANAGER"],
}