def get_all_feedback(request): ''' 获取所有反馈 :param request: :return: ''' page = int(request.GET['page']) size = int(request.GET['size']) feedbacks_all = Feedback.objects().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 create_feedback(request): ''' 创建反馈,接受的是form-data 目前图片处理尚未完成 :param request: :return: ''' params = request.POST img = request.FILES.get('img') ok, message = check_html_content(params['content']) if not ok: return JsonResponse({"message": message}, status=400) ok, feedback_id = Feedback.create_feedback({ "user_id": int(params['user_id']), "stars": int(params['stars']), "content": params["content"], "img": img, "reservation_id": int(params['reservation_id']) }) if ok: return JsonResponse({"message": "ok", "feedback_id": feedback_id}) else: return JsonResponse({"message": feedback_id}, status=500)
def update_feedback(request): # 目前上传新的图片会更改图片的名字,但是原来的图片没有删除。删除反馈的图片时候也没有删除 try: params = request.POST ok, message = check_html_content(params['content']) if not ok: return JsonResponse({"message": message}, status=400) feedback = Feedback.objects( feedback_id=int(params['feedback_id'])).first() feedback.content = params['content'] feedback.stars = int(params['stars']) img = request.FILES.get("img") if img: assert img.name.endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')) img_name = "static/feedback/" + str( feedback.feedback_id) + '.' + img.name.split('.')[-1] feedback.img = img_name feedback.save() with open(img_name, 'wb+') as img_file: for chunk in img: img_file.write(chunk) feedback.time = datetime.datetime.now() feedback.save() return JsonResponse({"message": "ok"}) except Exception: return JsonResponse({"message": "服务器内部错误"}, status=500)
def reply_feedback(request): ''' 管理员回复 ''' params = json.loads(request.body) feedback = Feedback.objects(feedback_id=params['feedback_id']).first() if not feedback: return JsonResponse({"message": "这条反馈已经不存在了..."}, status=500) feedback.reply = params['reply'] feedback.solved = params['solved'] feedback.save() return JsonResponse({"message": "ok"})
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 get_venues_info(request): ''' 获取场馆信息 :return: ''' venue_id = request.GET['id'] venue = Venue.objects(venue_id=venue_id).first() if venue == None: return JsonResponse({"message": "require correct venue id"}, status=401) courts = venue.courts notices_id = venue.notices feedbacks = Feedback.objects(court__in=courts).order_by("-time") if len(feedbacks): feedback = feedbacks[0] review = { "content": feedback.content, "stars": feedback.stars, "publish_date": feedback.time } else: review = {} notices_raw = Notification.objects(id__in=notices_id).order_by("-time")[:3] notices = [] for notice in notices_raw: notices.append({ "title": notice.title, "content": notice.content, "time": notice.time }) return JsonResponse({ "message": "ok", "venue_info": { "id": venue.venue_id, "name": venue.name, "description": venue.intro, "img": venue.image, "review": review, "notices": notices } })
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"})