Last active
February 9, 2017 04:54
-
-
Save bugkiwi/5d84822a5b0d707c53d8 to your computer and use it in GitHub Desktop.
a flask request validate decorator~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a example~