def change_user_avatar(user_id): get_user = Users.query.get_or_404(user_id) # 获取请求中上传的文件 file = request.files['avatar'] # 验证图片类型 if not allowed_pic_file(file.content_type): return jsonify({'status_code': 400, 'status_message': '图片不符合要求'}) # 对图片文件名安全加密 # filename = secure_filename(file.filename) filename = str(uuid.uuid4()).replace('-', '') # 生成uuid代替文件名 filename = '%s.%s' % (filename, file.filename.split('.')[-1]) # 保存文件 file.save(os.path.join(current_app.config['UPLOAD_PATH'], filename)) # 生成图片链接 avatar_url = url_for('users_api.get_avatar_by_filename', filename=filename, _external=True) get_user.avatar = avatar_url db.session.add(get_user) db.session.commit() users_schema = UsersSchema() user = users_schema.dump(get_user) return jsonify({ 'status_code': 200, 'status_message': 'avatar修改成功', 'data': user })
def get_user_score(user_id): get_user = Users.query.filter( and_( Users.status == 1, Users.avatar != None, Users.id.notin_( UsersScores.query.with_entities( UsersScores.voted_user_id).filter( UsersScores.user_id == user_id)))).first() users_schema = UsersSchema() user = users_schema.dump(get_user) return jsonify({'status_code': 200, 'status_message': '成功', 'data': user})
def create_user(): # 从请求里面获取json数据 json_data_str = request.get_data(as_text=True) json_data = json.loads(json_data_str) json_data['password'] = generate_password_hash(json_data['password']) users_schema = UsersSchema() # 将json数据转换成model user = users_schema.load(json_data) db.session.add(user) db.session.commit() user = users_schema.dump(user) return jsonify({'status_code': 200, 'status_message': '成功', 'data': user})
def update_user(id): json_data = request.get_json() get_user = Users.query.get(id) if 'fullname' in json_data: get_user.fullname = json_data['fullname'] if 'password' in json_data: get_user.password = generate_password_hash(json_data['password']) if 'status' in json_data: get_user.status = json_data['status'] if 'current_listen_status' in json_data: get_user.current_listen_status = json_data['current_listen_status'] db.session.add(get_user) db.session.commit() users_schema = UsersSchema() user = users_schema.dump(get_user) return jsonify({'status_code': 200, 'status_message': '成功', 'data': user})
def login(): json_data_str = request.get_data(as_text=True) json_data = json.loads(json_data_str) username = json_data['username'] user = Users.query.filter_by(username=username).first() if not user: return jsonify({'status_code': 403, 'status_message': '用户名或密码错误'}) if check_password_hash(user.password, json_data['password']): users_schema = UsersSchema() user = users_schema.dump(user) # 根据用户名创建jwt token access_token = create_access_token(identity=username) return jsonify({ 'status_code': 200, 'status_message': '登录成功', 'access_token': access_token, 'data': user }) return jsonify({'status_code': 403, 'status_message': '用户名或密码错误'})
def get_user_by_id(id): get_user = Users.query.get(id) users_schema = UsersSchema() user = users_schema.dump(get_user) return jsonify({'status_code': 200, 'status_message': '成功', 'data': user})
def index(): get_users = Users.query.all() users_schema = UsersSchema(many=True) # 把model数据变成一个Python列表 users = users_schema.dump(get_users) return jsonify({'status_code': 200, 'status_message': '成功', 'data': users})