def post(self, request): # 获取到当前用户的id user = request.user # 获取到传入的参数 dict_data = json.loads(request.body.decode()) house_id = dict_data.get('house_id') start_date_str = dict_data.get('start_date') end_date_str = dict_data.get('end_date') # 校验参数 if not all([house_id, start_date_str, end_date_str]): return http.JsonResponse({"errno": RET.PARAMERR, "errmsg": "参数错误"}) try: start_date = datetime.datetime.strptime(start_date_str, "%Y-%m-%d") end_date = datetime.datetime.strptime(end_date_str, "%Y-%m-%d") assert start_date < end_date, Exception('开始日期大于结束日期') # 计算出入住天数 days = (end_date - start_date).days except Exception as e: logger.error(e) return http.JsonResponse({"errno": RET.PARAMERR, "errmsg": "参数错误"}) # 判断房屋是否存在 try: house = House.objects.get(id=house_id) except Exception as e: logger.error(e) return http.JsonResponse({"errno": RET.NODATA, "errmsg": "房屋不存在"}) # 判断房屋是否是当前登录用户的 if user.id == house.user.id: return http.JsonResponse({ "errno": RET.ROLEERR, "errmsg": "不能订购自己的房间" }) # 查询是否存在冲突的订单 try: filters = { "house": house, "begin_date__lt": end_date, "end_date__gt": start_date } count = Order.objects.filter(**filters).count() except Exception as e: logger.error(e) return http.JsonResponse({"errno": RET.DBERR, "errmsg": "数据库查询错误"}) if count > 0: return http.JsonResponse({ "errno": RET.DATAERR, "errmsg": "房间已经被预定" }) amount = days * house.price # 生成订单的模型 order = Order() order.user = user order.house = house order.begin_date = start_date order.end_date = end_date order.days = days order.house_price = house.price order.amount = amount try: order.save() except Exception as e: logger.error(e) return http.JsonResponse({"errno": RET.DBERR, "errmsg": "数据库保存失败"}) return http.JsonResponse({ "errno": RET.OK, "errmsg": "发布成功", "data": { "order_id": order.pk } })
def post(self,request): data_dict = json.loads(request.body.decode()) house_id = data_dict.get('house_id') start_date = data_dict.get('start_date') end_date = data_dict.get('end_date') user = request.user #判断完整 if not all([house_id,start_date,end_date]): return JsonResponse({'errno': 400, 'errmsg': '缺少必传参数'}) #是否存在 try: house = House.objects.get(id=house_id) except Exception as e: return JsonResponse({"errno": 400, "errmsg": "房屋不存在"}) #是否房主 if user.id == house.user.id: return JsonResponse({"errno": 400, "errmsg": "是房主无法预定"}) #跳转页面 #判断是否传入错误数据 try: d1 = datetime.datetime.strptime(start_date, '%Y-%m-%d') d2 = datetime.datetime.strptime(end_date, '%Y-%m-%d') assert d1 < d2, Exception('开始日期大于结束日期') days = (d2 - d1).days if days < 0: return JsonResponse({'errno': 400, 'errmsg': '日期有误'}) except Exception as e: return JsonResponse({'errno': 400, 'errmsg': '参数有误'}) #开启事务 # with transaction.atomic(): # # save_id = transaction.savepoint() # try: amount = days * house.price if days < house.min_days: return JsonResponse({'errno': 400, 'errmsg': '住的时间太短'}) if days > house.max_days: return JsonResponse({'errno': 400, 'errmsg': '住的时间太长'}) order = Order() order.user_id = user.id order.house_id = house.id order.begin_date = d1 order.end_date = d2 order.days = days order.amount = amount order.house_price = house.price order.status = Order.ORDER_STATUS['WAIT_ACCEPT'] order.save() # order = Order.objects.create(user_id=user.id, # house_id=house_id, # begin_date=d1, # end_date=d2, # days=days, # amount=amount, # # # status=Order.ORDER_STATUS['PAID'], # price=house.price, # ) # 判断用户下单的时间段是否有别的订单 count = Order.objects.filter(house_id=house_id, begin_date__lte=end_date, end_date__gte=start_date).count() if count > 0: # 说明房子被别人预定 # transaction.savepoint_rollback(save_id) return JsonResponse({'errno': 400, 'errmsg': '房子已被预定'}) # except Exception as e: # transaction.savepoint_rollback(save_id) # return JsonResponse({'errno': 400, 'errmsg': '下单失败'}) #提交事务 # transaction.savepoint_commit(save_id) return JsonResponse({'errno': 0, 'errmsg': '添加订单成功', "data": {"order_id": order.pk}})