Exemple #1
0
def upload_house_image():

    if request.method == 'POST':
        image = request.files.get('house_image')
        if not image:
            REQUEST_FAIL['results'] = '请选择图片!'

        PATH = os.path.join(MEDIA_PATH, 'ihomeimge')

        filename = allowed_file(image.filename)

        if not filename:
            return jsonify(NOT_ALLOWED_FILES)

        path = os.path.join(PATH, filename)

        image.save(path)

        house_id = session.get('house_id', '')
        if not house_id:
            return jsonify(REQUEST_FAIL)

        filename = f'ihomeimge/{filename}'

        house = House.query.filter(House.id == house_id).first()

        house_img = HouseImage()
        house_img.house_id = house_id
        house_img.url = filename
        if not house.index_image_url:
            house.index_image_url = filename
        house_img.save()
        REQUEST_SUCCESS['result'] = filename
        return jsonify(REQUEST_SUCCESS)
Exemple #2
0
def new_house_img():
    # 创建房屋图片对象
    house_image = HouseImage()
    # 获取房屋对象
    house = House.query.filter(House.id == session['new_house_id']).first()
    # 获取房屋图片
    house_img = request.files['house_image']
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    UPLOAD_FOLDER = os.path.join(os.path.join(BASE_DIR, 'static'), 'upload')
    url = os.path.join(UPLOAD_FOLDER, house_img.filename)
    house_img.save(url)
    # 保存房屋图片
    house_image.house_id = house.id
    house_image.url = os.path.join('/static/upload', house_img.filename)
    house_image.save()
    # 判断是否存在房源首页图片,没有则保存
    if not house.index_image_url:
        house.index_image_url = os.path.join('/static/upload',
                                             house_img.filename)
        house.save()
    return jsonify({'code': 200, 'msg': '请求成功'})