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 index(): current_page = int(request.args.get("p", "1")) values = request.values users_per_page = app.config["MEMBER_INDEX_ITEMS_PER_PAGE"] user_info_query = User.query.order_by(User.status.desc(), User.uid.desc()) # filtering by search if "mix_kw" in values and len(values["mix_kw"]) > 0: app.logger.debug("mix_kw %s" % values["mix_kw"]) rule = or_(User.nickname.ilike("%%%s%%" % values["mix_kw"]), User.mobile.ilike("%%%s%%" % values["mix_kw"])) user_info_query = user_info_query.filter(rule) # pagination offset = (current_page - 1) * users_per_page pagination_dict = pagination(num_items=user_info_query.count(), items_per_page=users_per_page, current_page=current_page, url=build_url("/account/index?")) user_info_list = user_info_query[offset:offset + users_per_page] page_params = { "user_info_list": user_info_list, "pagination": pagination_dict, "search": { "mix_kw": values.get("mix_kw", None), "status": values.get("status", "-1") }, "status_mapping": app.config["ACCOUNT_STATUS_MAPPING"] } return render_template_with_global_vars("account/index.html", context=page_params)
def set(): if request.method == "GET": id = int(request.args.get("id", "0")) redir_response = make_response(redirect(build_url("/member/index"))) if id == 0: return redir_response member_info = Member.query.filter_by(id=id).first() if member_info is None: return redir_response return render_template_with_global_vars( "member/set.html", context={"member": member_info}) elif request.method == "POST": id = int(request.form.get("id", "0")) if id == 0: return json_error_response("该用户不存在,请确认用户id!") member_info = Member.query.filter_by(id=id).first() if member_info is None: return json_error_response("该用户不存在,请确认用户id!") new_nickname = request.form.get("nickname", "") if len(new_nickname) < 1: return json_error_response("会员名称不能为空!") member_info.nickname = new_nickname member_info.update_time = get_current_time() db.session.add(member_info) db.session.commit() return json_response("修改会员信息成功!")
def info(): id = int(request.args.get("id", "0")) redir_response = make_response(redirect(build_url("/member/index"))) if id == 0: return redir_response member_info = Member.query.filter_by(id=id).first() if member_info is None: return redir_response return render_template_with_global_vars("member/info.html", context={"member": member_info})
def user_auth(): # filter out urls that do not need to check cookie login info pattern = re.compile("|".join(app.config["IGNORE_COOKIE_AUTH_URLS"])) if pattern.match(request.path): return g.current_user = check_login() if g.current_user is not None: app.logger.info("[user_auth()] User is logged in by checking cookies") else: app.logger.info("[user_auth()] User is not logged in") return redirect(build_url("/user/login")) add_access_log(request, g)
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 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 index(): current_page = int(request.args.get("p", "1")) values = request.values items_per_page = app.config["ACCOUNT_INDEX_ITEMS_PER_PAGE"] member_info_query = Member.query.order_by(Member.status.desc(), Member.id.desc()) app.logger.debug("number of members: %d" % member_info_query.count()) # filtering by search if "mix_kw" in values and len(values["mix_kw"]) > 0: app.logger.debug("mix_kw %s" % values["mix_kw"]) rule = or_(Member.nickname.ilike("%%%s%%" % values["mix_kw"]), Member.mobile.ilike("%%%s%%" % values["mix_kw"])) member_info_query = member_info_query.filter(rule) if "status" in values: if values["status"] != "-1": member_info_query = member_info_query.filter_by( status=int(values["status"])) # pagination offset = (current_page - 1) * items_per_page pagination_dict = pagination(num_items=member_info_query.count(), items_per_page=items_per_page, current_page=current_page, url=build_url("/member/index?")) member_info_list = member_info_query.offset(offset).limit( items_per_page).all() page_params = { "member_info_list": member_info_list, "pagination": pagination_dict, "search": { "mix_kw": values.get("mix_kw", None), "status": values.get("status", "-1") }, "status_mapping": app.config["ACCOUNT_STATUS_MAPPING"] } return render_template_with_global_vars("member/index.html", context=page_params)
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(): uid = int(request.args["uid"]) if "uid" in request.args else None # redirect if uid is invalid or empty redir_response = make_response(redirect(build_url("/account/index"))) if uid is None: return redir_response user_info = User.query.filter_by(uid=uid).first() if user_info is None: return redir_response # access log access_log_num_entries = 30 access_log_query = AppAccessLog.query.order_by( AppAccessLog.created_time.desc()) access_log = access_log_query.filter_by( uid=uid).all()[:access_log_num_entries] return render_template_with_global_vars("account/info.html", context={ "user": user_info, "num_entries": len(access_log), "access_log": access_log })
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 logout(): g.current_user = None response = make_response(redirect(build_url("/user/login"))) response.delete_cookie(app.config["AUTH_COOKIE_NAME"]) return response
def order_pay(): member_info = g.current_member order_sn = request.form.get("order_sn", None) if order_sn is None: return json_error_response("支付失败,请稍后再试(1)") pay_order_info = PayOrder.query.filter_by(order_sn=order_sn).first() if pay_order_info is None: return json_error_response("支付失败,请稍后再试(2)") # get openid for member oauth_bind_info = OauthMemberBind.query.filter_by( member_id=member_info.id).first() if oauth_bind_info is None: return json_error_response("支付失败,请稍后再试(3)") subscribed = request.form.get("subscribed", "False") subscribed = subscribed == "true" notify_url = build_url("/api/order/callback") data = { "appid": app.config["MINA_APP_ID"], "mch_id": app.config["MCH_ID"], "nonce_str": wc_utils.get_nonce_str(), "body": "订餐", "out_trade_no": pay_order_info.order_sn, "total_fee": int(pay_order_info.total_price * 100), #单位为分 "notify_url": notify_url, "trade_type": "JSAPI", "openid": oauth_bind_info.openid } prepay_info = wc_utils.get_pay_info(data) # save prepay_id to database pay_order_info.prepay_id = prepay_info["prepay_id"] pay_order_info.subscribed = subscribed db.session.add(pay_order_info) db.session.commit() res_data = {"prepay_info": prepay_info} if app.config["DEV_MODE"]: # prepare callback xml message right here, send to frontend, and then # frontend will send it back to backend's /order/callback_dev res_data["dev_mode"] = True cb_dev_data = { "appid": data["appid"], "bank_type": "CFT", "cash_fee": data["total_fee"], "fee_type": "CNY", "is_subscribe": "N", "mch_id": app.config["MCH_ID"], "nonce_str": wc_utils.get_nonce_str(), "openid": oauth_bind_info.openid, "out_trade_no": pay_order_info.order_sn, "result_code": "SUCCESS", "return_code": "SUCCESS", "time_end": get_current_time("%Y%m%d%H%M%S"), "total_fee": data["total_fee"], "trade_type": "JSAPI", # this is supposed to be automatically generated by WeChat API # use our own order_sn temporarily for development purposes "transaction_id": pay_order_info.order_sn } sign = wc_utils.create_sign(cb_dev_data) cb_dev_data["sign"] = sign xml_data = wc_utils.dict_to_xml(cb_dev_data) res_data["cb_dev_data"] = xml_data return json_response(data=res_data)