2020-05-26 12:29:01 +00:00
|
|
|
from . import socketio
|
2020-05-20 07:36:59 +00:00
|
|
|
from flask import abort, current_app, request
|
2019-07-09 13:41:16 +00:00
|
|
|
from flask_login import current_user
|
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):
|
2020-04-23 07:40:43 +00:00
|
|
|
'''
|
|
|
|
' This decorator executes a function in a Thread.
|
|
|
|
' Decorated functions need to be executed within a code block where an
|
|
|
|
' app context exists.
|
|
|
|
'
|
|
|
|
' NOTE: An app object is passed as a keyword argument to the decorated
|
|
|
|
' function.
|
|
|
|
'''
|
2020-03-26 15:14:09 +00:00
|
|
|
@wraps(f)
|
|
|
|
def wrapped(*args, **kwargs):
|
2020-04-23 06:35:18 +00:00
|
|
|
kwargs['app'] = current_app._get_current_object()
|
|
|
|
thread = Thread(target=f, args=args, kwargs=kwargs)
|
2020-04-21 16:34:21 +00:00
|
|
|
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:
|
2020-05-20 07:36:59 +00:00
|
|
|
response = {'code': 401, 'desc': 'Unauthorized'}
|
|
|
|
socketio.emit(request.event['message'], response, room=request.sid)
|
2020-04-21 16:34:21 +00:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
def socketio_login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapped(*args, **kwargs):
|
2020-05-20 07:36:59 +00:00
|
|
|
if current_user.is_authenticated:
|
2020-03-26 15:14:09 +00:00
|
|
|
return f(*args, **kwargs)
|
2020-05-20 07:36:59 +00:00
|
|
|
else:
|
|
|
|
response = {'code': 401, 'desc': 'Unauthorized'}
|
|
|
|
socketio.emit(request.event['message'], response, room=request.sid)
|
2020-03-26 15:14:09 +00:00
|
|
|
return wrapped
|