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() push_option = PushOption( user_id = uid, user_email = email, posts_type = '全部'.encode( 'utf-8' ), keywords = '' ) yield from push_option.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 test( loop ): yield from orm.create_pool( loop = loop, user='******', password='******', dbname='personalblog' ) users = yield from User.findAll() for user in users: push_option = PushOption( user_id = user.id, user_email = user.email, posts_type = '全部'.encode( 'utf-8' ), keywords = '' ) yield from push_option.save()