本文整理汇总了Python中www.models.User.findAll方法的典型用法代码示例。如果您正苦于以下问题:Python User.findAll方法的具体用法?Python User.findAll怎么用?Python User.findAll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类www.models.User
的用法示例。
在下文中一共展示了User.findAll方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: api_register_user
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def api_register_user(*, email, name, passwd): # https://www.python.org/dev/peps/pep-3102/
if not name or not name.strip():
raise APIValueError('name')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
users = yield from User.findAll('email=?', [email])
if len(users) > 0:
raise APIError('register:failed', 'email', 'email already in use')
uid = next_id()
sha1_passwd = '%s:%s' % (uid, passwd) # get SHA1 for uid+passwd
user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),
image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
yield from user.save()
# make session cookie:
r = web.Response()
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.passwd = '******'
r.content_type = 'application/json'
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
示例2: api_register_user
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def api_register_user(*, email, name, passwd):
#判断name是否为空:
if not name or not name.strip():
raise APIValueError('name')
#判断email是否为空及是否满足email格式:
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
#判断password首付为空及是否满足password格式:
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
#数据中查询对应的email信息:
users = yield from User.findAll('email=?', [email])
#判断查询结果是否存在,若存在则返回异常提示邮件已存在:
if len(users) > 0:
raise APIError('register:failed', 'email', 'Email is already in use.')
#生成唯一ID:
uid = next_id()
#重构唯一ID和password成新的字符串:
sha1_passwd = '%s:%s' % (uid, passwd)
#构建用户对象信息:
#hashlib.sha1().hexdigest():取得SHA1哈希摘要算法的摘要值。
user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www(first).gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
#将用户信息存储到数据库:
yield from user.save()
# make session cookie:
#构造session cookie信息:
r = web.Response()
#aiohttp.web.StreamResponse().set_cookie():设置cookie的方法。
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) #max_age:定义cookie的有效期(秒);
user.passwd = '******'
r.content_type = 'application/json'
#以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
示例3: authenticate
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def authenticate(*, email, passwd):
#判断email(用户名)及password是否为空;为空则抛出异常:
if not email:
raise APIValueError('email', 'Invalid email.')
if not passwd:
raise APIValueError('passwd', 'Invalid password.')
#数据中查询对应的email信息:
users = yield from User.findAll('email=?', [email])
#判断查询结果是否存在,若不存在则抛出异常:
if len(users) == 0:
raise APIValueError('email', 'Email not exist.')
#获取查询结果集的第一条数据:
user = users[0]
# check passwd:
#调用摘要算法SHA1组装登陆信息;计算摘要值同数据库中的信息进行比配:
sha1 = hashlib.sha1()
sha1.update(user.id.encode('utf-8'))
sha1.update(b':')
sha1.update(passwd.encode('utf-8'))
if user.passwd != sha1.hexdigest():
#登陆信息不匹配则跑出异常:
raise APIValueError('passwd', 'Invalid password.')
# authenticate ok, set cookie:
#构造session cookie信息:
r = web.Response()
#aiohttp.web.StreamResponse().set_cookie():设置cookie的方法。
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.passwd = '******'
r.content_type = 'application/json'
#以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
示例4: api_get_users
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def api_get_users(*, page='1'):
#获取页面索引,默认为1:
page_index = get_page_index(page)
#查询数据库中User表中用户总数:
num = yield from User.findNumber('count(id)')
p = Page(num, page_index)
if num == 0:
return dict(page=p, users=())
#查询数据库中User表中对应分页的用户结果;(limit为mysql的分页查询条件)
users = yield from User.findAll(orderBy='created_at desc', limit=(p.offset, p.limit))
for u in users:
u.passwd = '******'
return dict(page=p, users=users)
示例5: api_register_user
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def api_register_user(*, email, name, passwd):
if not name or not name.strip():
raise APIValueError('name')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
users = yield from User.findAll('email=?', [email])
if len(users) > 0:
raise APIError('register:failed', 'email', 'Email is already in use.')
uid = next_id()
sha1_password = '%s:%s' % (uid, passwd)
user = User(id=uid, name=name.strip(), email=email,passwd=hashlib.sha1(sha1_password.encode('utf-8').hexdigest(),
image=''))
yield from user.save()
r = web.Response()
r.set_cookie(COOKIE_NAME,user2cookie(user, 86400), max_age=86400, httponly=True)
user.passwd = '******'
r.content_type = 'application/json'
r.body = json.dumps(user,ensure_ascii=False).enconde('utf-8')
return r
示例6: getUsers
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def getUsers():
users = yield from User.findAll()
print(users)
示例7: api_get_users
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def api_get_users():
users = yield from User.findAll()
for u in users:
u.passwd = '******'
return dict(users=users)
示例8: api_get_users
# 需要导入模块: from www.models import User [as 别名]
# 或者: from www.models.User import findAll [as 别名]
def api_get_users():
users = yield from User.findAll(orderBy='created_at desc')
for u in users:
u.passwd = '******'
return dict(users=users)