Move api_wrapper to decorators.py
This commit is contained in:
parent
08b9a84c84
commit
1e911c3f20
6 changed files with 23 additions and 26 deletions
|
@ -1,4 +1,4 @@
|
||||||
import admin
|
import admin
|
||||||
import models
|
import models
|
||||||
import user
|
import user
|
||||||
import utils
|
import utils
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from utils import api_wrapper
|
from decorators import api_wrapper
|
||||||
|
|
||||||
blueprint = Blueprint("admin", __name__)
|
blueprint = Blueprint("admin", __name__)
|
||||||
|
|
|
@ -10,12 +10,29 @@ def login_required(f):
|
||||||
def admins_only(f):
|
def admins_only(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated_function(*args, **kwargs):
|
def decorated_function(*args, **kwargs):
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
||||||
def check_csrf(f):
|
def check_csrf(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
@login_required
|
@login_required
|
||||||
def wrapper(*args, **kwds):
|
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
|
return wrapper
|
||||||
|
|
|
@ -2,7 +2,7 @@ from flask import Blueprint, session, request
|
||||||
from flask import current_app as app
|
from flask import current_app as app
|
||||||
|
|
||||||
from models import db, Users
|
from models import db, Users
|
||||||
from utils import api_wrapper
|
from decorators import api_wrapper
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
|
|
@ -24,20 +24,3 @@ def unix_time_millis(dt):
|
||||||
|
|
||||||
def get_time_since_epoch():
|
def get_time_since_epoch():
|
||||||
return unix_time_millis(datetime.datetime.now())
|
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
|
|
||||||
|
|
|
@ -5,9 +5,6 @@ import config
|
||||||
import json
|
import json
|
||||||
import api
|
import api
|
||||||
|
|
||||||
from api.api import api as api_blueprint
|
|
||||||
from api.user import blueprint as user_blueprint
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
|
|
Loading…
Reference in a new issue