先看代码,代码中有两个函数都被testwra装饰器装饰,并且这两个函数一个有参数,一个没有参数,注意参数传递方法:
def users2dict(user):
userdict = {}
for name in dir(user):
value = getattr(user, name)
if not name.startswith('__') and not callable(value) and not name.startswith('_'):
userdict[name] = value
return userdict
def testwra(func):
@functools.wraps(func)
def _wrapper(*args, **kwargs):
alluserlist = []
allusers = func(*args, **kwargs)
if type(allusers) == type(users()):
allusers.password = '******'
userdict = users2dict(allusers)
alluserlist.append(userdict)
else:
for u in allusers:
u.password = '******'
userdict = users2dict(u)
alluserlist.append(userdict)
return json.dumps(alluserlist)
return _wrapper
@app.route('/myinfo/<username>', methods=['GET'])
@testwra
def myinfohtml(username):
# print "username", username
if len(username) != 0:
dbengine.connection(**dbconnect)
u = users()
user = u.find_first('name', username)
dbengine.closeconnection()
else:
user = users()
print "内部错误!"
return user
@app.route('/users', methods=['GET'])
@testwra
def get_users():
dbengine.connection(**dbconnect)
u = users()
findby = 'order by created_at desc'
allusers = u.find_by(findby)
dbengine.closeconnection()
return allusers