def create_order(): #获取参数 dict = request.form house_id = int(dict.get('hosue_id')) start_date = datetime.strptime(dict.get('begin_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) #获取房屋对象 house = House.query.get(house_id) #创建订单对象 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.house_price * order.days try: order.add_update() except: return jsonify(status_code.DATABASE_ERROR) return jsonify(code=200, msg='请求成功')
def booking_post(): house_id = request.form.get('house_id') house = House.query.get(house_id) if session['user_id'] == house.user_id: return jsonify(status_code.ORDER_HOUSE_USER_ID_IS_SESSION_ID) start_date = request.form.get('start_date') start_time = datetime.datetime.strptime(start_date, '%Y-%m-%d') end_date = request.form.get('end_date') end_time = datetime.datetime.strptime(end_date, '%Y-%m-%d') days = (end_time-start_time).days + 1 price = House.query.get(house_id).price amount = days * price order = Order() order.user_id = session['user_id'] order.house_id = request.form.get('house_id') order.begin_date = start_time order.end_date = end_time order.days = days order.house_price = price order.amount = amount order.add_update() return jsonify(code=status_code.OK)
def 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: return jsonify(status_code.DATABASE_ERROR)
def create_order(): try: user_id = session['user_id'] except: user_id = None if not user_id: return jsonify(status_code.LOGIN_STATUS_NOT_LOGIN) house_id = request.form.get('house_id') house = House.query.filter(House.id == house_id).first() price = house.price max_days = house.max_days sd = request.form.get('sd') ed = request.form.get('ed') sd = datetime.datetime.strptime(sd, '%Y-%m-%d') ed = datetime.datetime.strptime(ed, '%Y-%m-%d') if ed <= sd: return jsonify(status_code.BOOKING_STATUS_DATE_ERROR) days = (ed - sd).days if max_days != 0 and days > max_days: return jsonify(status_code.BOOKING_STATUS_MAX_DAYS_ERROR) amount = price * days order = Order() order.house_id = house_id order.user_id = user_id order.begin_date = sd order.end_date = ed order.days = days order.house_price = price order.amount = amount order.status = 'WAIT_ACCEPT' order.add_update() return jsonify(status_code.SUCCESS)
def booking(): if request.method == 'GET': return render_template('booking.html') if request.method == 'POST': house_id = int(request.form.get('house_id')) house = House.query.filter(House.id == house_id).first() days = request.form.get('days') max_days = house.max_days begin_date = request.form.get('begin_date') end_date = request.form.get('end_date') begin_date = datetime.strptime(begin_date, '%Y-%m-%d') end_date = datetime.strptime(end_date, '%Y-%m-%d') amount = request.form.get('amount') user_id = session.get('user_id') order = Order() if house.max_days == 0 or days <= max_days: order.days = days order.house_id = house_id order.user_id = user_id order.begin_date = begin_date order.end_date = end_date order.house_price = house.price order.amount = amount order.add_update() return jsonify({'code': 200, 'msg': '请求成功'}) return jsonify({'code': 10000, 'msg': '请求成功'})
def check_in(): uid = session.get('user_id') hid = request.form.get('hid') begin = request.form.get('begin') end = request.form.get('end') days = request.form.get('days') price = request.form.get('price') amount = request.form.get('amount') if not all([hid, begin, end, days, price, amount]): return jsonify(status_code.ORDER_PARAMS_NOT_COMPLETE) if begin > end: return jsonify(status_code.ORDER_TIME_PARAMS_ERROR) try: order = Order() order.user_id = uid order.house_id = hid order.begin_date = begin order.end_date = end order.days = days order.house_price = price order.amount = amount order.add_update() return jsonify(status_code.SUCCESS) except BaseException as e: print(e) db.session.rollback() return jsonify(status_code.DATABASE_ERROR)
def order(): # 创建订单模型 # 1.获取开始和结束时间 begin_date = request.form.get('begin_date') end_date = request.form.get('end_date') begin_date = datetime.strptime(begin_date, '%Y-%m-%d') end_date = datetime.strptime(end_date, '%Y-%m-%d') # 2.获取当前用户和房屋id user_id = session['user_id'] house_id = request.form.get('house_id') 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 * house.price order.add_update() return jsonify(code=200)
def order(): # 接收参数 house_id = int(request.form.get('house_id')) start_date = datetime.strptime(request.form.get('start_date'), '%Y-%m-%d') end_date = datetime.strptime(request.form.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(): 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 my_orders(): if session: start = request.form.get('start') end = request.form.get('end') # span = request.form.get('span') house_id = request.form.get('house_id') starttime = datetime.strptime(start, '%Y-%m-%d') endtime = datetime.strptime(end, '%Y-%m-%d') house = House.query.get(house_id) order = Order() order.user_id = session['user_id'] order.house_id = house_id order.begin_date = starttime order.end_date = endtime order.days = (endtime - starttime).days + 1 order.house_price = house.price order.amount = order.days * order.house_price order.add_update() return jsonify({'code':200, 'msg':'ok'}) else: return jsonify({ 'code':1122, 'msg':'未登录' })
def add_orders(): """ 添加订单 :return: """ s_date = request.form.get('start_date') e_date = request.form.get('end_date') h_id = request.form.get('house_id') days = request.form.get('days') # 不在选择区间的已完成或者待接单的订单 order1 = Order.query.filter(Order.end_date > s_date, Order.begin_date <= s_date, Order.status != 'REJECTED').all() order2 = Order.query.filter(Order.end_date >= e_date, Order.begin_date <= e_date, Order.status != 'REJECTED').all() # 在选择区间内的待接单和已完成的订单 # 所有的订单 和 房屋 house1 = House.query.all() # 不能预订的房源id f_house_id = [] for order in order1: f_house_id.append(order.house_id) for order in order2: f_house_id.append(order.house_id) house_id = [] for house in house1: if house.id not in f_house_id: house_id.append(house.id) # 获取到有效的房屋id值 并去重 houses_id = list(set(house_id)) # 获取 # ======================================================================= if int(h_id) in houses_id: order = Order() order.begin_date = s_date order.end_date = e_date order.house_id = h_id order.days = days house = House.query.get(order.house_id) house.order_count += 1 order.house_price = house.price order.amount = request.form.get('amount') order.user_id = session['user_id'] order.add_update() house.add_update() return jsonify({'code': 200, 'msg': '请求成功!'}) already_order = Order.query.filter(Order.house_id == h_id, Order.status != 'REJECTED').first() data = already_order.to_dict() return jsonify({'code': 1001, 'msg': '该时间段已被预订', 'data': data})
def submit_order(id): order = Order() order.user_id = session.get('user_id') order.house_id = id order.begin_date = request.form.get('startDate') order.end_date = request.form.get('endDate') order.days = request.form.get('days') order.house_price = request.form.get('price') order.amount = request.form.get('amount') order.add_update() return jsonify({'code': 200, 'msg': '请求成功'})
def stay(): if request.method == 'POST': order = Order() order.amount = request.form.get('t')[1:].split('.')[0] order.days = request.form.get('d') order.begin_date = request.form.get('st') order.end_date = request.form.get('et') order.house_price = int(order.amount) / int(order.days) order.user_id = session['user_id'] order.house_id = request.form.get('h_id') order.add_update() return jsonify({'code': 200, 'msg': '请求成功'})
def orders_post(): 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') house = House.query.get(house_id) order = Order() order.house_id = house_id order.user_id = session['user_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.house_price * order.days order.add_update() return jsonify(status_code.SUCCESS)
def order(): house_id = request.form.get('house_id') start_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d') end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d') house = House.query.filter(House.id == house_id).first() 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 order.house_price = house.price order.amount = order.days * order.house_price order.add_update() return jsonify(code=200, msg='ok', data=order.to_dict())
def my_orders(): order_dict = request.form house_id = order_dict.get('house_id') start_date = datetime.strptime(order_dict.get('start_date'), '%Y-%m-%d') end_date = datetime.strptime(order_dict.get('end_date'), '%Y-%m-%d') if not all([start_date,end_date]): return jsonify({'code':1001,'msg':'请选择日期'}) house = House.query.get(house_id) order = Order() order.user_id = current_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 order.add_update() return jsonify({'code':200})
def my_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') house = House.query.get(house_id) 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 = (end_date - begin_date).days + 1 order.house_price = house.price order.amount = order.days * order.house_price order.add_update() return jsonify({'code': 200, 'msg': '请求成功'})
def orders_info(): if request.method == 'POST': start_date = request.form.get('start_date') end_date = request.form.get('end_date') # order_amount = request.form.get('order_amount') days_amount = request.form.get('days_amount') house_id = session.get('house_id') user_id = session.get('user_id') house = House.query.get(house_id) order = Order() order.user_id = user_id order.house_id = house_id order.begin_date = start_date order.end_date = end_date days = int(float(days_amount) / house.price) # 判断days是否大于规定的 if days > house.max_days or days < house.min_days: return jsonify({'code': 1111, 'msg': '日期有误,请重新选择!'}) order.days = days order.house_price = house.price order.amount = days_amount order.add_update() session['order_id'] = order.id return jsonify({'code': 200, 'msg': '接收数据成功'}) if request.method == 'GET': # 生成订单号 seed = "1234567890abcdefghijklmnopqrstuvwxyz" sa = [] for i in range(11): sa.append(random.choice(seed)) order_num = ''.join(sa) order_id = session.get('order_id') order = Order.query.get(order_id) return jsonify({ 'code': 200, 'msg': '接收数据成功', 'order': order.to_dict(), 'order_num': order_num })
def submit_order(): """提交订单""" # 获取数据 user_id = session.get('user_id') if not user_id: return jsonify(status.NOT_LOGIN) house_id = session.get('house_id') sd = request.form.get('sd') ed = request.form.get('ed') house = House.query.filter(House.id == house_id).first() price = house.price days = int(request.form.get('days')) if not days >= house.min_days: return jsonify(status.SORT_TIME) max_days = house.max_days if house.max_days else 99999 if not days <= max_days: return jsonify(status.LONG_TIME) sd = datetime.strptime(sd, '%Y-%m-%d') ed = datetime.strptime(ed, '%Y-%m-%d') # 拿到所有订单判断时间段 orders = Order.query.filter( and_(Order.house_id == house_id, Order.status.in_(['WAIT_PAYMENT', 'PAID', 'WAIT_COMMENT']))) for order in orders: if ((order.begin_date <= sd) and (sd < order.end_date)) or \ ((order.begin_date < ed) and (ed < order.end_date)): return jsonify({'code': 990, 'msg': '此时间段已被预订'}) amount = price * days order = Order() order.user_id = user_id order.house_id = house_id order.begin_date = sd order.end_date = ed order.days = days order.house_price = price order.amount = amount order.add_update() return jsonify(status.SUCCESS)
def my_order(): # 获取用户id, 开始时间, 结束时间 order_dict = request.form house_id = order_dict.get('house_id') begin_date = datetime.strptime(order_dict.get('begin_date'), '%Y-%m-%d') end_date = datetime.strptime(order_dict.get('end_date'), '%Y-%m-%d') house = House.query.get(house_id) 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 = (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 orders_post(): house_id = request.form.get('house_id') start_date = request.form.get('start_date') start_time = datetime.datetime.strptime(start_date, '%Y-%m-%d') end_date = request.form.get('end_date') end_time = datetime.datetime.strptime(end_date, '%Y-%m-%d') days = (end_time-start_time).days + 1 price = House.query.get(house_id).price amount = days * price order = Order() order.user_id = session['user_id'] order.house_id = request.form.get('house_id') order.begin_date = start_time order.end_date = end_time order.days = days order.house_price = price order.amount = amount order.add_update() return jsonify(status_code.OK)
def booking_data(): house_id = int(request.form.get('house_id')) start_time = request.form.get('start_time') end_time = request.form.get('end_time') days = request.form.get('days') house_price = request.form.get('house_price') amount = request.form.get('amount') user_id = session.get('user_id') user_order = Order() user_order.user_id = user_id user_order.house_id = house_id user_order.begin_date = start_time user_order.end_date = end_time user_order.days = days user_order.house_price = house_price user_order.amount = amount user_order.add_update() house = House.query.filter_by(id=house_id).first() house.order_count += 1 house.add_update() return jsonify({'code': 200, 'msg': '请求成功'})
def my_booking(): data = request.form user_id = session['user_id'] if 'house_id' in data: order = Order() order.house_id = data.get('house_id') order.user_id = user_id order.begin_date = data.get('sd') order.end_date = data.get('ed') order.days = data.get('days') order.house_price = data.get('price') order.amount = data.get('amount') order.add_update() return jsonify({ 'code': 200, 'msg': '订单信息提交成功', }) return jsonify({'code': 1000})
def add_order(): if request.method == 'POST': startdate = request.form.get('startdate') enddate = request.form.get('enddate') house_id = request.form.get('house_id') days = int(request.form.get('days')) house = House.query.filter(House.id == house_id).first() if int(house.max_days) == 0: max_day = 100000 else: max_day = int(house.max_days) # 判断输入入住时间是否合法 if all([startdate, enddate, house_id]): # 判断入住时间是否在范围内 if (int(house.min_days) <= days <= max_day): # 判断下单房源是不是自己发布的房源 user_id = session.get('user_id') if house.user_id == user_id: return jsonify({'code': 702, 'msg': '不能对自己的房源下单!'}) else: # 确定不是自己房源之后,执行下单操作 house_price = house.price amount = house_price * days status = "WAIT_ACCEPT" # 创建订单 order = Order() order.user_id = user_id order.house_id = house_id order.begin_date = startdate order.end_date = enddate order.days = days order.house_price = house_price order.amount = amount order.status = status order.add_update() return jsonify({'code': 200, 'msg': '提交订单成功!'}) else: return jsonify({'code': 701, 'msg': '请输入在可入住范围内选择入住时间!'}) else: return jsonify({'code': 700, 'msg': '请选择正确的入住时间!'})
def my_booking(): # 获取数据 house_id = request.form.get('house_id') start_date = request.form.get('start_date') end_date = request.form.get('end_date') order_price = request.form.get('order_price') one_price = request.form.get('one_price') all_price = order_price.split('(')[0] day = order_price.split('共')[1].split('晚')[0] # 存入数据库 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 = day order.house_price = one_price order.amount = all_price order.add_update() return jsonify({'code': 200, 'msg': '请求成功'})
def my_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') # 获取当前用户和房屋id user_id = current_user.id house_id = request.form.get('house_id') # 获取房屋对象 house = House.query.get(house_id) if house.user_id == user_id: return jsonify(status_code.USER_HOUSE_IS_NOT_SAME) 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.amount = order.days * house.price order.house_price = house.price order.add_update() return jsonify(status_code.SUCCESS)
def create_order(): id = request.form.get('id') sd = request.form.get('sd') ed = request.form.get('ed') user_id = session.get('user_id') sd = datetime.datetime.strptime(sd, '%Y-%m-%d') ed = datetime.datetime.strptime(ed, '%Y-%m-%d') days = (ed - sd).days house = House.query.filter(House.id == id).first() price = house.price amount = days * price order = Order() order.user_id = user_id order.house_id = id order.begin_date = sd order.end_date = ed order.days = days order.house_price = price order.amount = amount order.status = 'WAIT_ACCEPT' order.add_update() return jsonify({'code': 200})
def booking_process(): user_id = session.get('user_id') house_id = request.form.get('house_id') begin_date = datetime.strptime(request.form.get('start_date'), '%Y-%m-%d') end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d') days = (end_date - begin_date).days if days == 0: days = 1 house_price = House.query.get(house_id).price amount = days * house_price order = Order() order.user_id = 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.amount = amount try: order.add_update() return jsonify(status_code.SUCCESS) except: return jsonify(status_code.DATABASE_ERROR)
def order(): # 创建订单模型 order = 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') # 获取当前用户和房屋id user_id = session['user_id'] house_id = request.form.get('house_id') # 获取房屋对象 house = House.query.get(house_id) # 写入订单模型数据 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 = house.price * order.days # 保存模型 order.add_update() return jsonify(status_code.SUCCESS)
def order(): # 接收house_id start_time end_time order_dict = request.form # 拿到房屋开始与结束时间 house_id = order_dict.get('house_id') begin_date = datetime.strptime(order_dict.get('begin_date'), '%Y-%m-%d') end_date = datetime.strptime(order_dict.get('end_date'), '%Y-%m-%d') house = House.query.get(house_id) 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 = (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)