apis.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:Weppy 作者: seniorcandy 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号