2015-12-31 02:56:00 +00:00
|
|
|
import json
|
|
|
|
import traceback
|
|
|
|
|
2015-12-23 01:45:50 +00:00
|
|
|
from functools import wraps
|
|
|
|
from flask import session
|
|
|
|
|
2015-12-31 02:56:00 +00:00
|
|
|
class WebException(Exception): pass
|
|
|
|
|
2015-12-23 01:45:50 +00:00
|
|
|
def admins_only(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2016-01-02 18:40:56 +00:00
|
|
|
if "admin" not in session and not session["admin"]:
|
|
|
|
return { "success": 0, "message": "Not authorized." }
|
2015-12-27 00:19:31 +00:00
|
|
|
return f(*args, **kwargs)
|
2015-12-23 01:45:50 +00:00
|
|
|
return decorated_function
|
|
|
|
|
2015-12-27 00:19:31 +00:00
|
|
|
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()
|
2015-12-31 02:56:00 +00:00
|
|
|
web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", str(error): traceback.format_exc() }
|
2015-12-27 00:19:31 +00:00
|
|
|
return json.dumps(web_result), response, { "Content-Type": "application/json; charset=utf-8" }
|
2015-12-23 01:45:50 +00:00
|
|
|
return wrapper
|