def delete(self, food_id): food = Food.objects().with_id(food_id) food.delete() if food != None: return {"message": "delete successful"} else: return {"message": "not found food"}
def put(self, food_id): parser = reqparse.RequestParser() parser.add_argument(name="rate", type=float, location="json") body = parser.parse_args() # name = body.name # url = body.url # coint_old = body.coint_old # coint_new = body.coint_new # cout_rate = body.cout_rate rate_res = body.rate food = Food.objects().with_id(food_id) cout_rate = food.cout_rate rate = food.rate rate_new = (cout_rate * rate + rate_res) / (cout_rate + 1) food.update(cout_rate=cout_rate + 1, rate=rate_new) add_food = Food.objects().with_id(food_id) return mlab.item2json(add_food)
def post(self, id): parser = reqparse.RequestParser() parser.add_argument(name="id_user", type=str, location="json") parser.add_argument(name="message", type=str, location="json") body = parser.parse_args() id_user = body["id_user"] message = body["message"] print(id_user) print(message) food = Food.objects().with_id(id) user = User.objects().with_id(id_user) date = datetime.datetime.utcnow() + datetime.timedelta(hours=7) comment = Comment(user=user, message=message, date=str(date), food=food) comment.save() comment_add = Comment.objects().with_id(comment.id) return comment_add.get_json(), 200
def post(self): parser = reqparse.RequestParser() parser.add_argument(name="name", type=str, location="json") parser.add_argument(name="url", type=str, location="json") parser.add_argument(name="coint_old", type=str, location="json") parser.add_argument(name="coint_new", type=str, location="json") parser.add_argument(name="cout_rate", type=int, location="json") parser.add_argument(name="rate", type=float, location="json") body = parser.parse_args() name = body.name url = body.url coint_old = body.coint_old coint_new = body.coint_new cout_rate = body.cout_rate rate = body.rate food = Food(name=name, url=url, coint_new=coint_new, coint_old=coint_old, cout_rate=cout_rate, rate=rate) food.save() add_food = Food.objects().with_id(food.id) return mlab.item2json(add_food)
def post(self): parser = reqparse.RequestParser() parser.add_argument(name="items", type=list, location="json") parser.add_argument(name="address", type=str, location="json") parser.add_argument(name="phone_number", type=str, location="json") parser.add_argument(name="user", type=str, location="json") parser.add_argument(name="code", type=str, location="json") parser.add_argument(name="name", type=str, location="json") parser.add_argument(name="id", type=str, location="json") parser.add_argument(name="count", type=int, location="json") body = parser.parse_args() items = body["items"] date = datetime.datetime.today().strftime('%d-%m-%Y') address = body.address phone_number = body.phone_number user_id = body.user name = body.name code = body["code"] if items is None or date is None or address is None or phone_number is None or user_id is None: return {"message": "Thiếu trường "}, 400 if len(str(phone_number)) > 11 or len(str(phone_number)) < 10: return {"message": "Số điện thoại cc gì! sdt 10 or 11 số!"}, 402 try: sdt = float(phone_number) except: return {"message": "Số điện thoại lol gì mà có chữ?"}, 401 API_KEY = "AIzaSyCEB4QVng3uFEQ-SwxfwOWAG4H3sr7Mfi8" url_request = "https://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=vi-VN&key={2}".format( "301 Trường Chinh,Hà nội", str(address) + " Hà Nội", API_KEY) ship_spend = 0 try: req = requests.get(url_request) json_data = json.loads(req.content) list_add = json_data["rows"] elements = list_add[0]["elements"] km = elements[0]["distance"]["text"] txt = str(km).split(" ") distance = txt[0].replace(",", ".") ship_spend = float(distance) * 3000 except: ship_spend = -1 order_items = [] spend = 0 for i in range(0, len(items)): count = items[i]["count"] food_id = items[i]["id"] print("count:", items[i]["count"], ";id:", items[i]["id"]) try: sl = int(count) if sl < 1: return {"message": "Số lượng >0 ok?"}, 401 except: return {"message": "count là số int ok?"}, 401 food = Food.objects().with_id(food_id) single_order = SingleOrder(count=count, food=food) order_items.append(single_order) spend += (float(food["coint_new"]) * int(count)) if spend == 0: return {"message": "Đặt hàng cc gì mà giá bằng 0?"}, 401 try: user = User.objects().with_id(user_id) user.update(address=address, phone_number=phone_number, name=name) except: return {"message": "Đm gửi cái user id mà data mlab lưu ấy"}, 401 oder = Oder() code = str(code).lower() gift = GiftCode.objects(code=code).first() try: spend_min = gift.spend_min except: spend_min = -1 if (spend > 0 and (spend >= spend_min and spend_min != -1)): use_number = gift["use_number"] use_number -= 1 if use_number == 0: gift.delete() else: gift.update(set__use_number=use_number) oder = Oder(items=order_items, date=date, address=address, phone_number=phone_number, user=User.objects().with_id(user_id), is_Succues=False, spend=spend + ship_spend - gift.price, ship_money=ship_spend, code_price=gift.price, is_Received=False, is_Shipping=False) else: oder = Oder(items=order_items, date=date, address=address, phone_number=phone_number, user=User.objects().with_id(user_id), is_Succues=False, spend=spend + ship_spend, ship_money=ship_spend, code_price=0, is_Received=False, is_Shipping=False) if oder in Oder.objects(): return {"message": "Đăth lằm đặt lốn!"}, 404 oder.save() add_oder = Oder.objects().with_id(oder.id) #apikey = "AAAAhgLLpbs:APA91bGLuFvqDwvWs7L7RNnMHwus426M1fi5oLSP2azB5jOhB2jW2i91uIZF7jrECoyUYk-c-h5yyp4DY0oKFhEgg3S6o7fsv7dc-M5aBDdwxbYaWgXVn3HFEqImNYEm6xfyeMqb4lAR" #push_service = FCMNotification(api_key=apikey) #push_service.notify_topic_subscribers(topic_name="admin", message_body="Có đơn hàng mới", # message_title="Kiểm tra ngay") return mlab.item2json(add_oder), 200
def get_json(self): return { "food": Food.objects().with_id(self.food.id).get_json(), "count": self.count }
def get(selfk, food_id): food = Food.objects().with_id(food_id) return mlab.item2json(food)
def get(self): food = Food.objects() return mlab.list2json(food)
def get(self, id): food = Food.objects().with_id(id) comments = Comment.objects(food=food) return [comment.get_json() for comment in comments], 200
def get(self): food = Food.objects() return mlab.item2json(food)