easyctf-2017/server/api/decorators.py

39 lines
1.1 KiB
Python
Raw Normal View History

2015-12-23 01:45:50 +00:00
from functools import wraps
from flask import session
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
return decorated_function
def admins_only(f):
@wraps(f)
def decorated_function(*args, **kwargs):
2015-12-27 00:19:31 +00:00
return f(*args, **kwargs)
2015-12-23 01:45:50 +00:00
return decorated_function
def check_csrf(f):
@wraps(f)
@login_required
def wrapper(*args, **kwds):
2015-12-27 00:19:31 +00:00
return f(*args, **kwds)
return wrapper
def api_wrapper(f):
@wraps(f)
def wrapper(*args, **kwds):
web_result = {}
response = 200
try:
web_result = f(*args, **kwds)
except WebException as error:
response = 200
web_result = { "success": 0, "message": str(error) }
except Exception as error:
response = 200
traceback.print_exc()
web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", error: traceback.format_exc() }
return json.dumps(web_result), response, { "Content-Type": "application/json; charset=utf-8" }
2015-12-23 01:45:50 +00:00
return wrapper