From 1e911c3f2067e6cd532771bc4a714a2258885e70 Mon Sep 17 00:00:00 2001 From: James Wang Date: Sat, 26 Dec 2015 19:19:31 -0500 Subject: [PATCH] Move api_wrapper to decorators.py --- server/api/__init__.py | 2 +- server/api/admin.py | 4 ++-- server/api/decorators.py | 21 +++++++++++++++++++-- server/api/user.py | 2 +- server/api/utils.py | 17 ----------------- server/app.py | 3 --- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/server/api/__init__.py b/server/api/__init__.py index 0876fc5..f0dd712 100644 --- a/server/api/__init__.py +++ b/server/api/__init__.py @@ -1,4 +1,4 @@ import admin import models import user -import utils \ No newline at end of file +import utils diff --git a/server/api/admin.py b/server/api/admin.py index 4f72f9f..4c734c9 100644 --- a/server/api/admin.py +++ b/server/api/admin.py @@ -1,4 +1,4 @@ from flask import Blueprint -from utils import api_wrapper +from decorators import api_wrapper -blueprint = Blueprint("admin", __name__) \ No newline at end of file +blueprint = Blueprint("admin", __name__) diff --git a/server/api/decorators.py b/server/api/decorators.py index 9164a8f..2e11a0d 100644 --- a/server/api/decorators.py +++ b/server/api/decorators.py @@ -10,12 +10,29 @@ def login_required(f): def admins_only(f): @wraps(f) def decorated_function(*args, **kwargs): - return f(*args, **kwargs) + return f(*args, **kwargs) return decorated_function def check_csrf(f): @wraps(f) @login_required def wrapper(*args, **kwds): - return f(*args, **kwds) + 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" } return wrapper diff --git a/server/api/user.py b/server/api/user.py index 4efd811..17de908 100644 --- a/server/api/user.py +++ b/server/api/user.py @@ -2,7 +2,7 @@ from flask import Blueprint, session, request from flask import current_app as app from models import db, Users -from utils import api_wrapper +from decorators import api_wrapper import logging import requests diff --git a/server/api/utils.py b/server/api/utils.py index f638462..21a5c91 100644 --- a/server/api/utils.py +++ b/server/api/utils.py @@ -24,20 +24,3 @@ def unix_time_millis(dt): def get_time_since_epoch(): return unix_time_millis(datetime.datetime.now()) - -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" } - return wrapper diff --git a/server/app.py b/server/app.py index bd9c84d..e5d34df 100644 --- a/server/app.py +++ b/server/app.py @@ -5,9 +5,6 @@ import config import json import api -from api.api import api as api_blueprint -from api.user import blueprint as user_blueprint - app = Flask(__name__) with app.app_context():