本文整理匯總了Python中www.models.User.passwd方法的典型用法代碼示例。如果您正苦於以下問題:Python User.passwd方法的具體用法?Python User.passwd怎麽用?Python User.passwd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類www.models.User
的用法示例。
在下文中一共展示了User.passwd方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: api_register_user
# 需要導入模塊: from www.models import User [as 別名]
# 或者: from www.models.User import passwd [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 passwd [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: api_register_user
# 需要導入模塊: from www.models import User [as 別名]
# 或者: from www.models.User import passwd [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