def setUpClass(cls): ''' 在该函数中定义需要用到的模型,必须要先disconnect然后连接mock.之后的 定义要多少自己定 ''' disconnect() connect('mongoenginetest', host='mongomock://localhost') Stat.objects.create(name='size_of_collection', data={'user': 0, 'venue': 0 , 'court': 0, 'feedback': 0, 'notification': 0, 'share_notification': 0, 'reservation': 0, 'course': 0}) User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000001', privilege=0)
def create(cls, params): reservation = Reservation.objects( reservation_id=params['reservation_id']).first() #未付款、过期、已拼场的不符合要求 if reservation: if reservation.status != 2: return False, {"message": "场地不符合拼场要求"} if reservation.details['start'] < datetime.datetime.now(): return False, {"message": "该订单已经过期了"} if len(cls.objects(reservation=reservation.id)): return False, {"message": "该预定已经发布拼场通知"} user = User.objects(user_id=params['user_id']).first() share = cls.objects.create( user_id=user.user_id, content=params['content'], time=datetime.datetime.now(), reservation=reservation.id, status=1, share_id=Stat.add_object("share_notification")) user.invitation.append(share.id) user.save() return True, {"message": "ok", "share_id": share.share_id} else: return False, {"message": "找不到此预定"}
def transfer_reservation(request): ''' 转让场地 :param request: :return: ''' # 是新建一个订单,原来的状态改为已经转移,只有已经预定成功(含未付款)才能转让 params = json.loads(request.body) reservation = Reservation.objects( reservation_id=params['reservation_id']).first() if not reservation: return JsonResponse({"message": "找不到订单"}, status=400) if reservation.status not in [1, 2]: return JsonResponse({"message": "该订单不可转让"}, status=400) if reservation.details['start'] < datetime.datetime.now(): return JsonResponse({"message": "该订单已经过期了"}, status=400) user_now = User.objects(user_id=reservation.details['user_id']).first() if user_now.user_id != request.session.get('user_id'): return JsonResponse({"message": "你没有转移权限!"}, status=400) user_new = User.objects(user_id=params['new_user_id']).first() if not user_new: return JsonResponse({"message": "找不到目标用户"}, status=400) new_reservation = Reservation.objects.create( type=reservation.type, details=reservation.details, reservation_id=Stat.add_object("reservation"), status=reservation.status) new_reservation.details['user_id'] = user_new.user_id new_reservation.save() user_new.rent_now.append(new_reservation.id) reservation.status = 4 user_new.save() user_now.save() reservation.save() Share_notification.del_share(reservation.id) # 再到court里面去修改status,可能可以抽象成函数 court = Court.objects(id=reservation.details['court']).first() for status in court.Status: if status['start'] == reservation.details['start'] and \ status['end'] == reservation.details['end']: status['user_id'] = user_new.user_id break court.save() return JsonResponse({"message": "ok"})
def del_share(cls, reservation): ''' 传入reservation的objectID,如果有对应的拼场就删除 ''' share = cls.objects(reservation=reservation).first() if share: user = User.objects(user_id=share.user_id).first() user.invitation.remove(share.id) share.delete()
def book_first_come(request): """ 先到先得预定,接受用户id和要预定的场馆和时间段 : """ book_info = json.loads(request.body) print(book_info) court = Court.objects(court_id=book_info['court_id']).first() if not court: return JsonResponse({"message": "找不到场地"}, status=400) court_status = court.Status book_info['start'] = parse(book_info['start']) book_info['end'] = parse(book_info['end']) user = User.objects(user_id=request.session.get("user_id")).first() if not user: return JsonResponse({"message": "用户不存在或登陆过期,请重新登陆"}, status=400) cache_key = str(court.court_id) + str(book_info['start']) for status in court_status: if status['start'] == book_info['start'] \ and status['end'] == book_info['end']: if status[ 'code'] == 1: # 场地状态1,先到先得还能订此时用户就是第一个到的,时间段和场地状态都符合要求,直接预定成功 if cache.get(cache_key): return JsonResponse({"message": "error"}, status=400) cache.set(cache_key, 1, 50000) status["user_id"] = user.user_id status["code"] = 2 # 2是已经定了 court.save() reservation = Reservation( type=court.enum_id, details={ "court": court.id, "user_id": user.user_id, "start": book_info["start"], "end": book_info["end"], "created": datetime.datetime.now() }, reservation_id=Stat.add_object("reservation"), status=1) reservation.save() # 应该先保存,不然会导致读取不出id user.rent_now.append(reservation.id) court.save() user.save() return JsonResponse({ "message": "ok", "reservation_id": reservation.reservation_id }) return JsonResponse({"message": "error"}, status=400)
def book_draw(request): ''' 用户抽签预定 params:user_id,court_id,start,end ''' book_info = json.loads(request.body) court = Court.objects(court_id=book_info['court_id']).first() court_status = court.Status book_info['start'] = parse(book_info['start']) book_info['end'] = parse(book_info['end']) user = User.objects(user_id=request.session.get("user_id")).first() draw_now = Reservation.objects(id__in=user.rent_now, status=5).count() if draw_now >= 3: return JsonResponse({ "message": "您最多同时参与三场抽签", "draw_now": draw_now }, status=400) for status in court_status: if status['start'] == book_info['start'] \ and status['end'] == book_info['end']: if status['code'] == 3: # 可供抽签 if user.user_id in status['users_id']: return JsonResponse({"message": "您已经预定过这个场地了"}, status=400) reservation = Reservation( type=court.enum_id, details={ "court": court.id, "user_id": user.user_id, "start": book_info["start"], "end": book_info["end"], "created": datetime.datetime.now() }, reservation_id=Stat.add_object("reservation"), status=5) # 5是等待抽签 reservation.save() # 应该先保存,不然会导致读取不出id if 'reservation_ids' not in status.keys(): status['reservation_ids'] = [] status['reservation_ids'].append(reservation.id) status['users_id'].append(user.user_id) user.rent_now.append(reservation.id) court.save() user.save() return JsonResponse({ "message": "ok", "reservation_id": reservation.reservation_id }) return JsonResponse({"message": "找不到需要预定的时间段"}, status=400)
def get_reservations(request): ''' 获取用户当前预定信息 :param request: ''' user_id = int(request.GET['user_id']) user = User.objects(user_id=user_id)[0] rent_now_id = user.rent_now rent_now = Reservation.objects( id__in=rent_now_id).order_by("-reservation_id") courts = [] for rent in rent_now: feedback = Feedback.objects(reservation_id=rent.reservation_id).first() if feedback: reviewed = feedback.feedback_id else: reviewed = 0 share = Share_notification.objects(reservation=rent.id).first() if share: shared = share.share_id else: shared = 0 try: court_name = Court.objects(id=rent.details['court'])[0].name except Exception: continue court = { "reservation_id": rent.reservation_id, "type": rent.type, "status": rent.status, "details": { "name": court_name, "start": rent.details['start'], "end": rent.details['end'], "created": rent.details['created'] + datetime.timedelta(hours=8) }, "reviewed": reviewed, "shared": shared } if rent.status == 2: court["details"]["paid_at"] = rent.details[ "paid_at"] + datetime.timedelta(hours=8) courts.append(court) return JsonResponse({"message": "ok", "courts": courts})
def cancel_reservation(request): ''' 取消订单,把订单状态改为3,并且在场馆处修改状态。 对于未付款(1)的,将对应场地状态制空,可供先到先得 对于已付款的,认为不能取消了(退款功能没有实现,可以转让) 对于未抽签(5)的,将其从队列里面删除 :param request: :return: ''' params = json.loads(request.body) reservation = Reservation.objects( reservation_id=params['reservation_id']).first() if not reservation: return JsonResponse({"message": "找不到订单"}, status=400) user = User.objects(user_id=reservation.details['user_id']).first() if user.user_id != request.session.get('user_id'): return JsonResponse({"message": "你没有取消权限!"}, status=400) court = Court.objects(id=reservation.details['court']).first() if reservation.status == 1: reservation.status = 3 reservation.save() for status in court.Status: if status['start'] == reservation.details['start'] and \ status['end'] == reservation.details['end']: status['user_id'] = -1 status['code'] = 1 break court.save() elif reservation.status == 5: reservation.status = 3 reservation.save() for status in court.Status: if status['start'] == reservation.details['start'] and \ status['end'] == reservation.details['end']: try: status['reservation_ids'].remove(reservation.id) status['users_id'].remove(user.user_id) except: return JsonResponse({"message": "您已经从抽签中退出"}, status=400) break court.save() else: return JsonResponse({"message": "当前状态不可取消预定"}, status=400) Share_notification.del_share(reservation.id) return JsonResponse({"message": "ok"})
def create_feedback(cls, params): try: feedback = cls.objects( reservation_id=params['reservation_id']).first() if feedback: return False, "已经反馈过啦!" reservation = Reservation.objects( reservation_id=params['reservation_id']).first() feedback = cls.objects.create( user_id=params['user_id'], court=reservation.details['court'], content=params['content'], stars=params['stars'], time=datetime.datetime.now(), reply="等待管理员回复中", feedback_id=Stat.add_object("feedback"), reservation_id=params['reservation_id'], solved=False) #img_name = settings.STATIC_URL + str(feedback.feedback_id) + params['img'].name if params['img']: assert params['img'].name.endswith( ('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')) img_name = "static/feedback/" + str( feedback.feedback_id) + params['img'].name.split('.')[-1] feedback.img = img_name feedback.save() with open(img_name, 'wb+') as img_file: for chunk in params['img'].chunks(): img_file.write(chunk) else: feedback.img = "None" feedback.save() user = User.objects(user_id=params['user_id'])[0] user.feedback.append(feedback.id) user.save() return True, feedback.feedback_id except Exception: return False, "创建反馈失败!系统内部错误"
def delete_feedback(request): params = json.loads(request.body) feedback = Feedback.objects(feedback_id=params['feedback_id']).first() if not feedback: return JsonResponse({"message": "这条反馈已经不存在了..."}, status=500) if feedback.user_id != request.session.get( 'user_id') and request.session.get('privilege') != 1: return JsonResponse({"message": "您没有删除的权限,请确认登陆状态"}, status=403) user = User.objects(user_id=feedback.user_id).first() try: user.feedback.remove(feedback.id) user.save() except: pass feedback.delete() if feedback.img != "None": try: os.remove(feedback.img) except Exception: pass return JsonResponse({"message": "ok"})
def get_user_feedbacks(request): ''' 获取单个用户的反馈 :param request: :return: ''' page = int(request.GET['page']) size = int(request.GET['size']) user_id = int(request.GET['user_id']) user = User.objects(user_id=user_id).first() if not user: return JsonResponse({"message": "找不到该用户"}, status=400) feedbacks_all = Feedback.objects(id__in=user.feedback).order_by("-time") total = len(feedbacks_all) if page * size > total: feedbacks_page = feedbacks_all else: feedbacks_page = feedbacks_all[(page - 1) * size:page * size] feedbacks = [{ "feedback_id": feedback.feedback_id, "content": feedback.content, "publish_date": feedback.time + datetime.timedelta(hours=8), "court_id": Court.objects(id=feedback.court)[0].court_id, "img": feedback.img if feedback.img != "None" else "static/feedback/default.png", "reply": feedback.reply, "solved": feedback.solved, "stars": feedback.stars, "user_id": feedback.user_id } for feedback in feedbacks_page] return JsonResponse({ "message": "ok", "total": total, "feedbacks": feedbacks })
def setUpClass(cls): disconnect() connect('mongoenginetest', host='mongomock://localhost') Stat.objects.create(name='size_of_collection', data={ 'user': 0, 'venue': 0, 'court': 0, 'feedback': 0, 'notification': 0, 'share_notification': 0, 'reservation': 0, 'course': 0 }) user = User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000001', privilege=0) user2 = User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000002', privilege=0) venue = Venue.objects.create(name="击剑场", intro="击剑的场地", image="img", venue_id=Stat.add_object("venue")) court = Court.objects.create(name="击剑一号", venue=venue.id, status_now="开放", enum_id=1, Status=[{ 'start': datetime(2021, 12, 25, 7), 'end': datetime(2021, 12, 25, 8), "user_id": user.user_id, "code": 2 }, { 'start': datetime(2021, 12, 25, 8), 'end': datetime(2021, 12, 25, 9), "user_id": user.user_id, "code": 2 }], court_id=Stat.add_object("court"), price=15) venue.courts.append(court) venue.save() reservation = Reservation.objects.create( type=1, status=2, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2021, 12, 25, 7), 'end': datetime(2021, 12, 25, 8), 'created': datetime.now(), 'paid_at': datetime.now() }) reservation2 = Reservation.objects.create( type=1, status=2, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2021, 12, 25, 8), 'end': datetime(2021, 12, 25, 9), 'created': datetime.now(), 'paid_at': datetime.now() }) share = Share_notification.objects.create( user_id=user.user_id, time=datetime.now(), content="<p>很不错的场地</p>", reservation=reservation.id, img="None", share_id=Stat.add_object("share_notification")) share_to_be_delete = Share_notification.objects.create( user_id=user.user_id, time=datetime.now(), content="<p>很不错的场地</p>", reservation=reservation.id, img="None", share_id=Stat.add_object("share_notification")) user.invitation.append(share.id) user.rent_now.append(reservation.id) user.rent_now.append(reservation2.id) user.invitation.append(share_to_be_delete.id) user.save()
def setUpClass(cls): disconnect() connect('mongoenginetest', host='mongomock://localhost') Stat.objects.create(name='size_of_collection', data={ 'user': 0, 'venue': 0, 'court': 0, 'feedback': 0, 'notification': 0, 'share_notification': 0, 'reservation': 0, 'course': 0 }) user = User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000001', privilege=0) user2 = User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000002', privilege=1) user3 = User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000003', privilege=0) venue = Venue.objects.create(name="击剑场", intro="击剑的场地", image="img", venue_id=Stat.add_object("venue")) court = Court.objects.create(name="击剑一号", venue=venue.id, status_now="开放", enum_id=1, Status=[{ 'start': datetime(2021, 12, 25, 7), 'end': datetime(2021, 12, 25, 8), "user_id": user.user_id, "code": 2 }, { 'start': datetime(2021, 12, 25, 8), 'end': datetime(2021, 12, 25, 9), "user_id": user.user_id, "code": 2 }, { 'start': datetime(2021, 12, 25, 9), 'end': datetime(2021, 12, 25, 10), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 10), 'end': datetime(2021, 12, 25, 11), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 11), 'end': datetime(2021, 12, 25, 12), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 12), 'end': datetime(2021, 12, 25, 13), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 13), 'end': datetime(2021, 12, 25, 14), "user_id": -1, "code": 1 }], court_id=Stat.add_object("court"), price=15) venue.courts.append(court) venue.save() reservation = Reservation.objects.create( type=1, status=2, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2021, 12, 25, 7), 'end': datetime(2021, 12, 25, 8), 'created': datetime.now(), 'paid_at': datetime.now() }) #已经付款 reservation2 = Reservation.objects.create( type=1, status=5, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2021, 12, 20, 8), 'end': datetime(2021, 12, 20, 9), 'created': datetime.now() }) #过期,已经创建反馈 reservation3 = Reservation.objects.create( type=1, status=2, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2020, 11, 25, 8), 'end': datetime(2020, 11, 25, 9), 'created': datetime.now(), 'paid_at': datetime.now() }) #过期,可以用来创建反馈 feedback = Feedback.objects.create( user_id=1, time=datetime.now, stars=5, content="<p>很好,五星好评蛤蛤蛤哈</p>", reply="wu", court=court.id, feedback_id=Stat.add_object("feedback"), img="None", solved=False, reservation_id=reservation2.reservation_id) feedback2 = Feedback.objects.create( user_id=1, time=datetime.now, stars=5, content="<p>很好,五星好评蛤蛤蛤哈</p>", reply="wu", court=court.id, feedback_id=Stat.add_object("feedback"), img="None", solved=False, reservation_id=reservation3.reservation_id) #用来删除 user.rent_now.append(reservation.id) user.rent_now.append(reservation2.id) user.rent_now.append(reservation3.id) user.feedback.append(feedback.id) user.feedback.append(feedback2.id) user.save()
def setUpClass(cls): ''' 在该函数中定义需要用到的模型,必须要先disconnect然后连接mock.之后的 定义要多少自己定 ''' disconnect() connect('mongoenginetest', host='mongomock://localhost') Stat.objects.create(name='size_of_collection', data={ 'user': 0, 'venue': 0, 'court': 0, 'feedback': 0, 'notification': 0, 'share_notification': 0, 'reservation': 0, 'course': 0 }) user = User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000001', privilege=0) user2 = User.create(password='******', user_id=Stat.add_object("user"), name='test', api_id='2018000002', privilege=0) venue = Venue.objects.create(name="击剑场", intro="击剑的场地", image="img", venue_id=Stat.add_object("venue")) court = Court.objects.create(name="击剑一号", venue=venue.id, status_now="开放", enum_id=1, Status=[{ 'start': datetime(2021, 12, 25, 7), 'end': datetime(2021, 12, 25, 8), "user_id": user.user_id, "code": 2 }, { 'start': datetime(2021, 12, 25, 8), 'end': datetime(2021, 12, 25, 9), "user_id": user.user_id, "code": 2 }, { 'start': datetime(2021, 12, 25, 9), 'end': datetime(2021, 12, 25, 10), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 10), 'end': datetime(2021, 12, 25, 11), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 11), 'end': datetime(2021, 12, 25, 12), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 12), 'end': datetime(2021, 12, 25, 13), "user_id": -1, "code": 3, "reservation_ids": [], "users_id": [] }, { 'start': datetime(2021, 12, 25, 13), 'end': datetime(2021, 12, 25, 14), "user_id": -1, "code": 1 }], court_id=Stat.add_object("court"), price=15) venue.courts.append(court) venue.save() reservation = Reservation.objects.create( type=1, status=2, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2021, 12, 25, 7), 'end': datetime(2021, 12, 25, 8), 'created': datetime.now(), 'paid_at': datetime.now() }) #已经付款,用来转让 reservation2 = Reservation.objects.create( type=1, status=5, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2021, 12, 25, 8), 'end': datetime(2021, 12, 25, 9), 'created': datetime.now() }) reservation3 = Reservation.objects.create( type=1, status=2, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2020, 11, 25, 8), 'end': datetime(2020, 11, 25, 9), 'created': datetime.now(), 'paid_at': datetime.now() }) #过期 reservation4 = Reservation.objects.create( type=1, status=1, reservation_id=Stat.add_object("reservation"), details={ "court": court.id, "user_id": user.user_id, 'start': datetime(2021, 12, 25, 7), 'end': datetime(2021, 12, 25, 8), 'created': datetime.now(), 'paid_at': datetime.now() }) #用于取消 user.rent_now.append(reservation.id) user.rent_now.append(reservation2.id) user.rent_now.append(reservation3.id) user.rent_now.append(reservation4.id) user.save()