def cart_set(): member_id = g.current_member.id food_id = get_int(request.form, "food_id", 0) food_info = Food.query.filter_by(id=food_id).first() if food_id < 1 or food_info is None: return json_error_response("该菜品不存在") quantity = get_int(request.form, "quantity", None) if quantity is None or quantity < 0: return json_error_response("请提供正确的菜品数量") if food_info.stock < quantity: return json_error_response("该菜品库存不足") if quantity > 0: cart_info = set_cart_info(member_id, food_id, quantity) if cart_info is None: return json_error_response("添加至购物车失败") else: return json_response("成功添加菜品至购物车") else: if delete_cart_info(member_id, food_id): return json_error_response("删除菜品失败") else: return json_response("成功从购物车删除菜品")
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 my_address_set(): member_id = g.current_member.id addr_id = utils.get_int(request.form, "id", 0) contact_name = request.form.get("contact_name", "") mobile = request.form.get("mobile", "") address = request.form.get("address", "") province_id = utils.get_int(request.form, "province_id", 0) city_id = utils.get_int(request.form, "city_id", 0) district_id = utils.get_int(request.form, "district_id", 0) province_str = request.form.get("province_str", "") city_str = request.form.get("city_str", "") district_str = request.form.get("district_str", "") empty_items = [] for var, item in zip([contact_name, mobile, address, province_str, city_str, province_id, city_id], ["联系人姓名", "手机号码", "详细地址", "省份名称", "城市名称", "省份代码", "城市代码"]): if not var: empty_items.append(item) if len(empty_items) > 0: return json_error_response("设置地址时以下内容不能为空:" + "、".join(empty_items)) # check if addr_info already exists addr_info = MemberAddress.query.filter_by(id=addr_id).first() if addr_info: if addr_info.member_id != member_id: return json_error_response("修改地址时出现错误(1)") if not addr_info: default_addr_cnt = MemberAddress.query.filter_by(is_default=1, member_id=member_id, status=1).count() addr_info = MemberAddress() addr_info.member_id = member_id addr_info.is_default = 1 if default_addr_cnt == 0 else 0 addr_info.created_time = utils.get_current_time() addr_info.contact_name = contact_name addr_info.mobile = mobile addr_info.province_id = province_id addr_info.province_str = province_str addr_info.city_id = city_id addr_info.city_str = city_str addr_info.district_id = district_id addr_info.district_str = district_str addr_info.address = address addr_info.updated_time = utils.get_current_time() db.session.add(addr_info) db.session.commit() return json_response("操作成功")
def ops(): id = get_int(request.form, "id", 0) act = request.form.get("act", None) if "act" is None or "id" == 0: return json_error_response("无效的菜品项目编辑操作") food_info = Food.query.filter_by(id=id).first() if not food_info: return json_error_response("无效的菜品项目编辑操作") if act == "remove": food_info.status = 0 success_msg = "成功移除菜品项目 %s" % (food_info.name) elif act == "recover": success_msg = "成功恢复菜品项目 %s" % (food_info.name) food_info.status = 1 else: return json_error_response("无效的账号编辑操作") food_info.update_time = get_current_time() db.session.add(food_info) db.session.commit() return json_response(success_msg)
def cat(): current_page = get_int(request.args, "p", 1) values = request.values items_per_page = app.config["FOOD_CAT_ITEMS_PER_PAGE"] food_cat_query = FoodCat.query.order_by(FoodCat.status.desc(), FoodCat.weight.desc()) # filtering by status if "status" in values: if values["status"] != "-1": food_cat_query = food_cat_query.filter_by( status=int(values["status"])) # pagination offset = (current_page - 1) * items_per_page pagination_dict = pagination(num_items=food_cat_query.count(), items_per_page=items_per_page, current_page=current_page, url=build_url("/food/cat?")) food_cat_info_list = food_cat_query.offset(offset).limit( items_per_page).all() page_params = { "food_cat_info_list": food_cat_info_list, "pagination": pagination_dict, "search": { "status": values.get("status", "-1") }, "status_mapping": app.config["ACCOUNT_STATUS_MAPPING"] } return render_template_with_global_vars("food/cat.html", context=page_params)
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 my_address_ops(): addr_id = utils.get_int(request.form, "id", 0) action = request.form.get("action", "") addr_info = MemberAddress.query.filter_by(id=addr_id).first() if not addr_info: return json_error_response("地址更改操作错误(1)") if action == "set_default": member_id = g.current_member.id current_time = utils.get_current_time() MemberAddress.query.filter_by(member_id=member_id) \ .update({"is_default": 0, "updated_time": current_time}) addr_info.is_default = 1 addr_info.updated_time = utils.get_current_time() db.session.add(addr_info) db.session.commit() return json_response() elif action == "delete": addr_info.status = 0 addr_info.updated_time = utils.get_current_time() db.session.add(addr_info) db.session.commit() latest_addr = MemberAddress.query.filter_by(status=1).order_by(MemberAddress.updated_time.desc()).first() if latest_addr: latest_addr.is_default = 1 db.session.add(latest_addr) db.session.commit() return json_response() else: return json_error_response("地址更改操作错误(2)")
def cat_set(): if request.method == "GET": id = get_int(request.args, "id", 0) if id > 0: cat_info = FoodCat.query.filter_by(id=id).first() if cat_info is None: return make_response(redirect(build_url("/food/cat"))) else: cat_info = None return render_template_with_global_vars("food/cat_set.html", context={"cat": cat_info}) elif request.method == "POST": id = get_int(request.form, "id", 0) if id > 0: cat_info = FoodCat.query.filter_by(id=id).first() if cat_info is None: return json_error_response("无效的食品类别编辑操作") else: cat_info = None name = request.form.get("name", "") weight = int(request.form.get("weight", "0")) if len(name) < 1: return json_error_response("食品类别名称不能为空!") if weight < 1 or weight > 4: return json_error_response("食品类别的权重必须在1-4之间!(含1和4)") if cat_info is None: cat_info = FoodCat() cat_info.created_time = get_current_time() cat_info.status = 1 cat_info.name = name cat_info.weight = weight cat_info.updated_time = get_current_time() db.session.add(cat_info) db.session.commit() return json_response("修改食品类别信息成功!")
def info(): id = get_int(request.args, "id", 0) redir_response = make_response(redirect(build_url("/food/index"))) if id == 0: return redir_response food_info = Food.query.filter_by(id=id).first() if food_info is None: return redir_response stock_change_query = FoodStockChangeLog.query.filter_by(food_id=id) stock_change_query = stock_change_query.order_by( FoodStockChangeLog.created_time.desc()) stock_change_list = stock_change_query.all() ctx = {"food": food_info, "stock_change_list": stock_change_list} return render_template_with_global_vars("food/info.html", context=ctx)
def order_create(): member_id = g.current_member.id order_type = request.form.get("type", None) order_list = request.form.get("purchaseList", None) address_id = get_int(request.form, "address_id", 0) deliver_price = Decimal(request.form.get("deliver_price", "0.00")) note = request.form.get("note", "") if order_type is None: return json_error_response("未注明订单类别,无法提交订单") if order_list is None: return json_error_response("订单内容不能为空,下单失败") if address_id is None: return json_error_response("未填写地址") order_list = json.loads(order_list) if len(order_list) < 1: return json_error_response("订单内容不能为空,下单失败") address_info = MemberAddress.query.filter_by(id=address_id).first() if not address_info or address_info.status != 1: return json_error_response("未填写地址,或者地址信息有误") params = { "shipping_price": deliver_price, "note": note, "deliver_address_id": address_info.id, "deliver_info": { "name": address_info.contact_name, "mobile": address_info.mobile, "address": "%s%s%s%s" % (address_info.province_str, address_info.city_str, address_info.district_str, address_info.address) } } res = pay_utils.create_order(member_id, order_list, params=params) app.logger.debug("type of res: %s" % type(res)) app.logger.debug("res error: %s" % str(res)) if res["code"] == 200 and order_type == "cart": food_id_list = [item["food_id"] for item in order_list] delete_cart_info(member_id, food_id_list) return make_response(jsonify(res))
def comment(): id = get_int(request.args, "id", 0) if id == 0: return json_error_response("无法获取评价信息(1)") comment_member_list = db.session.query(MemberComment, Member)\ .filter(MemberComment.member_id == Member.id, MemberComment.food_ids.ilike("%_{0}_%".format(id))).all() res_list = [{ "date": str(comment.created_time), "user_avatar_url": member.avatar, "content": comment.content, "score": comment.score_desc } for comment, member in comment_member_list] return json_response(data={"list":res_list})
def index(): current_page = get_int(request.args, "p", 1) values = request.values items_per_page = app.config["FOOD_INDEX_ITEMS_PER_PAGE"] food_info_query = Food.query.order_by(Food.status.desc(), Food.id.desc()) # filtering by search if "mix_kw" in values and len(values["mix_kw"]) > 0: rule = Food.name.ilike("%%%s%%" % values["mix_kw"]) food_info_query = food_info_query.filter(rule) if "status" in values and values["status"] != "-1": food_info_query = food_info_query.filter_by( status=int(values["status"])) if "cat_id" in values and values["cat_id"] != "0": food_info_query = food_info_query.filter_by( cat_id=int(values["cat_id"])) # pagination offset = (current_page - 1) * items_per_page pagination_dict = pagination(num_items=food_info_query.count(), items_per_page=items_per_page, current_page=current_page, url=build_url("/food/index?")) food_info_list = food_info_query.offset(offset).limit(items_per_page).all() food_cat_list = FoodCat.query.all() food_cat_dict = get_id_to_model_dict(FoodCat, "id", "id") page_params = { "food_info_list": food_info_list, "food_cat_list": food_cat_list, "food_cat_dict": food_cat_dict, "pagination": pagination_dict, "search": { "mix_kw": values.get("mix_kw", None), "status": values.get("status", "-1"), "cat_id": values.get("cat_id", "0") }, "status_mapping": app.config["ACCOUNT_STATUS_MAPPING"] } return render_template_with_global_vars("food/index.html", context=page_params)
def my_address_get(): address_id = utils.get_int(request.values, "id", 0) address_info = MemberAddress.query.filter_by(id=address_id).first() if not address_info: return json_error_response("无法获取地址信息") prov_idx, city_idx, distr_idx = \ utils.get_addr_idxs(address_info.province_id, address_info.city_id, address_info.district_id) data = { "contact_name": address_info.contact_name, "mobile": address_info.mobile, "province_name": address_info.province_str, "city_name": address_info.city_str, "district_name": address_info.district_str, "province_idx": prov_idx, "city_idx": city_idx, "distr_idx": distr_idx, "address": address_info.address } return json_response(data=data)
def my_comment_add(): member_id = g.current_member.id order_sn = request.form.get("order_sn", None) if not order_sn: return json_error_response("评价操作失败(1)") pay_order_info = PayOrder.query.filter_by(member_id=member_id, order_sn=order_sn).first() if not pay_order_info: return json_error_response("评价操作失败(2)") if pay_order_info.comment_status: return json_error_response("已经评价过了,请勿重复评价") pay_order_items = PayOrderItem.query.filter_by(pay_order_id=pay_order_info.id).all() food_ids = "_".join(str(item.food_id) for item in pay_order_items) score = utils.get_int(request.form, "score", 10) content = request.form.get("content", "") comment_info = MemberComment() comment_info.member_id = member_id comment_info.food_ids = "_" + food_ids + "_" comment_info.pay_order_id = pay_order_info.id comment_info.score = score comment_info.content = content comment_info.created_time = utils.get_current_time() db.session.add(comment_info) db.session.commit() pay_order_info.comment_status = 1 pay_order_info.updated_time = utils.get_current_time() db.session.add(pay_order_info) db.session.commit() return json_response()
def set(): if request.method == "GET": cat_list = FoodCat.query.all() id = get_int(request.args, "id", 0) food_info = Food.query.filter_by(id=id).first() if (id > 0 and food_info is None) or (food_info is not None and food_info.status != 1): return redirect(build_url("/food/index")) ctx = {"food": food_info, "cat_list": cat_list} return render_template_with_global_vars("food/set.html", context=ctx) elif request.method == "POST": id = get_int(request.form, "id", 0) cat_id = get_int(request.form, "cat_id", 0) name = request.form.get("name", "") price = request.form.get("price", "") title_pic = request.form.get("title_pic", "") summary = request.form.get("summary", "") stock = get_int(request.form, "stock", 0) tags = request.form.get("tags", "") # form content verification if cat_id == 0: return json_error_response("请选择类别") empty_items = [] for var, item in zip([name, price, title_pic, summary, tags], ["菜品名称", "菜品价格", "封面图", "描述", "标签"]): if len(var) < 1: empty_items.append(item) if len(empty_items) > 0: return json_error_response("以下内容不能为空:" + "、".join(empty_items)) price = Decimal(price).quantize(Decimal("0.00")) if price <= 0: return json_error_response("售卖价格不能小于或者等于0") # create new entry in Food table food_info = Food.query.filter_by(id=id).first() before_stock = 0 if food_info: before_stock = food_info.stock else: food_info = Food() food_info.status = 1 food_info.created_time = get_current_time() food_info.cat_id = cat_id food_info.name = name food_info.price = price food_info.main_image = title_pic food_info.summary = summary food_info.stock = stock food_info.tags = tags food_info.updated_time = get_current_time() db.session.add(food_info) db.session.commit() # add entry into food stock change log if not set_food_stock_change_log(food_info.id, int(before_stock), int(stock) - int(before_stock), "后台直接更改"): return json_error_response("登记库存变更信息出现错误") return json_response("成功添加菜品 %s" % name)
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)