def food_search(): values = request.values cat_id = get_int(values, "cat_id", 0) mix_kw = values.get("mix_kw", "") current_page = get_int(values, "p", 1) food_info_query = Food.query.filter_by(status=1) if len(mix_kw) > 0: pattern = "%%%s%%" % values["mix_kw"] rule = or_(Food.name.ilike(pattern), Food.tags.ilike(pattern)) food_info_query = food_info_query.filter(rule) if cat_id > 0: food_info_query = food_info_query.filter_by(cat_id=cat_id) num_items = food_info_query.count() items_per_page = app.config["APP_FOOD_INDEX_ITEMS_PER_PAGE"] offset = (current_page - 1) * items_per_page food_info_query = food_info_query.order_by(Food.total_count.desc(), Food.id.desc()) food_info_list = food_info_query.offset(offset).limit(items_per_page).all() food_list = [] if food_info_list: food_list = [{"id": food.id, "name": food.name, "price": str(food.price), "min_price": str(food.price), "pic_url": build_image_url(food.main_image) } for food in food_info_list] data = {"list": food_list, "has_next_page": math.ceil(num_items / items_per_page) > current_page} return json_response(data=data)
def food_info(): values = request.values food_id = get_int(values, "id", 0) if food_id == 0: return json_error_response("该菜品不存在!") food_info = Food.query.filter_by(id=food_id).first() if food_info is None: return json_error_response("该菜品不存在!") if food_info.status != 1: return json_error_response("该菜品已下架!") member_info = g.current_member member_id = member_info.id if member_info else 0 main_image_url = build_image_url(food_info.main_image) info = { "id": food_id, "name": food_info.name, "summary": food_info.summary, "total_count": food_info.total_count, "comment_count": food_info.comment_count, "stock": food_info.stock, "price": str(food_info.price), "main_image": main_image_url, "pics": [main_image_url], "cart_quantity": get_cart_quantity(member_id, food_id) } return json_response(data={"info": info})
def list_image(): """ List the url of all images that are uploaded """ res = {"state": "FAILURE", "list": [], "start": 0, "total": 0} start = int(request.values.get("start", "0")) page_size = int(request.values.get("size", "20")) # managing offset for pagination effect in image list display query = Image.query if start > 0: query = query.filter(Image.id > start) # get list of images from database query img_info_list = query.order_by(Image.id.desc()).limit(page_size).all() if img_info_list: img_list = [{ "url": build_image_url(img.file_key) } for img in img_info_list] else: return res res["start"] = img_info_list[-1].id res["state"] = "SUCCESS" res["list"] = img_list res["total"] = len(img_list) return make_response(jsonify(res))
def cart_index(): if g.current_member is None: return json_error_response("您需要登录才能使用购物车!") if g.current_member.status != 1: return json_error_response("该账户已被注销,无法使用购物车") member_id = g.current_member.id cart_info_list = MemberCart.query.filter_by(member_id=member_id).all() if cart_info_list is None: data = {"list": None} return json_response(data=data) # this is different from what the imooc course does cart_food_info_list = db.session.query(MemberCart, Food)\ .filter(MemberCart.food_id == Food.id).all() cart_list = [{ "id": cart_info.id, "food_id": food_info.id, "pic_url": build_image_url(food_info.main_image), "name": food_info.name, "price": str(food_info.price), "quantity": cart_info.quantity, "active": True } for cart_info, food_info in cart_food_info_list] app.logger.info("length of list %d" % len(cart_list)) total_price = sum(float(item["price"]) for item in cart_list) data = { "list": cart_list, "totalPrice": str(Decimal(total_price).quantize(Decimal("0.00"))), } return json_response(data=data)
def order_info(): member_id = g.current_member.id purchase_list = request.form.get("purchaseList", None) if purchase_list is None: return json_error_response("订单内容不能为空!") purchase_list = json.loads(purchase_list) if len(purchase_list) < 1: return json_error_response("订单内容不能为空!") food_id_to_quantity = { item["food_id"]: item["quantity"] for item in purchase_list } food_ids = food_id_to_quantity.keys() food_info_list = Food.query.filter(Food.id.in_(food_ids)).all() if len(food_info_list) < 1: return json_error_response("无法查询到订单中的菜品!") deliver_price = Decimal(5000.00) order_list = [{ "food_id": food.id, "name": food.name, "price": str(food.price), "pic_url": build_image_url(food.main_image), "quantity": food_id_to_quantity[food.id], } for food in food_info_list] pay_price = Decimal( sum(food.price * food_id_to_quantity[food.id] for food in food_info_list)) default_address = {} default_addr_info = MemberAddress.query.filter_by( status=1, is_default=1, member_id=member_id).first() if default_addr_info: default_address = { "id": default_addr_info.id, "name": default_addr_info.contact_name, "mobile": default_addr_info.mobile, "address": "%s%s%s%s" % (default_addr_info.province_str, default_addr_info.city_str, default_addr_info.district_str, default_addr_info.address) } data = { "order_list": order_list, "deliver_price": str(deliver_price), "pay_price": str(pay_price), "total_price": str(pay_price + deliver_price), "default_address": default_address } return json_response(data=data)
def upload_image(): """ Save image to server """ # initialize json dict to return to ueditor res = {"state": "FAILURE", "url": "", "title": "", "original": ""} upfile = request.files.get("upfile", None) if upfile is None: return make_response(jsonify(res)) upload_res = upload_by_file(upfile) if upload_res["code"] != 200: res["state"] = "FAILURE: " + upload_res["msg"] return make_response(jsonify(res)) res["state"] = "SUCCESS" # the url field in res is important for front-end url editor to retrieve image from backend and show in editor res["url"] = build_image_url(upload_res["data"]["file_key"]) return make_response(jsonify(res))
def my_order_info(): order_sn = request.values.get("order_sn", "") if not order_sn: return json_error_response("查询订单信息错误,请稍后再试(1)") pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first() if not pay_order_info: return json_error_response("查询订单信息错误,请稍后再试(2)") pay_wait_time = app.config["PAY_WAIT_TIME"] deadline = pay_order_info.created_time + datetime.timedelta(minutes=pay_wait_time) address_info = json.loads(pay_order_info.deliver_info) data = { "order_sn": pay_order_info.order_sn, "status": pay_order_info.pay_status, "status_desc": pay_order_info.pay_status_desc, "deadline": deadline.strftime("%Y-%m-%d %H:%M"), "address": address_info, "base_price": str(pay_order_info.base_price), "shipping_price": str(pay_order_info.shipping_price), "total_price": str(pay_order_info.total_price), } # "poi" short for "PayOrderItem" poi_and_food_list = db.session.query(PayOrderItem, Food).filter( PayOrderItem.pay_order_id == pay_order_info.id, PayOrderItem.food_id == Food.id ) goods = [{ "pic_url": build_image_url(food.main_image), "name": food.name, "price": str(poi.price), "unit": poi.quantity } for poi, food in poi_and_food_list] data["goods"] = goods return json_response(data=data)
def food_index(): # get food info from database food_cat_info_list = FoodCat.query.filter_by(status=1).order_by(FoodCat.weight.desc()).all() food_cat_list = [{"id": 0, "name": "全部"}] if food_cat_info_list: food_cat_list = food_cat_list + [{"id": cat.id, "name": cat.name} for cat in food_cat_info_list] else: food_cat_list = None food_info_list = Food.query.filter_by(status=1).order_by(Food.total_count.desc(), Food.id).limit(3).all() if food_info_list: food_list = [{"id": food.id, "name": food.name, "pic_url": build_image_url(food.main_image)} for food in food_info_list] else: food_list = None data = { "bannerList": food_list, "catList": food_cat_list } return json_response(data=data)
def my_order(): member_id = g.current_member.id status = utils.get_int(request.form, "status", None) if status is None: return json_error_response("查询订单信息失败,请注明订单状态") pay_order_query = PayOrder.query.filter_by(member_id=member_id) if status == -8: #待付款 pay_order_query = pay_order_query.filter(PayOrder.status == -8) elif status == -7: #待付款 pay_order_query = pay_order_query.filter(PayOrder.status == 1, PayOrder.deliver_status == -7) elif status == -6: #待确认 pay_order_query = pay_order_query.filter(PayOrder.status == 1, PayOrder.deliver_status == -6) elif status == -5: #待评价 pay_order_query = pay_order_query.filter(PayOrder.status == 1, PayOrder.deliver_status == 1, PayOrder.comment_status == 0) elif status == 1: #已完成 pay_order_query = pay_order_query.filter(PayOrder.status == 1, PayOrder.deliver_status == 1, PayOrder.comment_status == 1) elif status == 0: #未完成 pay_order_query = pay_order_query.filter(PayOrder.status.in_([0, -1, -2, -9])) else: return json_error_response("查询订单信息失败,订单状态有误") # TODO: use table join method to retrieve data pay_order_list = pay_order_query.order_by(PayOrder.id.desc()).all() pay_order_data_list = [] if pay_order_list: pay_order_ids = [pay_order.id for pay_order in pay_order_list] pay_order_items_list = PayOrderItem.query.filter(PayOrderItem.pay_order_id.in_(pay_order_ids)) food_ids = [item.food_id for item in pay_order_items_list] fid_to_info = utils.get_id_to_model_dict(Food, "id", Food.id, food_ids) oid_to_items = {} for item in pay_order_items_list: oid = item.pay_order_id fid = item.food_id if oid not in oid_to_items: oid_to_items[oid] = [] food_info = fid_to_info[fid] oid_to_items[oid].append({ "pay_order_item_id": item.id, "food_id": fid, "quantity": item.quantity, "pic_url": build_image_url(food_info.main_image), "name": food_info.name }) pay_order_data_list = [{ "status": order.pay_status, "status_desc": order.pay_status_desc, "date": order.created_time.strftime("%Y-%m-%d %H:%M:%S"), "order_number": order.order_number, "order_sn": order.order_sn, "note": order.note, "total_price": str(order.total_price), "goods_list": oid_to_items[order.id] } for order in pay_order_list] data = {"pay_order_list": pay_order_data_list} return json_response(data=data)