Move api_wrapper to decorators.py

This commit is contained in:
James Wang 2015-12-26 19:19:31 -05:00
parent 08b9a84c84
commit 1e911c3f20
6 changed files with 23 additions and 26 deletions

View file

@ -1,4 +1,4 @@
import admin
import models
import user
import utils
import utils

View file

@ -1,4 +1,4 @@
from flask import Blueprint
from utils import api_wrapper
from decorators import api_wrapper
blueprint = Blueprint("admin", __name__)
blueprint = Blueprint("admin", __name__)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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():