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 = await User.findAll('email=?',[email])
if len(users) > 0:
raise APIError('register:failed','email','Email is already in use')
uid = next_id()
sha1_passwd = '%s:%s' % (uid,passwd)
encrypt_passwd = hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest()
image = 'https://1.gravatar.com/avatar/%s?s=200&r=pg&d=mm'
user = User(id=uid,name=name.strip(),email=email,passwd=encrypt_passwd,image=image % hashlib.md5(email.encode('utf-8')).hexdigest())
await 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
python类next_id()的实例源码
def api_register_user(*, email, name, password):
if not name or not name.strip():
raise APIValueError('name')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
if not password or not _RE_SHA1.match(password):
raise APIValueError('password')
users = yield from User.find_all('email=?', [email])
if len(users) > 0:
raise APIError('Register failed', 'email', 'Email is already in use.')
uid = next_id()
sha1_password = '{}:{}'.format(uid, password)
logging.info('register password:{}, sha1_password:{}'.format(password, sha1_password))
user = User(id=uid, name= name.strip(), email= email, password = hashlib.sha1(sha1_password.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/{}?d=mm&s=120'.format(hashlib.md5(email.encode('utf-8')).hexdigest()))
yield from user.save()
r = web.Response()
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.password = '*' * 8
r.content_type = 'application/json'
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
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_passwd = '%s:%s' % (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
def api_register_user(*, email, name, passwd):
'''
????,?????cookie
'''
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 = await User.findAll('email=?', [email])
if len(users) > 0:
raise APIError('register:failed', 'email', 'Email is already in use.')
uid = next_id()
sha1_passwd = '%s:%s' % (uid, passwd)
# image ????????gravatar??????
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())
await 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
def api_user_sign_up(*, name, password):
"""
password = sha1((uid + ':' + password).encode(utf-8))
"""
if not name or not name.strip():
raise APIValueError('name')
if not password or not _RE_SHA1.match(password):
raise APIValueError('password')
all_users = yield from User.find_all('name=?', (name,))
if len(all_users):
raise APIError('sign up failed', 'name', 'User name already exist')
uid = next_id()
sha1_password = '%s:%s' % (uid, password)
password = hashlib.sha1(sha1_password.encode('utf-8')).hexdigest()
user = User(id=uid, name=name.strip(), password=password, is_admin=True)
yield from user.save()
r = web.Response()
cookie_name = configs['cookie_name']
r.set_cookie(
cookie_name, generate_cookie(user, 86400), max_age=86400, httponly=True
)
user.password = '******'
r.content_type = 'application/json'
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
def api_register_user(*, email, name, passwd):
if not name or not name.strip():
raise APIValueError('name', 'Invalid name.')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email', 'Invalid email.')
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd', 'Invalid password.')
users = await User.findAll('email=?', [email])
if len(users) > 0:
raise APIError('register: failed', 'email', 'Email is already in use.')
uid = next_id()
sha1_passwd = '%s:%s' % (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()
)
await 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
def api_register_user(*, email, name, passwd): # ??????????????
# ????????
# ?????name
if not name or not name.strip(): # s.strip(rm)??????s?????????????rm???????
# ??rm????????????
raise APIValueError('name')
# ??email?????????????
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
# ??passwd???SHA1??????????
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
# ?????????????email
users = yield from User.findAll('email=?', [email])
# users??????????????????email???????
if len(users) > 0:
raise APIError('register:failed', 'email', 'Email is already in use.')
# ???????email???????????
uid = next_id() # next_id?models????????????????????id??????????????
sha1_passwd = '%s:%s' % (uid, passwd) # ???id?????
# ????????????????????
# unicode???????????????????utf8??
# hashlib.sha1()??????????sha1?
# hash.hexdigest()???hash?????16????????
# ?????sha1?????????md5??
# Gravatar???????????????????????????
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()
# ?????????cookie(???????????????????????)
# http???????????,?????????????????.
# ??????????????Cookies?????,????????????????
# user2cookie????cookie?????????
# max_age?cookie???????,????.??????,???????cookie.????????
# ?????????24??
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.passwd = '******' # ??????????*
# ??content_type???data_factory????????
r.content_type = 'application/json'
# json.dump?????????json??
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
# API?????
def api_register_user(*, email, name, passwd): # ??????????????
# ????????
# ?????name
if not name or not name.strip(): # s.strip(rm)??????s?????????????rm???????
# ??rm????????????
raise APIValueError('name')
# ??email?????????????
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
# ??passwd???SHA1??????????
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
# ?????????????email
users = yield from User.findAll('email=?', [email])
# users??????????????????email???????
if len(users) > 0:
raise APIError('register:failed', 'email', 'Email is already in use.')
# ???????email???????????
uid = next_id() # next_id?models????????????????????id??????????????
sha1_passwd = '%s:%s' % (uid, passwd) # ???id?????
# ????????????????????
# unicode???????????????????utf8??
# hashlib.sha1()??????????sha1?
# hash.hexdigest()???hash?????16????????
# ?????sha1?????????md5??
# Gravatar???????????????????????????
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()
# ?????????cookie(???????????????????????)
# http???????????,?????????????????.
# ??????????????Cookies?????,????????????????
# user2cookie????cookie?????????
# max_age?cookie???????,????.??????,???????cookie.????????
# ?????????24??
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.passwd = '******' # ??????????*
# ??content_type???data_factory????????
r.content_type = 'application/json'
# json.dump?????????json??
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
# API?????