def catSet(): if request.method == 'GET': resp_data = {} req = request.args id = int(req.get("id", 0)) info = None if id: info = FoodCat.query.filter_by(id=id).first() resp_data['info'] = info resp_data['current'] = 'cat' return ops_render("food/cat_set.html", resp_data) elif request.method == 'POST': resp = {'code': 200, 'msg': '操作成功', 'data': {}} req = request.values id = req['id'] if 'id' in req else 0 name = req['name'] if 'name' in req else '' weight = int(req['weight']) if ('weight' in req and int(req['weight']) > 0) else 1 if name is None or len(name) < 1: resp['code'] = -1 resp['msg'] = '请输入符合规范的分类名称' return jsonify(resp) food_cat_info = FoodCat.query.filter_by(id=id).first() if food_cat_info: model_food_cat = food_cat_info else: model_food_cat = FoodCat() model_food_cat.created_time = getCurrentDate() model_food_cat.name = name model_food_cat.weight = weight model_food_cat.updated_time = getCurrentDate() db.session.add(model_food_cat) db.session.commit() return jsonify(resp)
def catSet(): if request.method == "GET": resp_data = {} req = request.args id = int(req.get("id", 0)) food_cat_info = None if id: food_cat_info = FoodCat.query.filter_by(id=id).first() resp_data["food_cat_info"] = food_cat_info resp_data["current"] = "cat" return ops_render("food/cat_set.html", resp_data) resp = {"code": 200, "msg": "操作成功~", "data": {}} req = request.values id = req["id"] if "id" in req else 0 name = req["name"] if "name" in req else "" weight = int(req["weight"]) if ("weight" in req and int(req["weight"]) > 0) else 1 if name is None or len(name) < 1: resp["code"] = -1 resp["msg"] = "请输入符合规范的分类名称~~" return jsonify(resp) food_cat_info = FoodCat.query.filter_by(id=id).first() if food_cat_info: model_food_cat = food_cat_info else: model_food_cat = FoodCat() model_food_cat.created_time = getCurrentDate() model_food_cat.name = name model_food_cat.weight = weight model_food_cat.updated_time = getCurrentDate() db.session.add(model_food_cat) db.session.commit() return jsonify(resp)
def cat_set(): if request.method == 'GET': res = {} req_data = request.values uid = int(req_data.get('id', 0)) food_info = None if uid: food_info = FoodCat.query.filter_by(id=uid).first() res['info'] = food_info return ops_render("food/cat_set.html", res) # POST:修改或者添加操作 res = {'code': 200, 'msg': '操作成功', 'data': {}} req_data = request.values uid = req_data['id'] if 'id' in req_data else 0 name = req_data['name'] if 'name' in req_data else '' weight = int(req_data['weight']) if ( 'weight' in req_data and int(req_data['weight']) > 0) else 1 if not name or len(name) < 1: res['code'] = -1 res['msg'] = '分类名不规范' return jsonify(res) food_cat_info = FoodCat.query.filter_by(id=uid).first() if food_cat_info: model_food_cat = food_cat_info else: # 查询不到说明是添加操作 model_food_cat = FoodCat() model_food_cat.created_time = get_current_time() # 添加和修改的共用代码 model_food_cat.name = name model_food_cat.weight = weight model_food_cat.update_time = get_current_time() db.session.add(model_food_cat) db.session.commit() return jsonify(res)