def pub_house(): """发布新的房源 0.判断用户是否登录 1.接受参数:基本信息和设备信息 2.判断参数是否为空,并对某些参数进行合法性的校验,比如金钱相关的 3.创建房屋模型对象,并赋值 4.保存房屋数据到数据库 5.响应发布新的房源的结果 """ # 1.接受参数:基本信息和设备信息 json_dict = request.json title = json_dict.get('title') price = json_dict.get('price') address = json_dict.get('address') area_id = json_dict.get('area_id') room_count = json_dict.get('room_count') acreage = json_dict.get('acreage') unit = json_dict.get('unit') capacity = json_dict.get('capacity') beds = json_dict.get('beds') deposit = json_dict.get('deposit') min_days = json_dict.get('min_days') max_days = json_dict.get('max_days') facility = json_dict.get('facility') # [2,4,6,8,10] # 2.判断参数是否为空,并对某些参数进行合法性的校验,比如金钱相关的 if not all([title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days, facility]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') # 校验价格和押金是否合法,不允许传入数字以外的数据 # 10元 * 100 == 1000 分 # 10.1元 * 100 == 1010分 try: price = int(float(price)*100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='金额格式错误') # 3.创建房屋模型对象,并赋值 house = House() house.user_id = g.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 # 给facilities属性赋值,实现多对多的关联关系 facility == [2,4,6,8,10] facilities = Facility.query.filter(Facility.id.in_(facility)).all() house.facilities = facilities # 4.保存房屋数据到数据库 try: db.session.add(house) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存房屋数据失败') # 5.响应发布新的房源的结果 return jsonify(errno=RET.OK, errmsg='发布新房源成功', data={'house_id': house.id})
def pub_house(): """ 是否登录 1.接受参数,校验 2.添加书库到数据库 3.响应 :return: """ # 1,获取参数并校验 json_dict = request.json title = json_dict.get('title') price = json_dict.get('price') address = json_dict.get('address') area_id = json_dict.get('area_id') room_count = json_dict.get('room_count') acreage = json_dict.get('acreage') unit = json_dict.get('unit') capacity = json_dict.get('capacity') beds = json_dict.get('beds') deposit = json_dict.get('deposit') min_days = json_dict.get('min_days') max_days = json_dict.get('max_days') facility = json_dict.get('facility') # 一个列表[1,2] # 校验参数合法性 if not all([title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days, facility]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') # 对于价格和押金是否合法,只可以传入数字 # 数据库价格和押金字段限制的是整数,实际上价格押金也有带小数的,所以要把他乘以100变成整数,要取出来渲染的时候再除以100,关于金钱小数之类的都可以这样处理 try: price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(reeno=RET.PARAMERR, errmsg='金额格式错误') # 创建house对象,添加数据到表 house = House() house.user_id = g.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 # 给对象添加属性,模型类中relationship 只是添加关联方便查询,不会在表中创建字段 facilities = Facility.query.filter(Facility.id.in_(facility)).all() house.facilities = facilities # 更新数据库信息 try: db.session.add(house) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(reeno=RET.DBERR, errmsg='添加数据失败') # 响应成功,带上房子的id,便于后面对于房屋的显示 return jsonify(reeno=RET.OK, errmsg='发布成功', data={'house_id': house.id})
def save_new_house(): req_dict = request.json title = req_dict.get('title') price = req_dict.get('price') address = req_dict.get('address') area_id = req_dict.get('area_id') room_count = req_dict.get('room_count') acreage = req_dict.get('acreage') unit = req_dict.get('unit') capacity = req_dict.get('capacity') beds = req_dict.get('beds') deposit = req_dict.get('deposit') min_days = req_dict.get('min_days') max_days = req_dict.get('max_days') if not all([ title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') try: price = float(price) * 100 deposit = float(deposit) * 100 except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='参数错误') house = House() house.user_id = g.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 facility = req_dict.get('facility') try: facilities = Facility.query.filter(Facility.id.in_(facility)).all() if facilities: house.facilities = facilities except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询房屋设施信息失败') try: db.session.add(house) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='保存房屋信息失败') return jsonify(errno=RET.OK, errmsg='ok', data={'house_id': house.id})
def save_new_house(): """ 1.获取上传的参数 2.判断是否为空,校验参数 3.初始化一个 House 的对象 4.进行数据保存 5.返回结果 :return: 前端发送过来的json数据 { "title":"", "price":"", "area_id":"1", "address":"", "room_count":"", "acreage":"", "unit":"", "capacity":"", "beds":"3", "deposit":"", "min_days":"", "max_days":"", "facility":["7","8"] }""" #1. 获取上传的参数 user_id = g.user_id json_dict = request.json title = json_dict.get('title') price = json_dict.get('price') area_id = json_dict.get('area_id') address = json_dict.get('address') room_count = json_dict.get('room_count') acreage = json_dict.get('acreage') unit = json_dict.get('unit') capacity = json_dict.get('capacity') beds = json_dict.get('beds') deposit = json_dict.get('deposit') min_days = json_dict.get('min_days') max_days = json_dict.get('max_days') # 2.判断是否为空.校验参数 if not all([ title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') try: price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') # 3.初始化 house 对象 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 # 设置房屋设施 facilities = json_dict.get('facility') if facilities: # 查询指定列表中对应的模型并设置设施信息 house.facilities = Facility.query.filter( Facility.id.in_(facilities)).all() # 4.进行数据保存 try: db.session.add(house) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='添加数据失败') return jsonify(errno=RET.OK, errmsg='ok', data={'house_id': house.id})
def pub_house(): """发布新房源 0.判断用户是否登录 @login_required 1.接受所有参数,并判断是否缺少 2.校验参数:price / deposit, 需要用户传入数字 3.实例化房屋模型对象,并给属性赋值 4.保存到数据库 5.响应结果 """ # 1.接受所有参数,并判断是否缺少 json_dict = request.json title = json_dict.get('title') price = json_dict.get('price') address = json_dict.get('address') area_id = json_dict.get('area_id') room_count = json_dict.get('room_count') acreage = json_dict.get('acreage') unit = json_dict.get('unit') capacity = json_dict.get('capacity') beds = json_dict.get('beds') deposit = json_dict.get('deposit') min_days = json_dict.get('min_days') max_days = json_dict.get('max_days') if not all([ title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') # 2.校验参数:price / deposit, 需要用户传入数字 # 提示:在开发中,对于像价格这样的浮点数,不要直接保存浮点数,因为有精度的问题,一般以分为单位 try: price = int(float(price) * 100) # 0.1元 ==> 10分 deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='参数格式错误') # 3.实例化房屋模型对象,并给属性赋值 house = House() house.user_id = g.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 # 处理房屋的设施 facilities = [2,4,6] facilities = json_dict.get('facility') # 查询出被选中的设施模型对象 house.facilities = Facility.query.filter(Facility.id.in_(facilities)).all() # 4.保存到数据库 try: db.session.add(house) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='发布新房源失败') # 5.响应结果 return jsonify(errno=RET.OK, errmsg='发布新房源成功', data={'house_id': house.id})
def save_new_house(): """ 发布新房源: 1. 接收房屋的基本信息并进行参数校验 2. 创建House对象并保存房屋基本信息 3. 将房屋的基本信息添加进数据库 4. 返回应答 """ # 1. 接收房屋的基本信息并进行参数校验 req_dict = request.json title = req_dict.get('title') price = req_dict.get('price') # 房屋价格 address = req_dict.get('address') area_id = req_dict.get('area_id') room_count = req_dict.get('room_count') acreage = req_dict.get('acreage') unit = req_dict.get('unit') capacity = req_dict.get('capacity') beds = req_dict.get('beds') deposit = req_dict.get('deposit') # 房屋押金 min_days = req_dict.get('min_days') max_days = req_dict.get('max_days') if not all([ title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') try: # 数据库中房屋的价格和押金以 分 保存 price = float(price) * 100 deposit = float(deposit) * 100 except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='参数错误') # 2. 创建House对象并保存房屋基本信息 house = House() house.user_id = g.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 # 获取房屋的设施信息 facility = req_dict.get('facility') # [1, 3, 4] try: facilities = Facility.query.filter(Facility.id.in_(facility)).all() if facilities: house.facilities = facilities except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='获取房屋设施信息失败') # 3. 将房屋的基本信息添加进数据库 try: db.session.add(house) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='保存房屋信息失败') # 4. 返回应答 return jsonify(errno=RET.OK, errmsg='OK', data={'house_id': house.id})
def save_new_house(): """ 1. 接收参数并且判空 2. 将参数的数据保存到新创建house模型 3. 保存house模型到数据库 前端发送过来的json数据 { "title":"", "price":"", "area_id":"1", "address":"", "room_count":"", "acreage":"", "unit":"", "capacity":"", "beds":"", "deposit":"", "min_days":"", "max_days":"", "facility":["7","8"] } :return: """ # 1. 取到参数 user_id = g.user_id json_dict = request.json title = json_dict.get('title') price = json_dict.get('price') address = json_dict.get('address') area_id = json_dict.get('area_id') room_count = json_dict.get('room_count') acreage = json_dict.get('acreage') unit = json_dict.get('unit') capacity = json_dict.get('capacity') beds = json_dict.get('beds') deposit = json_dict.get('deposit') min_days = json_dict.get('min_days') max_days = json_dict.get('max_days') # 1.1 判断是否都有值 if not all( [title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days]): return jsonify(errno=RET.PARAMERR, errmsg="参数有误") # 1.2 校验参数格式是否正确 try: price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数有误") 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 # 获取到当前房屋的设施列表数组 facilities = json_dict.get("facility") if facilities: house.facilities = Facility.query.filter(Facility.id.in_(facilities)).all() # 3.保存house模型到数据库 try: db.session.add(house) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据保存错误") return jsonify(errno=RET.OK, errmsg="ok", data={"house_id": house.id})
def create_house(): user_id = g.user_id title = request.form.get("title") # 房屋名称标题 price = request.form.get("price") # 房屋单价 area_id = request.form.get("area_id") # 房屋所属城区的编号 address = request.form.get("address") # 房屋地址 room_count = request.form.get("room_count") # 房屋包含的房间数目 acreage = request.form.get("acreage") # 房屋面积 unit = request.form.get("unit") # 房屋布局(几室几厅) capacity = request.form.get("capacity") # 房屋容纳人数 beds = request.form.get("beds") # 房屋卧床数目 deposit = request.form.get("deposit") # 押金 min_days = request.form.get("min_days") # 最小入住天数 max_days = request.form.get("max_days") # 最大入住天数 if not all([ title, price, area_id, address, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) try: price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="金额必须是数字") try: area = Area.query.get(area_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="地区错误") if area is None: return jsonify(errno=RET.PARAMERR, errmsg="地区错误") # 保存房屋信息 house = House(user_id=user_id, area_id=area_id, title=title, price=price, address=address, room_count=room_count, acreage=acreage, unit=unit, capacity=capacity, beds=beds, deposit=deposit, min_days=min_days, max_days=max_days) # 处理房屋的设施信息 try: facility_str = request.form.get("facility") facility_ids = facility_str.split(",") except Exception as e: current_app.logger.error(e) if facility_ids: try: facilities = Facility.query.filter( Facility.id.in_(facility_ids)).all() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存设施信息失败") if facilities: house.facilities = facilities try: db.session.add(house) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存房屋信息失败") return jsonify(errno=RET.OK, errmsg="保存成功", data={"house_id": house.id})
def pub_house(): """发布新的房源""" # 1,接受参数 json_dict = request.json title = json_dict.get('title') price = json_dict.get('price') address = json_dict.get('address') area_id = json_dict.get('area_id') room_count = json_dict.get('room_count') acreage = json_dict.get('acreage') unit = json_dict.get('unit') capacity = json_dict.get('capacity') beds = json_dict.get('beds') deposit = json_dict.get('deposit') min_days = json_dict.get('min_days') max_days = json_dict.get('max_days') facility = json_dict.get('facility') # 2,判断参数是否为空 if not all([title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days, facility]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') try: # 金额单位转成‘分’ price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='金额格式错误') # 3,创建房屋模型对象 house = House() house.user_id = g.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 facilities = Facility.query.filter(Facility.id.in_(facility)).all() house.facilities = facilities # 4,保存房源到数据库 try: db.session.add(house) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存房屋数据失败') # 5,响应发布的新的房源信息 return jsonify(errno=RET.OK, errmsg='发布房源成功', data={'house_id': house.id})
def save_house_info(): """保存房屋的基本信息""" user_id = g.user_id house_data = request.get_json() title = house_data.get("title") # 房屋标题 price = house_data.get("price") # 房屋单价 area_id = house_data.get("area_id") # 房屋所属城区的编号 address = house_data.get("address") # 房屋地址 room_count = house_data.get("room_count") # 房屋包含的房间数目 acreage = house_data.get("acreage") # 房屋面积 unit = house_data.get("unit") # 房屋布局(几室几厅) capacity = house_data.get("capacity") # 房屋容纳人数 beds = house_data.get("beds") # 房屋卧床数目 deposit = house_data.get("deposit") # 押金 min_days = house_data.get("min_days") # 最小入住天数 max_days = house_data.get("max_days") # 最大入住天数 # 校验参数 if not all([ title, price, area_id, address, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 判断金额是否正确 try: price = int(float(price) * 100) # 数据库中是以分为单位计算 deposit = int(float(deposit) * 100) except Exception as ex: current_app.logger.error(ex) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 判断城区id是否存在 try: area = Area.query.get(area_id) except Exception as ex: current_app.logger.error(ex) return jsonify(errno=RET.DBERR, errmsg="数据异常") if area is None: return jsonify(errno=RET.NODATA, errmsg="城区信息有误") # 保存房屋信息 house = House(user_id=user_id, area_id=area_id, title=title, price=price, address=address, room_count=room_count, acreage=acreage, unit=unit, capacity=capacity, beds=beds, deposit=deposit, min_days=min_days, max_days=max_days) # try: # db.session.add(house) # except Exception as ex: # current_app.logger.error(ex) # return jsonify(errno=RET.DBERR, errmsg="保存数据异常") # 处理房屋的设施信息 facilities_ids = house_data.get("facility") # 如果用户勾选了设施信息,再保存数据库 if facilities_ids: # ["7","8"] try: facilities = Facility.query.filter( Facility.id.in_(facilities_ids)).all() except Exception as ex: current_app.logger.error(ex) return jsonify(errno=RET.DBERR, errmsg="数据库异常") if facilities: # 表示有合法的数据 # 保存设施数据 house.facilities = facilities # 解决一对多 try: db.session.add(house) db.session.commit() except Exception as ex: current_app.logger.error(ex) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存数据失败") # 保存数据成功 return jsonify(errno=RET.OK, errmsg="OK", data={"house_id": house.id})
def pub_house(): """"发布房源信息 0、判断是否有登录 1、获取请求参数 2、判断参数合法性 3、创建新模型类,并赋值 4、提交数据库 5、返回结果 """ json_dict = request.json title = json_dict.get('title') price = json_dict.get('price') address = json_dict.get('address') area_id = json_dict.get('area_id') room_count = json_dict.get('room_count') acreage = json_dict.get('acreage') unit = json_dict.get('unit') capacity = json_dict.get('capacity') beds = json_dict.get('beds') deposit = json_dict.get('deposit') min_days = json_dict.get('min_days') max_days = json_dict.get('max_days') facility = json_dict.get('facility') # [2,4,6,8,10] if not all([ title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days, facility ]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') try: #检验价格 price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='金额格式错误') # 3.创建房屋模型对象,并赋值 house = House() house.user_id = g.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 # 给facilities属性赋值,实现多对多的关联关系 facility == [2,4,6,8,10] facilities = json_dict.get('facility') house.facilities = Facility.query.filter(Facility.id.in_(facilities)).all() #这一步很关键,要把发布的房屋数据保存到mysql,不然后面的操作都会无法展开 try: db.session.add(house) db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='保存新房屋数据失败') return jsonify(errno=RET.OK, errmsg='发布新房源成功', data={'house_id': house.id})
def add_house(): """ 添加新房屋 1. 接收所有参数 2. 判断参数是否有值&参数是否符合规范 3. 初始化房屋模型,并设置数据 4. 保存到数据库 5. 返回响应 :return: """ # 1. 接收所有参数 data_dict = request.json title = data_dict.get('title') price = data_dict.get('price') address = data_dict.get('address') area_id = data_dict.get('area_id') room_count = data_dict.get('room_count') acreage = data_dict.get('acreage') unit = data_dict.get('unit') capacity = data_dict.get('capacity') beds = data_dict.get('beds') deposit = data_dict.get('deposit') min_days = data_dict.get('min_days') max_days = data_dict.get('max_days') # 2. 判断参数是否有值&参数是否符合规范 if not all([ title, price, address, area_id, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: # 以分的形式进行保存 price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 3. 初始化房屋模型,并设置数据 house = House() house.user_id = g.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 # 取到当前房屋的设置列表 facilities = data_dict.get("facility") # [1, 3, 4, 6] # 当前房屋对应的所有设置 house.facilities = Facility.query.filter(Facility.id.in_(facilities)).all() try: db.session.add(house) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="添加数据失败") return jsonify(errno=RET.OK, errmsg="OK", data={"house_id": house.id})
def pub_house(): """发布房源""" user_id = g.user_id request_json_dict = request.json title = request_json_dict.get('title') price = request_json_dict.get('price') area_id = request_json_dict.get('area_id') address = request_json_dict.get('address') room_count = request_json_dict.get('room_count') acreage = request_json_dict.get('acreage') unit = request_json_dict.get('unit') capacity = request_json_dict.get('capacity') beds = request_json_dict.get('beds') deposit = request_json_dict.get('deposit') min_days = request_json_dict.get('min_days') max_days = request_json_dict.get('max_days') facility = request_json_dict.get('facility') if not all([ title, price, area_id, address, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days, facility ]): return jsonify(errno=RET.PARAMERR, errmsg=u'参数不完整') # 查询facility对象 try: facility_items = Facility.query.filter(Facility.id.in_(facility)).all() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=u'查询配置失败') # 记录数据库 new_house = House() new_house.user_id = user_id new_house.title = title new_house.price = float(price) * 100 new_house.area_id = area_id new_house.address = address new_house.room_count = room_count new_house.acreage = acreage new_house.unit = unit new_house.capacity = capacity new_house.beds = beds new_house.deposit = float(deposit) * 100 new_house.min_days = min_days new_house.facilities = facility_items try: db.session.add(new_house) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=u'新房屋保存数据库失败') return jsonify(errno=RET.OK, errmsg='ok', data={'house_id': new_house.id})
def my_new_house(): house_dict = request.form # 基本信息 title = house_dict.get('title') price = house_dict.get('price') area_id = house_dict.get('area_id') address = house_dict.get('address') # 详细信息 room_count = house_dict.get('room_count') acreage = house_dict.get('acreage') unit = house_dict.get('unit') capacity = house_dict.get('capacity') beds = house_dict.get('beds') deposit = house_dict.get('deposit') min_days = house_dict.get('min_days') max_days = house_dict.get('max_days') # 配套设施 facilitys = house_dict.getlist('facility') if not all([ title, price, area_id, address, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return jsonify(status_code.MYHOUSE_INFO_IS_NOT_FULL) user = User.query.get(session['user_id']) house = House() house.user_id = user.id house.title = title house.price = price house.area_id = area_id 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 = max_days house.max_days = max_days try: house.add_update() hou = House.query.get(house.id) for facility in facilitys: fac = Facility.query.get(facility) hou.facilities.append(fac) db.session.add(hou) db.session.commit() return jsonify(status_code.SUCCESS) except Exception as e: return jsonify(status_code.DATABASE_ERROR)
def user_newhouse(): house_dict = request.form.to_dict() facility_ids = request.form.getlist('facility') house = House() house.user_id = session['user_id'] # 基本信息 house.title = house_dict.get('title') house.price = house_dict.get('price') house.area_id = house_dict.get('area_id') house.address = house_dict.get('address') # 详细信息 house.room_count = house_dict.get('room_count') house.acreage = house_dict.get('acreage') house.unit = house_dict.get('unit') house.capacity = house_dict.get('capacity') house.beds = house_dict.get('beds') house.deposit = house_dict.get('deposit') house.min_days = house_dict.get('min_days') house.max_days = house_dict.get('max_days') if facility_ids: facilitys = Facility.query.filter(Facility.id.in_(facility_ids)).all() house.facilities = facilitys try: house.add_update() return jsonify(code=status_code.OK, house_id=house.id) except Exception as e: return jsonify(status_code.DATABASE_ERROR)