def register(): data, error = ones_deserialize(NewUserSchema, request.values.to_dict()) if error: return ones_json(code=402, error_mesg='账户名或密码需要再长一点, \ 账户名不少于3位, 密码不少于6位') data['password'] = User.generate_hash(data['password']) user = ones_model_add(User, data) # user_info = UserInfo(meta_id=user.id) # db.session.add(user_info) # db.session.commit() return ones_json(user=user.json())
def update_user(): user = g.user data, error = ones_deserialize(UpdateUserSchema, request.values.to_dict()) if error: return ones_json(code=402, error_mesg=str(error)) if data['password']!=None: data['password'] = User.generate_hash(data['password']) _meta_values = dict([(key, data[key]) for key in ['user_name', 'avatar', 'nick_name', 'password', \ 'introduction', 'gender']]) user = ones_model_update(user, _meta_values) return ones_json(user=user.json(with_info=True))
def login(): data, error = ones_deserialize(LoginSchema, request.values.to_dict()) if error: return ones_json(code=402, error_mesg=str(error)) user = User.query.filter(and_(User.user_name==data['user_name'], \ User.password==User.generate_hash(data['password']))).first() if not user: return ones_json(code=3003) authutil.login(user) _user = user.json() return ones_json(user=_user)
def logout(): authutil.logout(g.user) g.user.token = uuid.uuid1().hex db.session.commit() return ones_json()
def handler_404(error): resp = ones_json(code=404, error_mesg="内容不存在") resp.status_code = 404 return resp