mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-07-30 15:25:19 +00:00
Create and use a decorator for background functions
This commit is contained in:
@@ -1,16 +1,38 @@
|
||||
from flask import abort
|
||||
from flask import abort, current_app
|
||||
from flask_login import current_user
|
||||
from flask_socketio import disconnect
|
||||
from functools import wraps
|
||||
from .models import Permission
|
||||
from threading import Thread
|
||||
|
||||
|
||||
def admin_required(f):
|
||||
@wraps(f)
|
||||
def wrapped(*args, **kwargs):
|
||||
if not current_user.can(Permission.ADMIN):
|
||||
if current_user.is_administrator:
|
||||
return f(*args, **kwargs)
|
||||
else:
|
||||
abort(403)
|
||||
return f(*args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
def background(f):
|
||||
''' This decorator executes a function in a Thread '''
|
||||
@wraps(f)
|
||||
def wrapped(*args, **kwargs):
|
||||
app = current_app._get_current_object()
|
||||
thread = Thread(target=f, args=(app, *args), kwargs=kwargs)
|
||||
thread.start()
|
||||
return thread
|
||||
return wrapped
|
||||
|
||||
|
||||
def socketio_admin_required(f):
|
||||
@wraps(f)
|
||||
def wrapped(*args, **kwargs):
|
||||
if current_user.is_administrator:
|
||||
return f(*args, **kwargs)
|
||||
else:
|
||||
disconnect()
|
||||
return wrapped
|
||||
|
||||
|
||||
@@ -22,13 +44,3 @@ def socketio_login_required(f):
|
||||
else:
|
||||
return f(*args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
def socketio_admin_required(f):
|
||||
@wraps(f)
|
||||
def wrapped(*args, **kwargs):
|
||||
if not current_user.can(Permission.ADMIN):
|
||||
disconnect()
|
||||
else:
|
||||
return f(*args, **kwargs)
|
||||
return wrapped
|
||||
|
Reference in New Issue
Block a user