本文整理匯總了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)