def create_order(): house_id = request.form.get('house_id') # 对前端传过来的时间值进行处理成可以做加法运算的 begin_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d') end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d') user_id = session['user_id'] # 验证是否有填写时间 if not all([begin_date, end_date]): return jsonify(status_code.ORDER_BEGIN_END_DATA_NOT_NULL) # 验证初始时间是否大于结束时间 if not begin_date < end_date: return jsonify(status_code.ORDER_BEGIN_DATA_GT_END_DATA_ERROE) # 获取房屋信息 try: house = House.query.get(house_id) except: return jsonify(status_code.DATABASE_ERROR) # 创建订单对象 order = Order() order.house_id = house_id order.end_date = end_date order.begin_date = begin_date order.user_id = user_id order.days = (end_date - begin_date).days + 1 order.house_price = house.price order.amount = order.days * order.house_price order.add_update() return jsonify(status_code.SUCCESS)
def post_order(): order_dict = request.form house_id = order_dict.get('house_id') start_time = datetime.strptime(order_dict.get('start_time'), '%Y-%m-%d') end_time = datetime.strptime(order_dict.get('end_time'), '%Y-%m-%d') if not all([house_id, start_time, end_time]): return jsonify(status_code.PARAMS_ERROR) if start_time > end_time: return jsonify(status_code.ORDER_START_TIME_GT_END_TIME) house = House.query.get(house_id) order = Order() order.user_id = session['user_id'] order.house_id = house_id order.begin_date = start_time order.end_date = end_time order.house_price = house.price order.days = (end_time - start_time).days + 1 order.amount = order.days * order.house_price try: order.add_update() return jsonify(code=status_code.OK) except Exception as e: return jsonify(status_code.DATABASE_ERROR)
def get_order(id): """ 提交我的订单 """ user_id = session['user_id'] begin_date = datetime.datetime.strptime(request.form.get('start_date'), '%Y-%m-%d') end_date = datetime.datetime.strptime(request.form.get('end_date'), '%Y-%m-%d') times = end_date - begin_date days = times.days + 1 house = House.query.get(id) price = house.price amount = days * price order = Order() order.user_id = user_id order.house_id = id order.begin_date = begin_date order.end_date = end_date order.days = days order.house_price = price order.amount = amount order.add_update() return jsonify(code=status_code.OK)
def create_order(): begin_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d') end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d') house_id = request.form.get('house_id') user_id = session['user_id'] if not all([begin_date, end_date]): return jsonify(status_code.ORDER_BEGIN_END_DATA_NOT_NULL) if begin_date > end_date: return jsonify(status_code.ORDER_BEGIN_DATA_GT_END_DATE_ERROR) house = House.query.get(house_id) order = Order() order.user_id = user_id order.house_id = house_id order.begin_date = begin_date order.end_date = end_date order.days = (end_date - begin_date).days + 1 order.house_price = house.price order.amount = order.days * order.house_price order.add_update() return jsonify(status_code.SUCCESS)
def get_order(): house_id = request.form.get('house_id') begin_date = request.form.get('start_date') end_date = request.form.get('end_date') if not all([house_id, begin_date, end_date]): return jsonify(status_code.PARAMS_ERROR) x = (datetime.strptime(end_date, '%Y-%m-%d') - datetime.strptime(begin_date, '%Y-%m-%d')).days # 如果开始时间大于结束时间. 进行判断 返回参数错误 if x < 0: return jsonify(status_code.END_TIME_IS_ERROR) days = x + 1 house_price = House.query.filter_by(id=house_id).first().price amout = days * house_price # 创建订单 order = Order() order.user_id = session['user_id'] order.house_id = house_id order.begin_date = begin_date order.end_date = end_date order.days = days order.house_price = house_price order.amout = amout try: order.add_update() return jsonify(status_code.SUCCESS) except: return jsonify(status_code.DATABASE_ERROR)
def order(): # 接收参数 dict = request.form house_id = int(dict.get('house_id')) start_date = datetime.strptime(dict.get('start_date'), '%Y-%m-%d') end_date = datetime.strptime(dict.get('end_date'), '%Y-%m-%d') # 验证有效性 if not all([house_id, start_date, end_date]): return jsonify(status_code.PARAMS_ERROR) if start_date > end_date: return jsonify(status_code.ORDER_START_END_TIME_ERROR) # 查询房屋对象 try: house = House.query.get(house_id) except: return jsonify(status_code.DATABASE_ERROR) # 创建订单对象 order = Order() order.user_id = session['user_id'] order.house_id = house_id order.begin_date = start_date order.end_date = end_date order.days = (end_date - start_date).days + 1 order.house_price = house.price order.amount = order.days * order.house_price try: order.add_update() except: return jsonify(status_code.DATABASE_ERROR) # 返回信息 return jsonify(code=status_code.OK)
def create_order(): if request.method == "POST": house_id = request.form.get('house_id') # 对前端传过来的时间值进行处理成可以做加法运算的 print(request.form.get('begin_date')) begin_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d') end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d') print(begin_date, end_date) user_id = session['user_id'] # 验证是否有填写时间 if not all([begin_date, end_date]): return jsonify(status_code.ORDER_BEGIN_END_DATA_NOT_NULL) # 验证初始时间是否大于结束时间 if begin_date > end_date: return jsonify(status_code.ORDER_BEGIN_DATA_GT_END_DATA_ERROE) # 获取房屋信息 try: house = House.query.get(house_id) except: return jsonify(code=status_code.DATABASE_ERROR) # 创建订单对象 order = Order() order.house_id = house_id order.end_date = end_date order.begin_date = begin_date order.user_id = user_id order.days = (end_date - begin_date).days + 1 order.house_price = house.price order.amount = order.days * order.house_price order.add_update() data = {"order_id": order.id, "order_amount": order.amount} print(data) return jsonify(code=status_code.ok, data=data) else: id = request.args.get('id') amount = request.args.get('amount') print(id, amount) return render_template('order_success.html', data={ 'id': id, 'amount': amount })
def bookings_post(id): start_date = request.form.get('start_date') end_date = request.form.get('end_date') live_time = datetime.strptime(end_date, '%Y-%m-%d') - datetime.strptime( start_date, '%Y-%m-%d') if live_time.days < 0: return jsonify(status_code.ORDER_DATE_IS_INVALID) house = House.query.filter_by(id=id).first() order = Order() order.user_id = session['user_id'] order.house_id = id order.begin_date = start_date order.end_date = end_date order.days = live_time.days order.house_price = house.price order.amount = live_time.days * house.price try: order.add_update() except Exception as e: db.session.rollback() return jsonify(status_code.DATABASE_ERROR) return jsonify({'code': '200'})