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?????
评论列表
文章目录