libertywiki/libertywiki/decorators.py

34 lines
1.1 KiB
Python

from functools import wraps
from flask import current_app, request
from flask_login import current_user
from flask_login.config import EXEMPT_METHODS
from libertywiki.utils import WikiMode, needs_authentication
def check_access(access_type):
"""Check if a user can access the view method"""
def wrapper(func):
"""Decorator wrapper"""
@wraps(func)
def decorated_view(*args, **kwargs):
"""Determine if the decorated view should be run"""
wiki_mode = WikiMode[current_app.config.get('WIKI_MODE', 'OPEN').upper()]
print(wiki_mode)
if request.method in EXEMPT_METHODS or \
current_app.config.get('LOGIN_DISABLED'):
pass
elif needs_authentication(wiki_mode, access_type) \
and not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
try:
return current_app.ensure_sync(func)(*args, **kwargs)
except AttributeError:
return func(*args, **kwargs)
return decorated_view
return wrapper