def upload_image(): hid = request.form.get('house_id') img_list = request.files.getlist('house_image') # 验证图片格式 # if not re.match(r'image/.*', file.mimetype): # return jsonify(status_code.USER_IMAGE_FORMAT_ERROR) # 保存 file_dir = os.path.join(common.UPLOAD_DIR, 'house/' + hid) if not os.path.exists(file_dir): os.makedirs(file_dir) try: for img in img_list: img_path = os.path.join(file_dir, img.filename) img.save(img_path) himg = HouseImage() himg.house_id = hid himg.url = os.path.join('house/' + hid, img.filename) db.session.add(himg) house = House.query.filter_by(id=hid).first() house.index_image_url = os.path.join('house/' + hid, img_list[0].filename) db.session.add(house) db.session.commit() return jsonify(status_code.SUCCESS) except BaseException as e: print(e) db.session.rollback() return jsonify(status_code.DATABASE_ERROR)
def upload_house_pic(house_id): """ 上传房源图片 :param house_id: 房源id :return: """ # 1. 获取图片文件 image_file = request.files.get('house_image') if not image_file: return jsonify(errno=RET.PARAMERR, errmsg="未选择图片") # 2. 尝试查询房屋数据 try: house = House.query.filter_by(id=house_id).first() except Exception as e: logging.error(e) return jsonify(errno=RET.DBERR, errmsg='查询房屋数据失败') if not house: return jsonify(errno=RET.NODATA, errmsg='未查询到对应房屋') # 3. 使用七牛上传图片 image_data = image_file.read() try: image_name = storage(image_data) except Exception as e: logging.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传图片失败") # 4. 判断房屋是否有主图片,如果没有,则设置 if not house.index_image_url: house.index_image_url = image_name db.session.add(house) # 5. 生成房屋图片模型并保存至数据数据库 house_image = HouseImage() house_image.house_id = house_id house_image.url = image_name try: db.session.add(house_image) db.session.commit() except Exception as e: logging.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存房屋图片失败") # 删除缓存的前五房屋信息 try: redis_store.delete("home_page_house_info") except Exception as e: logging.error(e) db.session.rollback(e) return jsonify(errno=RET.DBERR, errmsg="删除首页房屋缓存失败") # 返回数据 image_url = constants.QINIU_DOMIN_PREFIX + image_name return jsonify(errno=RET.OK, errmsg='OK', data={'url': image_url})
def house_images(): BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取媒体文件路径 MEDIA_DIR = os.path.join(BASE_DIR, '/static/media/upload') # 接收图片 image = request.files['house_image'] # 获取图片名称 filename = image.filename # 保存图片 image.save('./static/media/upload/%s' % filename) # 获取当前房屋对象 house = HouseImage() house_id = request.form.get('house_id') house.house_id = house_id house.url = filename house.add_update() # 创建房屋首图 house = House.query.get(house_id) if not house.index_image_url: # house.index_image_url = image_url house.index_image_url = filename house.add_update() # first_house = House.query.get(house_id) # first_house.index_image_url = filename # first_house.add_update() return jsonify({'code': 200, 'image_url': filename})
def house_images(): if request.method == 'POST': images = HouseImage() house_id = request.form.get('house_id') house = House.query.filter(House.id == house_id).first() icon = request.files.get('house_image') path = os.path.join(IMAGE_PATH, icon.filename) icon.save(path) url = icon.filename images.house_id = house_id images.url = url house.index_image_url = url db.session.add(house) db.session.commit() db.session.add(images) db.session.commit() return jsonify({'code': 200})
def facility(): if request.method == 'GET': facility = Facility.query.all() facilitys = [] for facility_ in facility: facilitys.append(facility_.to_dict()) return jsonify({'code': 200, 'msg': '请求成功', 'facility': facilitys}) if request.method == 'PATCH': house_img = request.files.get('house_image') if house_img: path = os.path.join(MEDIA_PATH, house_img.filename) house_img.save(path) houseImg = HouseImage() house_id = session.get('house_id') house = House.query.filter(House.id == house_id).first() houseImg.house_id = house_id houseImg.url = house_img.filename houseImg.add_update() house.index_image_url = house_img.filename house.add_update() return jsonify({ 'code': 200, 'msg': '请求成功', 'img': house_img.filename }) return jsonify({'code': 1000, 'msg': '请求成功'})
def house_image(): img = request.files.get('house_image') # 获取用户 house_id = request.form.get('house_id') house = House.query.get(house_id) if img: # 保存save file_path = os.path.join(UPLOAD_DIR, img.filename) img.save(file_path) # 将图片保存到数据库 house_img = HouseImage() house_img.house_id = house_id house_img.url = img.filename house_img.add_update() # 判断有无首图 if not house.index_image_url: house.index_image_url = house_img.url house.add_update() # 判断房屋是否已存在 # if house: # return jsonify(status_code.HOUSE_IS_EXISTS) return jsonify(code=status_code.OK, img_url=house_img.url) else: return jsonify(status_code.HOUSE_IMAGE_IS_NOT_EXISTS)
def my_newhouse_info(): # 接收图片并保存图片 house_id = request.form.get('house_id') icon = request.files.get('house_image') if icon: # 获取项目根路径 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取媒体文件的路径 STSTIC_DIR = os.path.join(BASE_DIR, 'static') MEDIA_DIR = os.path.join(STSTIC_DIR, 'media') # 随机生成图片名称 filename = str(uuid.uuid4()) a = icon.mimetype.split('/')[-1:][0] name = filename + '.' + a # 拼接图片地址,并保存到/static/media/文件家中 path = os.path.join(MEDIA_DIR, name) icon.save(path) # print(icon) # 修改用户头像icon字段 image = HouseImage() image.house_id = house_id image.url = name image.add_update() house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = name house.add_update() return jsonify({'code': 200, 'msg': '请求成功', 'name': name})
def add_house_picture(): """ 添加新房屋图片 :return: """ image = request.files.get('house_image') house_id = request.form.get('house_id') house = House.query.get(house_id) if not re.match(r'image/*', image.mimetype): return jsonify({'code': 1004, 'msg': '请上传图片格式的文件'}) # 获取地址: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_DIR = os.path.join(BASE_DIR, 'static') IMAGE_DIR = os.path.join(STATIC_DIR, 'house_image') # 生成随机图片名 并保存到/static/house_image/路径下 filename = str(uuid.uuid4()) a = image.mimetype.split('/')[-1:][0] name = filename + '.' + a path = os.path.join(IMAGE_DIR, name) image.save(path) # 保存房屋图片字段: house_image = HouseImage() house_image.house_id = house_id house_image.url = name house_image.add_update() # 存储房屋主页图片 if not house.index_image_url: house.index_image_url = name house.add_update() return jsonify({'code':200,'msg':'请求成功!','url':'/static/house_image/'+ house_image.url})
def newhouse_pic(): dict = request.files #.get('house_image') house_id = request.form.get('house_id') if 'house_image' in dict: # 获取项目根路径 file = dict['house_image'] BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取媒体文件的路径 STATIC_DIR = os.path.join(BASE_DIR, 'static') STATIC_DIR1 = os.path.join(STATIC_DIR, 'media') MEDIA_DIR = os.path.join(STATIC_DIR1, 'house_image') url = os.path.join(MEDIA_DIR, file.filename) file.save(url) house = House.query.filter(House.id == house_id).first() url = 'house_image/' + file.filename houseimage = HouseImage() houseimage.url = url houseimage.house_id = house_id houseimage.add_update() if not (house.index_image_url): house.index_image_url = url house.add_update() return jsonify({'code': 200, 'msg': '房屋图片上传成功', 'image_url': url}) else: return jsonify({'code': 1017, 'msg': '房屋图片上传成功'})
def house_images(): house_id = request.form.get('house_id') image = request.files.get('house_image') if image: if not re.match(r'image/*', image.mimetype): return jsonify(status_code.HOUSE_NEW_IMAGE_INVALID) path = os.path.join(UPLOAD_PATH, image.filename) image.save(path) house_image = HouseImage() house_image.house_id = house_id image_url = os.path.join('upload', image.filename) house_image.url = image_url try: house_image.add_update() except: return jsonify(status_code.DATABASE_ERROR) house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = image_url try: house.add_update() except: return jsonify(status_code.DATABASE_ERROR) return jsonify(code=status_code.OK, image_url=image_url)
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)
def house_image(): house_id = request.form.get('house_id') image = request.files.get('house_image') # 保存图片 if not all([house_id, image]): return jsonify({'code': 1001,'msg': '请选择图片上传'}) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取媒体文件路径 STATIC_DIR = os.path.join(BASE_DIR, 'static') MEDIA_DIR = os.path.join(STATIC_DIR, 'media') filename = str(uuid.uuid4()) a = image.mimetype.split('/')[-1:][0] name = filename + '.' + a path = os.path.join(MEDIA_DIR, name) # 保存房屋图片到本地 image.save(path) # 保存房屋图片到数据库 img_url = '/static/media/'+name house_image = HouseImage() house_image.house_id = house_id house_image.url = img_url house_image.add_update() # 创建房屋图片 house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = img_url house.add_update() return jsonify({'code':200,'data':img_url})
def house_images(): house_id = request.form.get('house_id') house_image = request.files.get('house_image') image_path = os.path.join(UPLOAD_DIR, house_image.filename) house_image.save(image_path) # 图片路径 image_url = os.path.join('upload', house_image.filename) # 保存房屋的首图 house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = image_url house.add_update() h_image = HouseImage() h_image.house_id = house_id h_image.url = image_url try: h_image.add_update() except: db.session.rollback() return jsonify({'code': 0, 'msg': '数据库错误'}) return jsonify(code=200, image_url=image_url)
def image_house(): #接收房屋编号 house_id = request.form.get('house_id') #接收图片 h_image = request.files.get('house_image') #保存图片到images # 项目路径 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 上传图片地址 UPLOAD_FOLDER = os.path.join(os.path.join(BASE_DIR, 'static'), 'images') url = os.path.join(UPLOAD_FOLDER, h_image.filename) h_image.save(url) #保存到数据库 image = HouseImage() image.house_id = house_id image.url = os.path.join('/static/images', h_image.filename) image.add_update() #首图 house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = os.path.join('/static/images', h_image.filename) house.add_update() return jsonify(code=200, url=os.path.join('/static/images', h_image.filename))
def add_himg(): if request.method == 'POST': house_image = request.files.get('house_image') id = request.form.get('house_id') house = House.query.get(id) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_DIR = os.path.join(BASE_DIR, 'static') IMG_DIR = os.path.join(STATIC_DIR, 'images') filename = str(uuid.uuid4()) a = house_image.mimetype.split('/')[-1:][0] name = filename + '.' + a path = os.path.join(IMG_DIR, name) house_image.save(path) house.index_image_url = '/static/images/' + name house.add_update() himg = HouseImage() himg.house_id = id himg.url = '/static/images/' + name himg.add_update() # hg = HouseImage.query.filter_by(house_id=id).all() # h = [i.url for i in hg] # count = len(h) return jsonify({ 'code': 200, 'msg': '请求成功', 'data': '/static/images/' + name })
def my_newhouse_img(): # 获取图片 files = request.files.get('house_image') house_id = request.form.get('house_id') # 拼接图片完整地址 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_DIR = os.path.join(BASE_DIR, 'static') IMAGE_DIR = os.path.join(STATIC_DIR, 'images') filename = str(uuid.uuid4()) a = files.mimetype.split('/')[-1:][0] imgname = filename + '.' + a path = os.path.join(IMAGE_DIR, imgname) # 向image里面保存图片 files.save(path) # 向数据库保存图片 house = House.query.filter_by(id=house_id).first() image = HouseImage() image.house_id = house_id image.url = '/static/' + 'images/' + imgname image.add_update() index_image = house.index_image_url if not index_image: house.index_image_url = '/static/' + 'images/' + imgname house.add_update() return jsonify({'code': 200, 'msg': '请求成功'})
def image_house(): if request.method == 'POST': # 接收房屋编号 house_id = request.form.get('house_id') # 接收图片信息 f1 = request.files.get('house_image') # 保存到图片 con = Config() url = os.path.join(os.path.join(con.UPLOAD_FOLDER, 'house'), f1.filename) f1.save(url) # 保存图片对象 image = HouseImage() image.house_id = house_id image.url = os.path.join('/static/upload/house', f1.filename) image.add_update() # 房屋的默认图片 house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = os.path.join('/static/upload/house', f1.filename) house.add_update() # 返回图片信息 return jsonify(code='200', url=os.path.join('/static/upload/house', f1.filename))
def newhouse_images(): images = request.files.get('house_image') house_id = request.form.get('house_id') # 保存成功 url = os.path.join(UPLOAD_DIRS, images.filename) image_url = os.path.join(os.path.join('/static', 'upload'), images.filename) images.save(url) house_image = HouseImage() house_image.house_id = house_id house_image.url = image_url try: house_image.add_update() except: return jsonify(status_code.DATABASE_ERROR) house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = image_url try: house.add_update() except: return jsonify(status_code.DATABASE_ERROR) return jsonify(code=status_code.OK, image_url=image_url)
def newhouse(): if request.method == 'GET': return render_template('newhouse.html') if request.method == 'POST': user = current_user user_id = user.id area_id = int(request.form.get('area_id')) title = request.form.get('title') price = int(request.form.get('price')) address = request.form.get('address') room_count = int(request.form.get('room_count')) acreage = int(request.form.get('acreage')) unit = request.form.get('unit') capacity = int(request.form.get('capacity')) beds = request.form.get('beds') deposit = int(request.form.get('deposit')) min_days = int(request.form.get('min_days')) max_days = int(request.form.get('max_days')) facilities = request.form.getlist('facility') house = House() house.user_id = user_id house.area_id = area_id house.title = title house.price = price house.address = address house.room_count = room_count house.acreage = acreage house.unit = unit house.capacity = capacity house.beds = beds house.deposit = deposit house.min_days = min_days house.max_days = max_days for facility in facilities: h_facility = Facility.query.filter_by(id=int(facility)).first() house.facilities.append(h_facility) house.add_update() house_id = house.id return jsonify({"code": OK, "house_id": house_id}) if request.method == 'PATCH': house_image = request.files.get('house_image') house_id = int(request.form.get('house_id')) if is_graph(house_image.filename): file_path = os.path.join(UPLOAD_DIR, house_image.filename) house_image.save(file_path) # 保存图片路径到数据库 house = House.query.get(house_id) if house.index_image_url == 'upload\haveno.jpg': house.index_image_url = os.path.join('upload', house_image.filename) house.add_update() else: houseimage = HouseImage() houseimage.house_id = house_id houseimage.url = os.path.join('upload', house_image.filename) houseimage.add_update() return jsonify({"code": OK, "house_image": os.path.join('upload', house_image.filename)}) else: return jsonify({"code": NO})
def newhouse_image(): house_id = request.form.get('house_id') f = request.files.get('house_image') url = os.path.join(MEDIA_PATH, f.filename) f.save(url) image = HouseImage() image.house_id = house_id image_url = os.path.join(f.filename) image.url = image_url image.add_update() house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = os.path.join('/static/media/', f.filename) house.add_update() return jsonify(code='200', url=image_url)
def house_image(): image = request.files.get('house_image') path = os.path.join(MEDIA_PATH, image.filename) image.save(path) id = request.form.get('house_id') house = House.query.filter(House.id == id).first() index_image = house.index_image_url if not index_image: house.index_image_url = image.filename house.add_update() else: h_image = HouseImage() h_image.house_id = id h_image.url = image.filename h_image.add_update() return jsonify(status_code.SUCCESS)
def house_image_post(): image = request.files.get('house_image') house_id = request.form.get('house_id') file_path = os.path.join(UPLOAD_DIR, image.filename) image.save(file_path) # 保存到数据库中 house_image = HouseImage() house_image.house_id = house_id image_url = os.path.join('upload', image.filename) house_image.url = image_url house_image.add_update() # 创建房屋首图 house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = image_url house.add_update() return jsonify({'code': '200', 'image_url': image_url})
def add_house_image(): if request.method == 'POST': house_image = request.files.get('house_image') house_image_path = os.path.join(MEDIA_PATH, house_image.filename) house_image.save(house_image_path) house_id = session.get('house_id') house = House.query.get(house_id) image_url = '/static/media/' + house_image.filename # 主图存进house表 if not house.index_image_url: house.index_image_url = image_url house.add_update() # 其他图存进house_image表 houseimage = HouseImage() houseimage.house_id = house_id houseimage.url = image_url houseimage.add_update() return jsonify({'code': 200, 'msg': '图片保存成功'})
def house_image(): # 获取房屋的id和上传的图片 house_id = request.form.get('house_id') house_image = request.files.get('house_image') # 保存图片 save_path = os.path.join(UPLOAD_DIR, house_image.filename) house_image.save(save_path) # 保存在house_images表,house_id, url house_img = HouseImage() house_img.house_id = house_id img_url = os.path.join('upload', house_image.filename) house_img.url = img_url house_img.add_update() # 房屋首图的设置 house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = img_url house.add_update() return jsonify(code=status_code.OK, img_url=img_url)
def newhouse_image(): image_url = request.files.get('house_image') id = request.form.get('house_id') BASE_DIR = (os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) STATIC_DIR = os.path.join(BASE_DIR, 'static') MEDIA_DIR = os.path.join(STATIC_DIR, 'media') filename = str(uuid.uuid4()) a = image_url.mimetype.split('/')[-1:][0] name = filename + '.' + a path = os.path.join(MEDIA_DIR, name) image_url.save(path) house = House.query.filter_by(id=id).first() if not house.index_image_url: house.index_image_url = name house.add_update() image = HouseImage() image.house_id = id image.url = name image.add_update() return jsonify({'code': 200, 'msg': '请求成功'})
def new_house_img(): # 获得图片 img = request.files.get('house_image') house_id = request.form.get('house_id') # 获得房屋 house = House.query.get(house_id) # 保存图片到文件夹 img.save(os.path.join(UPLOAD_DIR, img.filename)) # 保存房屋图片信息 image_url = os.path.join('upload', img.filename) # 保存房屋的首图 if not house.index_image_url: house.index_image_url = image_url house.add_update() # 创建一个中间表对象 h_image = HouseImage() h_image.house_id = house_id h_image.url = image_url h_image.add_update() return jsonify(code=status_code.OK, image_url=image_url)
def house_images(): # 创建房屋图片 house_id = request.form.get('house_id') image = request.files.get('house_image') # 保存图片 /static/media/upload/xxx.jpg save_url = os.path.join(upload_folder, image.filename) image.save(save_url) # 保存房屋和图片信息 house_image = HouseImage() house_image.house_id = house_id image_url = os.path.join('upload', image.filename) house_image.url = image_url house_image.add_update() # 创建房屋首图 house = House.query.get(house_id) if not house.index_image_url: house.index_image_url = image_url house.add_update() return jsonify(code=status_code.OK, image_url=image_url)
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': '请求成功'})
def house_image(): # 获取项目根路径 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取媒体文件路径 MEDIA_DIR = os.path.join(BASE_DIR, '/static/imgs/') # 接收图片 img = request.files['house_image'] # 获取图片名称 filename = img.filename # 保存图片 img.save('./static/imgs/%s' % filename) # 获取当前房屋对象 house = HouseImage() house_id = request.form.get('house_id') house.house_id = house_id house.url = filename house.add_update() # 设置首图 first_house = House.query.get(house_id) first_house.index_image_url = filename first_house.add_update() return jsonify({'code': 200, 'data': filename})
def img(): house_id = request.form.get('house_id') img = request.files.get('house_image') file_path = os.path.join(UPLOAD_DIR, img.filename) img.save(file_path) house = House.query.filter(House.id == house_id).first() house_img = HouseImage() house_img.url = os.path.join('upload', img.filename) house_img.house_id = house_id house_img.add_update() if house.index_image_url: imges = HouseImage.query.filter(HouseImage.house_id == house_id).all() imgs = [] for item in imges: imgs.append(item.url) return jsonify(code=status_code.ok, imgs=imgs) else: house.index_image_url = os.path.join('upload', img.filename) house.add_update() imgs = [] imgs.append(house.index_image_url) return jsonify(code=status_code.ok, imgs=imgs)