def food_info(): form = IdForm().validate_for_api() id = form.id.data # req = request.values # id = int(req['id']) if 'id' in req else 0 food_info = Food.query.filter_by(id=id).first() if not food_info or not food_info.status: return NotFound(msg='美食已下架') member_info = g.user cart_number = 0 if member_info: cart_number = MemberCart.query.filter_by(member_id=member_info.uid).count() resp = {} resp['info'] = { "id": food_info.id, "name": food_info.name, "summary": food_info.summary, "total_count": food_info.total_count, "comment_count": food_info.comment_count, 'main_image': UrlManager.build_image_url(food_info.main_image), "price": str(food_info.price), "stock": food_info.stock, "pics": [UrlManager.build_image_url(food_info.main_image)] } resp['cart_number'] = cart_number return jsonify(resp)
def cart_index(): member_info = g.user if not member_info: return AuthFailed('请先登录') cart_list = MemberCart.query.filter_by(member_id=member_info.uid).all() data_cart_list = [] resp = {} if cart_list: food_ids = select_filter_obj(cart_list, "food_id") food_map = get_dict_filter_field(Food, Food.id, "id", food_ids) for item in cart_list: tmp_food_info = food_map[item.food_id] tmp_data = { "id": item.id, "number": item.quantity, "food_id": item.food_id, "name": tmp_food_info.name, "price": str(tmp_food_info.price), "pic_url": UrlManager.build_image_url(tmp_food_info.main_image), "active": True } data_cart_list.append(tmp_data) resp['data'] = data_cart_list return jsonify(resp)
def before_request(): ignore_urls = current_app.config['IGNORE_URLS'] ignore_check_login_urls = current_app.config['IGNORE_CHECK_LOGIN_URLS'] path = request.path # 如果是静态文件就不要查询用户信息了 pattern = re.compile('%s' % "|".join(ignore_check_login_urls)) if pattern.match(path): return if '/v1' in path: return user_info = check_login() g.current_user = None if user_info: g.current_user = user_info #加入日志 LogService.add_access_log() pattern = re.compile('%s' % "|".join(ignore_urls)) if pattern.match(path): return if not user_info: return redirect(UrlManager.build_url("/user/login")) return
def list_image(): resp = {'state': 'SUCCESS', 'list': [], 'start': 0, 'total': 0} req = request.values start = int(req['start']) if 'start' in req else 0 page_size = int(req['size']) if 'size' in req else 20 query = Image.query if start > 0: # 倒叙才这样处理 query = query.filter(Image.id < start) # i_list = query.order_by( # Image.id).offset(start).limit(page_size).all() # 正序用偏移量比较方便 数量 i_list = query.order_by(Image.id.desc()).limit(page_size).all() images = [] if i_list: for item in i_list: images.append({'url': UrlManager.build_image_url(item.file_key)}) start = item.id resp['list'] = images resp['start'] = start resp['total'] = len(images) return jsonify(resp)
def food_index(): resp = {} c_list = FoodCategory.query.filter_by(status=1).order_by(FoodCategory.weight.desc()).all() data_c_list = [] data_c_list.append({ 'id': 0, 'name': '全部' }) if c_list: for item in c_list: temp_data = { 'id': item.id, 'name': item.name } data_c_list.append(temp_data) resp['category_list'] = data_c_list food_list = Food.query.filter_by(status=1).order_by( Food.total_count.desc(), Food.id.desc()).limit(3).all() data_food_list = [] if food_list: for item in food_list: temp_data = { 'id': item.id, 'pic_url': UrlManager.build_image_url(item.main_image) } data_food_list.append(temp_data) resp['food_list'] = data_food_list return jsonify(resp)
def order_info(): req = request.get_json(silent=True) params_goods = req['goods'] if 'goods' in req else None member_info = g.user if not member_info: return AuthFailed('请先登录') params_goods_list = [] if params_goods: params_goods_list = json.loads(params_goods) food_dic = {} for item in params_goods_list: food_dic[item['id']] = item['number'] food_ids = food_dic.keys() food_list = Food.query.filter(Food.id.in_(food_ids)).all() data_food_list = [] yun_price = pay_price = decimal.Decimal(0.00) if food_list: for item in food_list: tmp_data = { "id": item.id, "name": item.name, "price": str(item.price), 'pic_url': UrlManager.build_image_url(item.main_image), 'number': food_dic[item.id] } pay_price = pay_price + item.price * int(food_dic[item.id]) data_food_list.append(tmp_data) address_info = MemberAddress.query.filter_by(is_default=1, member_id=member_info.uid, status=1).first() default_address = '' if address_info: default_address = { "id": address_info.id, "name": address_info.nickname, "mobile": address_info.mobile, "address": "%s%s%s%s" % (address_info.province_str, address_info.city_str, address_info.area_str, address_info.address) } resp = {} resp['food_list'] = data_food_list resp['pay_price'] = str(pay_price) resp['yun_price'] = str(yun_price) resp['total_price'] = str(pay_price + yun_price) resp['default_address'] = default_address return jsonify(resp)
def upload_image(): resp = {'state': 'SUCCESS', 'url': '', 'title': '', 'original': ''} file_target = request.files upfile = file_target['upfile'] if 'upfile' in file_target else None if upfile is None: resp['state'] = '上传失败' return jsonify(resp) ret = UploadService.upload_by_file(upfile) if ret['code'] != 200: resp['state'] = '上传失败:' + ret['msg'] return jsonify(resp) resp['url'] = UrlManager.build_image_url(ret['data']['file_key']) return jsonify(resp)
def info(): resp_data = {} req = request.args # 参数少用args # reqv = request.values # 参数多用values uid = int(req.get('id', 0)) reback_url = UrlManager.build_url("/account/index") if uid < 1: return redirect(reback_url) info = User.query.filter_by(uid=uid).first() if not info: return redirect(reback_url) access_list = AppAccessLog.query.filter_by(uid=uid).order_by( AppAccessLog.id.desc()).limit(10).all() resp_data['info'] = info resp_data['access_list'] = access_list return render_template("account/info.html", **resp_data)
def info(): resp_data = {} req = request.args f_id = int(req.get("id", 0)) back_url = UrlManager.build_url("/food/index") if f_id < 1: return redirect(back_url) f_info = Food.query.filter_by(id=f_id).first() if not info: return redirect(back_url) stock_change_list = FoodStockChangeLog.query.filter(FoodStockChangeLog.food_id == f_id)\ .order_by(FoodStockChangeLog.id.desc()).all() resp_data['info'] = f_info resp_data['stock_change_list'] = stock_change_list resp_data['current'] = 'index' return render_template("food/info.html", **resp_data)
def pay_info(): resp_data = {} req = request.values id = int(req['id']) if 'id' in req else 0 back_url = UrlManager.build_url("/finance/index") if id < 1: return redirect(back_url) pay_order_info = PayOrder.query.filter_by( id = id ).first() if not pay_order_info: return redirect(back_url) member_info = Member.query.filter_by(id=pay_order_info.member_id).first() if not member_info: return redirect(back_url) order_item_list = PayOrderItem.query.filter_by(pay_order_id=pay_order_info.id).all() data_order_item_list = [] if order_item_list: food_map = get_dict_filter_field(Food, Food.id, "id", select_filter_obj(order_item_list, "food_id")) for item in order_item_list: tmp_food_info = food_map[item.food_id] tmp_data = { "quantity": item.quantity, "price": item.price, "name": tmp_food_info.name } data_order_item_list.append(tmp_data) address_info = {} if pay_order_info.express_info: address_info = json.loads(pay_order_info.express_info) resp_data['pay_order_info'] = pay_order_info resp_data['pay_order_items'] = data_order_item_list resp_data['member_info'] = member_info resp_data['address_info'] = address_info resp_data['current'] = 'index' return render_template("finance/pay_info.html", **resp_data)
def info(): resp_data = {} req = request.args m_id = int(req.get("id", 0)) back_url = UrlManager.build_url("/member/index") if m_id < 1: return redirect(back_url) member_info = Member.query.filter_by(id=m_id).first() if not member_info: return redirect(back_url) # pay_order_list = PayOrder.query.filter_by( member_id = m_id ).filter( PayOrder.status.in_( [-8,1] ) )\ # .order_by( PayOrder.id.desc() ).all() # comment_list = MemberComments.query.filter_by( member_id = m_id ).order_by( MemberComments.id.desc() ).all() resp_data['info'] = MemberViewModel(member_info) # resp_data['pay_order_list'] = pay_order_list # resp_data['comment_list'] = comment_list resp_data['current'] = 'index' return render_template("member/info.html", **resp_data)
def my_order_info(): form = PayForm().validate_for_api() member_info = g.user pay_order_info = PayOrder.query.filter_by( member_id=member_info.uid, order_sn=form.order_sn.data).first_or_404() express_info = {} if pay_order_info.express_info: express_info = json.loads(pay_order_info.express_info) tmp_deadline = pay_order_info.created_time + datetime.timedelta(minutes=30) info = { "order_sn": pay_order_info.order_sn, "status": pay_order_info.pay_status, "status_desc": pay_order_info.status_desc, "pay_price": str(pay_order_info.pay_price), "yun_price": str(pay_order_info.yun_price), "total_price": str(pay_order_info.total_price), "address": express_info, "goods": [], "deadline": tmp_deadline.strftime("%Y-%m-%d %H:%M") } pay_order_items = PayOrderItem.query.filter_by( pay_order_id=pay_order_info.id).all() if pay_order_items: food_ids = select_filter_obj(pay_order_items, "food_id") food_map = get_dict_filter_field(Food, Food.id, "id", food_ids) for item in pay_order_items: tmp_food_info = food_map[item.food_id] tmp_data = { "name": tmp_food_info.name, "price": str(item.price), "unit": item.quantity, "pic_url": UrlManager.build_image_url(tmp_food_info.main_image), } info['goods'].append(tmp_data) return jsonify(info)
def food_search(): resp = {} req = request.values category_id = int(req['category_id']) if 'category_id' in req else 0 search_key = str(req['search_key']) if 'search_key' in req else '' p = int(req['p']) if 'p' in req else 1 if p < 1: p = 1 page_size = 10 offset = (p - 1) * page_size query = Food.query.filter_by(status=1) if category_id > 0: query = query.filter_by(cat_id=category_id) if search_key: query = query.filter(or_(Food.name.ilike( "%{0}%".format(search_key)), Food.tags.ilike("%{0}%".format(search_key)))) food_list = query.order_by(Food.total_count.desc(), Food.id.desc())\ .offset(offset).limit(page_size).all() data_food_list = [] if food_list: for item in food_list: tmp_data = { 'id': item.id, 'name': "%s"%(item.name), 'price': str(item.price), 'min_price': str(item.price), 'pic_url': UrlManager.build_image_url(item.main_image) } data_food_list.append(tmp_data) resp['foods'] = data_food_list resp['has_more'] = 0 if len(data_food_list) < page_size else 1 return jsonify(resp)
def set(): if request.method == "GET": resp_data = {} req = request.args m_id = int(req.get("id", 0)) back_url = UrlManager.build_url("/member/index") if m_id < 1: return redirect(back_url) member_info = Member.query.filter_by(id=m_id).first() if not member_info: return redirect(back_url) if member_info.status != 1: return redirect(back_url) resp_data['info'] = member_info resp_data['current'] = 'index' return render_template("member/set.html", **resp_data) req = request.values m_id = req['id'] if 'id' in req else 0 nickname = req['nickname'] if 'nickname' in req else '' if nickname is None or len(nickname) < 1: return AjaxFail('请输入符合规范的姓名') member_info = Member.query.filter_by(id=m_id).first() if not member_info: return AjaxFail('指定会员不存在') member_info.nickname = nickname member_info.updated_time = get_current_date() with db.auto_commit(): member_info return AjaxSuccess()
def logout(): response = make_response(redirect(UrlManager.build_url("/user/login"))) logout_user() # response.delete_cookie(app.config['AUTH_COOKIE_NAME']) return response
def my_order_list(): member_info = g.user form = MyOrderForm().validate_for_api() status = form.status.data query = PayOrder.query.filter_by(member_id=member_info.uid) if status == OrderStatusEnum.WAIT: # 等待付款 query = query.filter(PayOrder.status == -8) elif status == OrderStatusEnum.SEND: # 待发货 query = query.filter(PayOrder.status == 1, PayOrder.express_status == -7, PayOrder.comment_status == 0) elif status == OrderStatusEnum.CONFIRM: # 待确认 query = query.filter(PayOrder.status == 1, PayOrder.express_status == -6, PayOrder.comment_status == 0) elif status == OrderStatusEnum.SAY: # 待评价 query = query.filter(PayOrder.status == 1, PayOrder.express_status == 1, PayOrder.comment_status == 0) elif status == OrderStatusEnum.OK: # 已完成 query = query.filter(PayOrder.status == 1, PayOrder.express_status == 1, PayOrder.comment_status == 1) else: query = query.filter(PayOrder.status == 0) pay_order_list = query.order_by(PayOrder.id.desc()).all() data_pay_order_list = [] if pay_order_list: pay_order_ids = select_filter_obj(pay_order_list, "id") pay_order_item_list = PayOrderItem.query.filter( PayOrderItem.pay_order_id.in_(pay_order_ids)).all() food_ids = select_filter_obj(pay_order_item_list, "food_id") food_map = get_dict_filter_field(Food, Food.id, "id", food_ids) pay_order_item_map = {} if pay_order_item_list: for item in pay_order_item_list: if item.pay_order_id not in pay_order_item_map: pay_order_item_map[item.pay_order_id] = [] tmp_food_info = food_map[item.food_id] pay_order_item_map[item.pay_order_id].append({ 'id': item.id, 'food_id': item.food_id, 'quantity': item.quantity, 'price': str(item.price), 'pic_url': UrlManager.build_image_url(tmp_food_info.main_image), 'name': tmp_food_info.name }) for item in pay_order_list: tmp_data = { 'status': item.pay_status, 'status_desc': item.status_desc, 'date': item.created_time.strftime("%Y-%m-%d %H:%M:%S"), 'order_number': item.order_number, 'order_sn': item.order_sn, 'note': item.note, 'total_price': str(item.total_price), 'goods_list': pay_order_item_map[item.id] } data_pay_order_list.append(tmp_data) resp = {} resp['pay_order_list'] = data_pay_order_list return jsonify(resp)
def set(): if request.method == 'GET': resp_data = {} req = request.args id = int(req.get('id', 0)) info = Food.query.filter_by(id=id).first() if info and info.status != 1: return redirect(UrlManager.build_url('/food/index')) c_list = FoodCategory.query.all() resp_data['info'] = info resp_data['c_list'] = c_list resp_data['current'] = 'index' return render_template("food/set.html", **resp_data) req = request.values id = int(req['id']) if 'id' in req and req['id'] else 0 cat_id = int(req['cat_id']) if 'cat_id' in req else 0 name = req['name'] if 'name' in req else '' price = req['price'] if 'price' in req else '' main_image = req['main_image'] if 'main_image' in req else '' summary = req['summary'] if 'summary' in req else '' stock = int(req['stock']) if 'stock' in req else '' tags = req['tags'] if 'tags' in req else '' if cat_id < 1: return AjaxFail('请选择分类') if name is None or len(name) < 1: return AjaxFail('请输入符合规范的名称') if not price or len(price) < 1: return AjaxFail('请输入符合规范的售卖价格') price = Decimal(price).quantize(Decimal('0.00')) if price <= 0: return AjaxFail('请输入符合规范的售卖价格') if main_image is None or len(main_image) < 3: return AjaxFail('请上传封面图') if summary is None or len(summary) < 3: return AjaxFail('请输入图书描述,并不能少于10个字符') if stock < 1: return AjaxFail('请输入符合规范的库存量') if tags is None or len(tags) < 1: return AjaxFail('请输入标签,便于搜索') food_info = Food.query.filter_by(id=id).first() before_stock = 0 if food_info: model_food = food_info before_stock = model_food.stock else: model_food = Food() model_food.status = 1 model_food.created_time = get_current_date() model_food.cat_id = cat_id model_food.name = name model_food.price = price model_food.main_image = main_image model_food.summary = summary model_food.stock = stock model_food.tags = tags model_food.updated_time = get_current_date() with db.auto_commit(): db.session.add(model_food) PayService.set_stock_change_log(model_food.id, int(stock) - int(before_stock), "后台修改") return AjaxSuccess()