Skip to content

Instantly share code, notes, and snippets.

@bugkiwi
Last active February 9, 2017 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugkiwi/5d84822a5b0d707c53d8 to your computer and use it in GitHub Desktop.
Save bugkiwi/5d84822a5b0d707c53d8 to your computer and use it in GitHub Desktop.
a flask request validate decorator~
def not_empty(*params,**kkargs):
if (params is None or params == []) and len(kargs) == 0:
raise Exception("You should give me some params;")
def wrapper(f):
@wraps(f)
def wrapped(*args,**kargs):
for p in params:
if g.args.get(p,'') == '':
return error(CODE.COMMON.PARAM_NOT_EMPTY,"参数不存在:%s"%p)
for k,v in kkargs.iteritems():
if request.method.lower() == k.lower():
if isinstance(v,(tuple,list)):
for p in v:
if g.args.get(p,'') == '':
return error(CODE.COMMON.PARAM_NOT_EMPTY,"参数不存在:%s"%p)
elif isinstance(v,(str,unicode)):
if g.args.get(v,'') == '':
return error(CODE.COMMON.PARAM_NOT_EMPTY,"参数不存在:%s"%p)
return f(*args,**kargs)
return wrapped
return wrapper
@bugkiwi
Copy link
Author

bugkiwi commented Apr 15, 2015

a example~

@bp.route('/order/comment',methods=['GET','POST'])
@login_required
@not_empty('orderId',POST=['servicesc','envsc','tastesc','foods'])
def order_comment():
    pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment