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 create_course(request): course_id = Stat.add_object("course") params = json.loads(request.body) try: name = params['name'] assert check_content_len(name, min=3, max=10) == (True, "ok") except: return JsonResponse({"message": "requires correct course name"}, status=401) try: price = params['price'] assert len(price) > 0 except: return JsonResponse({"message": "requires correct price"}, status=401) try: tel = params['tel'] assert check_content_len(tel, min=3, max=20) == (True, "ok") except: return JsonResponse({"message": "requires correct telephone"}, status=401) try: intro = params['intro'] assert check_content_len(intro, min=3, max=100) == (True, "ok") except: return JsonResponse({"message": "requires correct introduction"}, status=401) course = Course(course_id=course_id, name=name, price=price, tel=tel, intro=intro).save() return JsonResponse({"message": "ok", "course_id": course_id})
def create_court(request): params = json.loads(request.body) court_id = Stat.add_object("court") try: venue_id = params['venue_id'] this_venue = Venue.objects(venue_id=int( venue_id)).first() # the venue found with id in database courts_ls = this_venue.courts except: return JsonResponse({"message": "venue id error"}, status=401) try: name = params['name'] assert check_content_len(name, min=3, max=10) == (True, "ok") except: return JsonResponse({"message": "requires court name"}, status=401) try: price = params['price'] assert price > 0 except: return JsonResponse({"message": "requires a integer price"}, status=401) try: type = params['type'] assert 0 <= type <= 5 except: return JsonResponse({"message": "venue sport type error"}, status=401) try: param_status = params['status'] # from parameters all_status = [] for status in param_status: # iterate status in each court in POST params new_status = { "start": parse(status['start']), "end": parse(status['end']), "code": status['code'] } all_status.append(new_status) except: return JsonResponse({"message": "status error"}, status=401) new_court = Court(name=name, enum_id=type, venue=this_venue.id, rent_queue=[], draw_list=[], rent_for_long=[], Status=all_status, status='开放', court_id=court_id, price=price).save() courts_ls.append(new_court.id) Venue.objects(venue_id=venue_id).update_one(set__courts=courts_ls) return JsonResponse({"message": "ok"})
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 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_venue(request): venue_id = Stat.add_object("venue") try: name = request.POST.get('name') assert check_content_len(name, min=3, max=10) == (True, "ok") except: return JsonResponse({"error": "require venue name"}, status=401) try: description = request.POST.get('description') assert check_content_len(description, min=3, max=100) == (True, "ok") assert type(name) == str except: return JsonResponse({"error": "require venue description"}, status=401) try: img = request.FILES.get('img') assert img.name.endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')) if img.size < 128 or img.size > 2048**2: return JsonResponse({"error": "image size invalid"}, status=401) try: img_format = img.name.split('.')[-1] img_name = 'venue_' + str(venue_id) + '_img.' + img_format Venue.objects(venue_id=venue_id).update_one( set__image='static/venue/' + img_name) if not os.path.exists('static/'): os.mkdir('static') if not os.path.exists('static/venue/'): os.mkdir('static/venue') with open('static/venue/' + img_name, 'wb+') as destination: for chunk in img.chunks(): destination.write(chunk) except: return JsonResponse({"error": "save image failed"}, status=500) except: return JsonResponse({"error": "image format illegal"}, status=401) Venue(name=name, intro=description, courts=[], image='static/venue/' + img_name, venue_id=venue_id, notices=[]).save() return JsonResponse({"message": "ok", "venue_id": venue_id})
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 signup(request): ''' :param request: :return: 目前可以根据注册人数自动增加id,但是还没有做重复的检查,工会id不能重复 ''' params = json.loads(request.body) ok,message=check_signup(params) if not ok: return JsonResponse({"message":message+"字段错误"},status=400) user=User.objects(api_id=params['api_id']).first() if user: return JsonResponse({"message": "该工号已经注册过啦!请进入登陆界面", "user_id": user.user_id},status=403) user = User.create(password=params['pwd'], user_id=Stat.add_object("user"), name=params['name'], api_id=params['api_id'],privilege=0) return JsonResponse({"message": "ok", "user_id": user.user_id})
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 create_notice(request): notice_id = Stat.add_object("notification") params = json.loads(request.body) try: title = params['title'] assert check_content_len(title, min=3, max=10) == (True, "ok") except: return JsonResponse({"error": "requires correct title"}, status=401) try: content = params['content'] assert check_content_len(content, min=3, max=100) == (True, "ok") except: return JsonResponse({"error": "requires correct content"}, status=401) notice = Notification(title=title, content=content, notice_id=notice_id, time=datetime.datetime.now()).save() return JsonResponse({"message": "ok", "notice_id": notice_id})
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()