Exemple #1
0
def api_user_register(*, name, password, email):
    if not name or not name.strip():
        raise APIValueError('name', 'name is Null')
    if not password or not password.strip():
        raise APIValueError('password', 'password is Null')
    if not email or not email.strip():
        raise APIValueError('email', 'email is Null')
    if not _RE_EMAIL.match(email):
        raise APIValueError('email: %s' % email, 'email is no format')
    users = (yield from User(email=email).find())['data']
    if len(users) > 0:
        raise APIValueError('email', 'email: %s is already in use' % email)
    uid = next_id()
    sha1_password = '******' % (uid, password)
    user = User(id=uid,
                name=name,
                password=hashlib.sha1(
                    sha1_password.encode('utf-8')).hexdigest(),
                email=email,
                image='./res/tumble.png')
    res = yield from user.save()
    rep = web.Response()
    rep.content_type = 'application/json'
    if res == 1:
        logging.info('save user sucessed')
        rep.set_cookie(_COOKIE_NAME,
                       user2cookie(user, 86400),
                       max_age=86400,
                       httponly=True)
        user.password = '******'
        rep.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    else:
        logging.error('user save error')
        raise APIError('register', 'save', 'register failed')

    return rep