2020-04-21 16:34:21 +00:00
|
|
|
from flask import abort, current_app
|
2019-07-09 13:41:16 +00:00
|
|
|
from flask_login import current_user
|
2020-03-26 15:14:09 +00:00
|
|
|
from flask_socketio import disconnect
|
2020-03-27 11:06:11 +00:00
|
|
|
from functools import wraps
|
2020-04-21 16:34:21 +00:00
|
|
|
from threading import Thread
|
2019-07-09 13:41:16 +00:00
|
|
|
|
|
|
|
|
2020-03-26 15:14:09 +00:00
|
|
|
def admin_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapped(*args, **kwargs):
|
2020-04-21 16:34:21 +00:00
|
|
|
if current_user.is_administrator:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
else:
|
2020-03-26 15:14:09 +00:00
|
|
|
abort(403)
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2020-04-21 16:34:21 +00:00
|
|
|
def background(f):
|
|
|
|
''' This decorator executes a function in a Thread '''
|
2020-03-26 15:14:09 +00:00
|
|
|
@wraps(f)
|
|
|
|
def wrapped(*args, **kwargs):
|
2020-04-21 16:34:21 +00:00
|
|
|
app = current_app._get_current_object()
|
|
|
|
thread = Thread(target=f, args=(app, *args), kwargs=kwargs)
|
|
|
|
thread.start()
|
|
|
|
return thread
|
2020-03-26 15:14:09 +00:00
|
|
|
return wrapped
|
2019-07-09 13:41:16 +00:00
|
|
|
|
|
|
|
|
2020-03-26 15:14:09 +00:00
|
|
|
def socketio_admin_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapped(*args, **kwargs):
|
2020-04-21 16:34:21 +00:00
|
|
|
if current_user.is_administrator:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
disconnect()
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
def socketio_login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated:
|
2020-03-26 15:14:09 +00:00
|
|
|
disconnect()
|
|
|
|
else:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return wrapped
|