Exemplo n.º 1
0
def update_user_avatar():
    """修改用户头像:
    0.登录校验 @login_required
    :param  前端传来的image文件,g变量中的user_id
    1.上传到七牛云
    2.返回key,保存到数据库
    :return: 成功返回用户头像
    """
    # 获取图片数据
    avatar = request.files.get('avatar')
    user_id = g.user_id
    if not avatar:
        return jsonify(re_code=RET.PARAMERR, msg='图片不能为空')

    # 1.上传到七牛云
    key = upload_image(avatar.read())
    # 2.保存到数据库
    try:
        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.DBERR, msg='查询用户失败')
    if not user:
        return jsonify(re_code=RET.NODATA, msg='用户不存在')
    user.avatar_url = key
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.debug(e)
        db.session.rollback()
        return jsonify(re_code=RET.DBERR, msg='保存头像失败')
    # 拼接头像地址,返回前端
    avatar_url = constants.QINIU_DOMIN_PREFIX + user.avatar_url
    return jsonify(re_code=RET.OK, msg='上传头像成功', avatar_url=avatar_url)
Exemplo n.º 2
0
def update_user_avatar():
    """修改用户头像:
    0.登录校验 @login_required
    :param  前端传来的image文件,g变量中的user_id
    1.上传到七牛云
    2.返回key,保存到数据库
    :return: 成功返回用户头像
    """
    # 获取图片数据
    image_file = request.files.get('avatar')

    # 在验证登陆的装饰器里设置了g的user_id属性
    user_id = g.user_id
    if not image_file:
        return jsonify(re_code=RET.PARAMERR, msg='图片不能为空')

    print(1)
    # 1.上传到七牛云
    image_data = image_file.read()
    try:
        # 上传的文件名
        file_name = upload_image(image_data)
    except Exception as e:
        current_app.logger.error(e)
        jsonify(re_code=RET.THIRDERR, msg='上传图片失败')
    # 2.保存到数据库
    print(2)
    try:
        user = User.query.get(user_id)
        # User.query.filter_by(id=user_id).update({"avatar_url":file_name})
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.DBERR, msg='查询用户失败')
    if not user:
        return jsonify(re_code=RET.NODATA, msg='用户不存在')
    # 数据库保存文件名
    print(3)
    user.avatar_url = file_name
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.debug(e)
        db.session.rollback()
        return jsonify(re_code=RET.DBERR, msg='保存头像失败')
    print(4)
    # 拼接头像地址,返回前端
    avatar_url = constants.QINIU_DOMIN_PREFIX + user.avatar_url

    return jsonify(re_code=RET.OK, msg='上传头像成功', avatar_url=avatar_url)
Exemplo n.º 3
0
def upload_house_image(house_id):
    """上传房屋图片"""
    #1.获取图片信息
    house_image = request.files.get('house_image')
    # 3.查询当前房屋是否有房屋主图片,没有就为其添加
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.DBERR, msg='查询房屋失败')
    if not house:
        return jsonify(re_code=RET.NODATA, msg='无该房屋')

    if not house_image:
        return jsonify(re_code=RET.PARAMERR, msg='图片不能为空')

    # 读取图片数据
    try:
        house_image_data = house_image.read()
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.DATAERR, msg='数据读取错误')

    #2.上传到七牛云,返回key
    try:
        key = upload_image(house_image_data)
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.THIRDERR, msg='上传房屋图片失败')

    # if not house.index_image_url:
    house.index_image_url = key
    #4.添加数据到当前house_id的House_Image模型中
    house_image = HouseImage()
    house_image.house_id = house_id
    house_image.url = key
    try:
        db.session.add(house_image)
        db.session.commit()
    except Exception as e:
        current_app.logger.debug(e)
        db.session.rollback()
        return jsonify(re_code=RET.DBERR, msg='保存房屋图片失败')
    #5.响应数据
    return jsonify(re_code=RET.OK,
                   msg='上传图片成功',
                   data={'url': constants.QINIU_DOMIN_PREFIX + key})
Exemplo n.º 4
0
def update_user_avatar():
    """修改用户头像"""
    # 获取图片数据
    avatar = request.files.get('avatar')
    user_id = g.user_id
    # 判断用户是否存在
    try:
        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.DBERR, msg='查询用户失败')
    if not user:
        return jsonify(re_code=RET.NODATA, msg='用户不存在')

    if not avatar:
        return jsonify(re_code=RET.PARAMERR, msg='图片不能为空')
    # 读取图片数据
    try:
        image_data = avatar.read()
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.DATAERR, msg='数据读取错误')
    # 调用七牛云
    try:
        image_name = upload_image(image_data)
        print(image_name)
    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(re_code=RET.THIRDERR, msg='上传图片异常')
    # 2.保存到数据库

    user.avatar_url = image_name
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.debug(e)
        db.session.rollback()
        return jsonify(re_code=RET.DBERR, msg='保存头像失败')
    # 拼接头像地址,返回前端
    avatar_url = constants.QINIU_DOMIN_PREFIX + user.avatar_url
    # 定义字典数据
    data = {'avatar_url': avatar_url}
    return jsonify(re_code=RET.OK, msg='上传头像成功', data=data)