Ejemplo n.º 1
0
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.buildUrl('/food/index'))

        cat_list = FoodCat.query.all()
        resp_data['info'] = info
        resp_data['current'] = 'set'
        resp_data['cat_list'] = cat_list
        return ops_render("food/set.html", resp_data)
    elif request.method == 'POST':
        resp = {'code': 200, 'msg': '操作成功', '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 ''
        price = Decimal(price).quantize(Decimal('0.00'))
        if cat_id < 1:
            resp['code'] = -1
            resp['msg'] = '请选择分类'
            return jsonify(resp)
        if name is None or len(name) < 1:
            resp['code'] = -1
            resp['msg'] = '请输入符合会犯的名称'
            return jsonify(resp)

        if price <= 0:
            resp['code'] = -1
            resp['msg'] = '请输入符合规范的售卖价格'
            return jsonify(resp)

        if main_image is None or len(main_image) < 3:
            resp['code'] = -1
            resp['msg'] = '请上传封面图'
            return jsonify(resp)

        if summary is None or len(summary) < 3:
            resp['code'] = -1
            resp['msg'] = "请输入图书描述,并不能少于10个字符"
            return jsonify(resp)

        if stock < 1:
            resp['code'] = -1
            resp['msg'] = '请输入符合规范的库存量'
            return jsonify(resp)

        if tags is None or len(tags) < 1:
            resp['code'] = -1
            resp['msg'] = '请输入标签,便于搜索'
            return jsonify(resp)

        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 = getCurrentDate()

        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 = getCurrentDate()
        db.session.add(model_food)
        ret = db.session.commit()

        FoodService.setStockChangeLog(model_food.id,
                                      int(stock) - int(before_stock), "后台修改")

        # model_stock_change = FoodStockChangeLog()
        # model_stock_change.food_id = model_food.id
        # # 当前的库存减去未修改前的库存,减少的话就是负数,增加就是正数
        # model_stock_change.unit = int(stock) - int(before_stock)
        # model_stock_change.total_stock = stock
        # model_stock_change.note = ''
        # model_stock_change.created_time = getCurrentDate()
        # db.session.add(model_stock_change)
        # db.session.commit()
        return jsonify(resp)
Ejemplo n.º 2
0
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"))

        cat_list = FoodCat.query.all()
        resp_data['info'] = info
        resp_data['cat_list'] = cat_list
        resp_data['current'] = 'index'
        return ops_render("food/set.html", resp_data)

    resp = {'code': 200, 'msg': '操作成功~~', '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:
        resp['code'] = -1
        resp['msg'] = "全部分类~~"
        return jsonify(resp)

    if name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的名称~~"
        return jsonify(resp)

    if not price or len(price) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(resp)

    price = Decimal(price).quantize(Decimal('0.00'))
    if price <= 0:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(resp)

    if main_image is None or len(main_image) < 3:
        resp['code'] = -1
        resp['msg'] = "请上传封面图~~"
        return jsonify(resp)

    if summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = "请输入图书描述,并不能少于10个字符~~"
        return jsonify(resp)

    if stock < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的库存量~~"
        return jsonify(resp)

    if tags is None or len(tags) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入标签,便于搜索~~"
        return jsonify(resp)

    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()

    db.session.add(model_food)
    db.session.commit()

    FoodService.setStockChangeLog(model_food.id, int(stock) - int(before_stock), "后台修改")
    return jsonify(resp)
Ejemplo n.º 3
0
def set():
    if request.method == 'GET':
        resp_data = {}
        req = request.args
        ids = req.get('id', '')
        id = int(ids) if ids.isdigit() else 0
        food_info = Food.query.filter_by(id=id).first()
        if food_info and food_info.status != 1:
            return redirect(UrlManager.buildUrl('/food/index'))

        cat_list = FoodCat.query.all()
        resp_data['current'] = 'index'
        resp_data['cat_list'] = cat_list
        resp_data['food_info'] = food_info
        return ops_render('food/set.html', resp_data)

    resp = {'code': 200, 'msg': '操作成功', 'data': {}}
    req = request.values
    id = int(req['id']) if 'id' in req and req['id'].isdigit() else 0
    cat_id = int(
        req['cat_id']) if 'cat_id' in req and req['cat_id'].isdigit() else 0
    name = req['name'] if 'name' in req else ''
    price = req['price'] if 'price' in req else 0
    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 and req['stock'].isdigit() else 0
    tags = req['tags'] if 'tags' in req else ''

    price = Decimal(price).quantize(Decimal('0.00'))

    if cat_id < 1:
        resp['code'] = -1
        resp['msg'] = '请选择分类'
        return jsonify(resp)

    if not name or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的名字'
        return jsonify(resp)

    if price <= 0:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的售卖价格'
        return jsonify(resp)

    if not main_image or len(main_image) < 3:
        resp['code'] = -1
        resp['msg'] = '请上传封面图'
        return jsonify(resp)

    if not summary or len(summary) < 10:
        resp['code'] = -1
        resp['msg'] = '请输入描述,并且不能少于10个字符'
        return jsonify(resp)

    if stock < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的库存量'
        return jsonify(resp)

    if not tags or len(tags) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入标签,便于搜索'
        return jsonify(resp)

    # has_food_name = Food.query.filter(Food.name==name).first()
    # if has_food_name:
    #     resp['code'] = -1
    #     resp['msg'] = '菜名已存在,请重新输入'
    #     return jsonify(resp)

    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.created_time = getCurrentDate()

    model_food.cat_id = cat_id
    model_food.name = name
    model_food.main_image = main_image
    model_food.stock = stock
    model_food.price = price
    model_food.summary = summary
    model_food.tags = tags
    model_food.updated_time = getCurrentDate()

    db.session.add(model_food)
    db.session.commit()

    # model_stock_change = FoodStockChangeLog()
    # model_stock_change.food_id = model_food.id
    # model_stock_change.unit = model_food.stock - before_stock
    # model_stock_change.total_stock = stock
    # model_stock_change.note = ''
    # model_stock_change.created_time = getCurrentDate()
    #
    # db.session.add(model_stock_change)
    # db.session.commit()

    FoodService.setFoodStockChangeLog(model_food.id,
                                      int(stock) - int(before_stock), '后台修改')

    return jsonify(resp)
Ejemplo n.º 4
0
def foot_set():
    # 修改食物库存等信息
    if request.method == "GET":
        resp_data = {}
        input_id = request.args.get('id', 0, type=int)
        target_info = Food.query.filter_by(id=input_id).first()
        if target_info and target_info.status != 1:
            return redirect(UrlManager.buildUrl("/food/index"))

        cat_list = FoodCat.query.all()
        resp_data['info'] = target_info
        resp_data['cat_list'] = cat_list
        resp_data['current'] = 'index'
        return ops_render("food/set.html", **resp_data)

    resp = {'code': 200, 'msg': '操作成功~~', 'data': {}}
    req = request.values
    input_id = ParamsValidator.GetORSetValue(req, 'id', value=0)
    cat_id = ParamsValidator.GetORSetValue(req, 'cat_id', value=0)
    name = ParamsValidator.GetORSetValue(req, 'name', value="")
    price = ParamsValidator.GetORSetValue(req, 'price', value="")
    main_image = ParamsValidator.GetORSetValue(req, 'main_image', value="")
    summary = ParamsValidator.GetORSetValue(req, 'summary', value="")
    stock = ParamsValidator.GetORSetValue(req, 'stock', value=0)
    tags = ParamsValidator.GetORSetValue(req, 'tags', value="")

    converted_price = 0.00
    try:
        converted_price = Decimal(price).quantize(Decimal('0.00'))
    except Exception:
        pass
    if cat_id < 1:
        resp['code'] = -1
        resp['msg'] = "请选择分类~~"
    elif name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的名称"
    elif not price or len(price) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格"
    elif converted_price <= 0:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格"
    elif main_image is None or len(main_image) < 3:
        resp['code'] = -1
        resp['msg'] = "请上传封面图"
    elif summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = "请输入食物描述,并不能少于10个字符"
    elif stock < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的库存量"
    elif tags is None or len(tags) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入标签,便于搜索"
    else:
        food_info = Food.query.filter_by(id=input_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 = getCurrentDate()
        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 = getCurrentDate()

        db.session.add(model_food)
        db.session.commit()

        FoodService.setStockChangeLog(model_food.id,
                                      int(stock) - int(before_stock), "后台修改")

    return jsonify(resp)
Ejemplo n.º 5
0
def set():
    if request.method == 'GET':
        resp_data = {}
        req = request.args
        cat_list = FoodCat.query.all()
        id = int(req.get('id', 0))
        info = None
        if id:
            info = Food.query.filter_by(id=id).first()

        if info and info.status != 1:
            return redirect(UrlManager('/food/index'))

        resp_data['cat_list'] = cat_list
        resp_data['info'] = info
        resp_data['current'] = 'index'
        return ops_render("food/set.html", resp_data)

    req = request.values
    resp = {'code': 200, 'msg': '操作成功', 'data': {}}

    id = int(req['id']) if 'id' in req else ''
    cat_id = int(req['cat_id']) if 'cat_id' in req else ''
    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 ''

    price = Decimal(price).quantize(Decimal('0.00'))

    if cat_id < 1:
        resp['code'] = -1
        resp['msg'] = '请选择分类~~'
        return jsonify(resp)

    if name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的名称~~'
        return jsonify(resp)

    if price <= 0:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范售卖价格~~'
        return jsonify(resp)

    if main_image is None or len(main_image) < 3:
        resp['code'] = -1
        resp['msg'] = '请上传封面图片~~'
        return jsonify(resp)

    if summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = '请输入图文描述,并且不能少于10个字符~~'
        return jsonify(resp)

    if stock < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范库存量~~'
        return jsonify(resp)

    if tags is None or len(tags) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入标签,便于搜索~~'
        return jsonify(resp)

    # 美食信息
    food_info = Food.query.filter_by(id=id).first()
    app.logger.info(food_info)
    # 变更之前的库存
    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 = getCurrentDate()

    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 = getCurrentDate()
    db.session.add(model_food)
    db.session.commit()

    model_stock_change = FoodStockChangeLog()
    model_stock_change.food_id = model_food.id
    model_stock_change.unit = int(stock) - int(before_stock)
    model_stock_change.total_stock = stock
    model_stock_change.note = ''
    model_stock_change.created_time = getCurrentDate()
    db.session.add(model_stock_change)
    db.session.commit()

    return jsonify(resp)
Ejemplo n.º 6
0
def food_set():
    """编辑、添加美食"""
    if request.method == 'GET':
        res = {}
        req_data = request.args
        fid = int(req_data.get('id', 0))
        food_info = Food.query.filter_by(id=fid).first()
        if food_info and food_info.status != 1:
            return redirect(UrlManager.buildUrl('/food/index'))
        cat_list = FoodCat.query.all()
        res['info'] = food_info
        res['cat_list'] = cat_list
        res['current'] = 'index'
        return ops_render("food/set.html", res)

    res = {'code': 200, 'msg': '操作成功', 'data': {}}
    req_data = request.values
    fid = int(req_data['id']) if 'id' in req_data and req_data['id'] else 0
    cat_id = int(req_data['cat_id']) if 'cat_id' in req_data else 0
    name = req_data['name'] if 'name' in req_data else ''
    price = req_data['price'] if 'price' in req_data else ''
    main_image = req_data['main_image'] if 'main_image' in req_data else ''
    summary = req_data['summary'] if 'summary' in req_data else ''
    stock = int(req_data['stock']) if 'stock' in req_data else ''
    tags = req_data['tags'] if 'tags' in req_data else ''

    if cat_id < 1:
        res['code'] = -1
        res['msg'] = "请选择分类~~"
        return jsonify(res)

    if name is None or len(name) < 1:
        res['code'] = -1
        res['msg'] = "请输入符合规范的名称~~"
        return jsonify(res)

    if not price or len(price) < 1:
        res['code'] = -1
        res['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(res)

    price = Decimal(price).quantize(Decimal('0.00'))
    if price <= 0:
        res['code'] = -1
        res['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(res)

    if main_image is None or len(main_image) < 3:
        res['code'] = -1
        res['msg'] = "请上传封面图~~"
        return jsonify(res)

    if summary is None or len(summary) < 3:
        res['code'] = -1
        res['msg'] = "请输入图书描述,并不能少于10个字符~~"
        return jsonify(res)

    if stock < 1:
        res['code'] = -1
        res['msg'] = "请输入符合规范的库存量~~"
        return jsonify(res)

    if tags is None or len(tags) < 1:
        res['code'] = -1
        res['msg'] = "请输入标签,便于搜索~~"
        return jsonify(res)

    # 数据库操作
    food_info = Food.query.filter_by(id=fid).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_time()

    model_food.cat_id = cat_id
    model_food.name = name
    model_food.price = price
    model_food.summary = summary
    model_food.stock = stock
    model_food.main_image = main_image
    model_food.updated_time = get_current_time()
    model_food.tags = tags

    db.session.add(model_food)
    db.session.commit()

    FoodService.set_stock_change_log(model_food.id,
                                     int(stock) - int(before_stock), '后台修改')
    return jsonify(res)
Ejemplo n.º 7
0
def set():
    if request.method == "GET":
        resp_data = {}
        req = request.args  # 参数多时用values ,参数少时用args
        id = req['id'] if 'id' in req else 0
        info = Food.query.filter_by(id=id).first()
        if info and info.status !=1:
            return redirect(UrlManager.buildUrl("/food/index"))

        cat_list = FoodCat.query.filter_by(status=1).all()
        resp_data['info'] = info
        resp_data['cat_list'] = cat_list
        resp_data['current'] = 'index'
        return ops_render("peixun/set.html", resp_data)

    # 下面是POST处理
    resp = {'code': 200, 'msg': '操作成功', 'data': ''}
    req = request.values                                    # 参数多时用values ,参数少时用args
    id = req['id'] if 'id' in req else 0                    # 获取当前用户id
    cat_id = req['cat_id'] if 'cat_id' in req else ''
    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 = req['stock'] if 'stock' in req else ''
    tags = req['tags'] if 'tags' in req else ''

    # price = Decimal(price).quantize(Decimal('0.00'))    # 转换价格格式

    if len(cat_id) < 1:
        resp['code'] = -1
        resp['msg'] = "请选择分类"
        return jsonify(resp)
    if name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的名称"
        return jsonify(resp)

    if summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的描述"
        return jsonify(resp)


    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 = getCurrentDate()

    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 = getCurrentDate()

    db.session.add(model_food)
    ret = db.session.commit()

    # 处理库存变化
    # model_stock_change = FoodStockChangeLog()
    # model_stock_change.food_id = model_food.id
    # model_stock_change.unit = int(stock)-int(before_stock)
    # model_stock_change.total_stock = stock
    # model_stock_change.note = ''
    # model_stock_change.created_time = getCurrentDate()
    # db.session.add(model_stock_change)
    # db.session.commit()
    return jsonify(resp)
Ejemplo n.º 8
0
def set():
    if request.method == 'GET':
        resp_data = {}
        #传到前台信息展示
        req = request.values
        id = int(req.get('id', 0))
        info = Food.query.filter_by(id=id).first()
        if info and info.status != 1:
            return redirect(UrlManager.buildUrl('/food/index'))

        cat_list = FoodCat.query.all()
        resp_data['current'] = 'index'
        resp_data['list'] = cat_list
        resp_data['info'] = info
        return ops_render("food/set.html", resp_data)

    resp = {'code': 200, 'msg': '操作成功!!!', '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 ''

    price = decimal.Decimal(price).quantize(decimal.Decimal('0.00'))

    if cat_id < 1:
        resp['code'] = -1
        resp['mag'] = '请输入符合规范的分类名!!!'
        return jsonify(resp)

    if name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的名称!!!'
        return jsonify(resp)
    if price <= 0:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的价格!!!'
        return jsonify(resp)
    if main_image is None or len(main_image) < 3:
        resp['code'] = -1
        resp['msg'] = '请上传封面图!!!'
        return jsonify(resp)

    if summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = '请输入描述,不不能少于10个字符!!!'
        return jsonify(resp)
    if stock < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的库存量!!!'
        return jsonify(resp)

    if tags is None or len(tags) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入标签,便于搜索!!!'
        return jsonify(resp)

    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 = getCurrentDate()
    model_food.cat_id = cat_id
    model_food.name = name
    model_food.price = price
    model_food.stock = stock
    model_food.main_image = main_image
    model_food.summary = summary
    model_food.tags = tags
    model_food.updated_time = getCurrentDate()

    db.session.add(model_food)
    ret = db.session.commit()

    model_stock_change = FoodStockChangeLog()
    model_stock_change.food_id = model_food.id
    model_stock_change.unit = int(stock) - int(before_stock)
    model_stock_change.total_stock = stock
    model_stock_change.note = ''
    model_stock_change.created_time = getCurrentDate()

    db.session.add(model_stock_change)
    db.session.commit()
    return jsonify(resp)
Ejemplo n.º 9
0
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.buildUrl("/food/index"))

        cat_list = FoodCat.query.all()
        current_user = g.current_user
        communities = Community.query.filter(
            Community.platform_id == current_user.platform_id).filter_by(
                status=1)
        resp_data['info'] = info
        resp_data['cat_list'] = cat_list
        resp_data['current'] = 'index'
        resp_data['communities'] = communities
        resp_data['current_user'] = current_user
        return ops_render("food/set.html", resp_data)

    resp = {'code': 200, 'msg': '操作成功~~', '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
    platform_id = int(req['platform_id']) if 'platform_id' in req else 0
    platform_name = req['platform_name'] if 'platform_name' in req else ''
    communities = req['communities'] if 'communities' in req else ''
    date_from = req['date_from'] if 'date_from' in req else None
    date_to = req['date_to'] if 'date_to' in req else None
    name = req['name'] if 'name' in req else ''
    ratio = req['ratio'] if 'ratio' in req else ''
    market_price = req['market_price'] if 'market_price' 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 ''
    limit_quantity = int(
        req['limit_quantity']
    ) if 'limit_quantity' in req and req['limit_quantity'] else 0

    if cat_id < 1:
        resp['code'] = -1
        resp['msg'] = "请选择分类~~"
        return jsonify(resp)

    if date_from is None or len(date_from) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入开始日期~~"
        return jsonify(resp)

    if date_to is None or len(date_to) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入结束日期~~"
        return jsonify(resp)

    if name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的名称~~"
        return jsonify(resp)

    if not price or len(price) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(resp)

    if not market_price or len(market_price) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的市场价格~~"
        return jsonify(resp)

    if not ratio or len(ratio) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的提成比例~~"
        return jsonify(resp)

    market_price = Decimal(market_price).quantize(Decimal('0.00'))
    if market_price <= 0:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(resp)

    price = Decimal(price).quantize(Decimal('0.00'))
    if price <= 0:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(resp)

    ratio = Decimal(ratio).quantize(Decimal('0.00'))
    if ratio <= 0:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的提成比例~~"
        return jsonify(resp)

    if main_image is None or len(main_image) < 3:
        resp['code'] = -1
        resp['msg'] = "请上传封面图~~"
        return jsonify(resp)

    if summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = "请输入图书描述,并不能少于10个字符~~"
        return jsonify(resp)

    if stock < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的库存量~~"
        return jsonify(resp)

    if tags is None or len(tags) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入标签,便于搜索~~"
        return jsonify(resp)

    # print(platform_name)
    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 = getCurrentDate()
        model_food.platform_id = g.current_user.platform_id
        model_food.platform_name = g.current_user.platform_name

    model_food.cat_id = cat_id
    model_food.platform_id = platform_id
    model_food.platform_name = platform_name
    model_food.communities = communities
    model_food.date_from = date_from
    model_food.date_to = date_to
    model_food.name = name
    model_food.ratio = ratio
    model_food.market_price = market_price
    model_food.price = price
    model_food.main_image = main_image
    model_food.summary = summary
    model_food.stock = stock
    model_food.tags = tags
    model_food.limit_quantity = limit_quantity
    model_food.updated_time = getCurrentDate()

    db.session.add(model_food)
    ret = db.session.commit()

    FoodService.setStockChangeLog(model_food.id,
                                  int(stock) - int(before_stock), "后台修改")
    return jsonify(resp)
Ejemplo n.º 10
0
def set():
    if request.method == "GET":
        resp_data = {}
        req = request.args
        id = int(req.get('id', 0))  # 获取查询的美食信息 id
        info = Food.query.filter_by(id=id).first()  # 获取美食信息
        if info and info.status != 1:  # 如果存在info,但是它的状态 !=1,即被删除了,那么返回首页
            return redirect(UrlManager.buildUrl("/food/index"))

        cat_list = FoodCat.query.all()  # 查询所有分类
        resp_data['info'] = info
        resp_data['cat_list'] = cat_list
        resp_data['current'] = 'index'
        return ops_render("food/set.html", resp_data)

    resp = {'code': 200, 'msg': '操作成功~~', '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:
        resp['code'] = -1
        resp['msg'] = "请选择分类~~"
        return jsonify(resp)

    if name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的名称~~"
        return jsonify(resp)

    if not price or len(price) <= 0:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(resp)

    price = Decimal(price).quantize(Decimal('0.00'))  # 价格,换成0.00
    if price <= 0:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的售卖价格~~"
        return jsonify(resp)

    if main_image is None or len(main_image) < 3:
        resp['code'] = -1
        resp['msg'] = "请上传封面图~~"
        return jsonify(resp)

    if summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = "请输入图书描述,并不能少于10个字符~~"
        return jsonify(resp)

    if stock < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的库存量~~"
        return jsonify(resp)

    if tags is None or len(tags) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入标签,便于搜索~~"
        return jsonify(resp)

    food_info = Food.query.filter_by(id=id).first()
    before_stock = 0  # 改变之前的 库存
    if food_info:
        model_food = food_info
        before_stock = model_food.stock  # 修改时,当前库存量before_stock就是 数据表里面的
    else:
        model_food = Food()
        model_food.status = 1
        model_food.created_time = getCurrentData()

    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 = getCurrentData()

    db.session.add(model_food)
    ret = db.session.commit()

    FoodService.setStockChangeLog(model_food.id,
                                  int(stock) - int(before_stock),
                                  "后台修改")  # 商品数量变更记录。商品id,变更的数量,备注
    return jsonify(resp)