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) 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': resp_data = {} req = request.args id = req.get('id', '') id = int(id) if id.isdigit() else 0 cat_info = None if id: cat_info = FoodCat.query.filter_by(id=id).first() resp_data['cat_info'] = cat_info resp_data['current'] = 'cat' return ops_render('food/cat_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 name = req['name'] if 'name' in req else '' weight = int( req['weight']) if 'weight' in req and req['weight'].isdigit() else 1 if not name and len(name) < 2: resp['code'] = -1 resp['msg'] = '请输入符合规范的用户名' return jsonify(resp) if weight < 1: resp['code'] = -1 resp['msg'] = '请输入符合规范的权重,大于等于1' return jsonify(resp) #是否存在分类名 has_name = FoodCat.query.filter(FoodCat.name == name, FoodCat.id != id).first() if has_name: 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.status = 1 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': resp_data = {} req = request.args query = FoodCat.query id = req['id'] if 'id' in req else 0 foodcat_info = None if id: foodcat_info = query.filter_by(id=id).first() resp_data['info'] = foodcat_info resp_data['current'] = 'set' 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 not weight: resp['code'] = -1 resp['msg'] = '权重错误,重新输入' return jsonify(resp) food_info = FoodCat.query.filter_by(id=id).first() if food_info: foodcat_info = food_info else: foodcat_info = FoodCat() foodcat_info.created_time = geneTime() foodcat_info.name = name foodcat_info.weight = weight foodcat_info.updated_time = geneTime() db.session.add(foodcat_info) db.session.commit() return jsonify(resp)
def catSet(): if request.method == "GET": resp_data = {} input_id = request.args.get('id', 0, type=int) target_info = None if input_id: target_info = FoodCat.query.filter_by(id=input_id).first() resp_data['info'] = target_info resp_data['current'] = 'cat' return ops_render("food/cat_set.html", **resp_data) resp = {'code': 200, 'msg': '操作成功', 'data': {}} req = request.values input_id = ParamsValidator.GetORSetValue(req, 'id', value=0) name = ParamsValidator.GetORSetValue(req, 'name', value="") 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'] = "请输入符合规范的分类名称" else: food_cat_info = FoodCat.query.filter_by(id=input_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(): """ 菜品分类的 编辑和添加(编辑会查到菜的id修改信息,添加不到id) """ if request.method == "GET": resp_data = {} req = request.args id = int(req.get("id", 0)) info = None # 因为如果是添加信息,那么上个页面,就不会传回id,所以为None,进入添加账号页面。否则点击编辑就传回id,进入修改信息页面 if id: info = FoodCat.query.filter_by( id=id).first() # filter_by不用写类,他会自动区分的 resp_data['info'] = info # 统一渲染的 resp_data(json)里面,将user_info放进去 resp_data['current'] = 'cat' return ops_render("food/cat_set.html", resp_data) resp = {'code': 200, 'msg': u"操作成功", 'data': {}} # 获取登录变量 req = request.values # 所有的请求变量,放到这个数组里 id = req['id'] if 'id' in req else 0 # id 是用来判断是增加用户信息,还是修改用户信息 name = req['name'] if 'name' in req else '' # 三元表达式 weight = int(req['weight']) if ('weight' in req and int(req['weight']) > 0) else '' if name is None or len(name) < 2 or len(name) > 12: # 进行参数判断 resp['code'] = -1 resp['msg'] = "请输入符合规范的分类名称" return jsonify(resp) # json 格式的转换 food_cat_info = FoodCat.query.filter_by( id=id).first() # 判断食品 id是否存在。如果存在,那么 modle_use,就是这个食品的信息。set页面为修改用户信息 if food_cat_info: modle_cat_info = food_cat_info else: # 否则,就是这个uid不存在。那么久为增加用户信息界面 modle_cat_info = FoodCat() modle_cat_info.created_time = getCurrentData( ) # 增加用户信息时,created_time才改变 modle_cat_info.name = name modle_cat_info.weight = weight modle_cat_info.updated_time = getCurrentData() db.session.add(modle_cat_info) # 数据库添加数据,统一提交 db.session.commit() return jsonify(resp) # 返回信息,更改成功
def cat_set(): if request.method == "GET": resp_data = {} req = request.args id = int(req.get("id", 0)) cat_info =None if id: cat_info = FoodCat.query.filter_by(id = id).first() resp_data['info'] = cat_info 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 '' name = req['name'] if 'name' in req else '' weight = int(req['weight']) if 'weight' in req else 0 if name is None or len(name) < 1: resp['code'] = -1 resp['msg'] = '分类名称填写不正确' return jsonify(resp) if weight is None or weight < 1: resp['code'] = -1 resp['msg'] = '权重填写不正确' return jsonify(resp) food_info = FoodCat.query.filter_by(id = id).first() if food_info: model_food = food_info else: model_food = FoodCat() model_food.name = name model_food.weight = weight model_food.updated_time = getCurrentDate() model_food.created_time = getCurrentDate() db.session.add(model_food) db.session.commit() return jsonify(resp)
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) 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 food_cat_info = FoodCat.query.filter_by(id=id).first() # 获取uid,判断是否存在,存在是更新,否则就新增 if name is None or len(name) < 1: # 判断用户名长度 resp['code'] = -1 resp['msg'] = "请输入符合规范的分类名称" return jsonify(resp) if weight is None or weight < 1: # 判断邮箱长度 resp['code'] = -1 resp['msg'] = '请输入符合规范的权重,并且至少要大于1' return jsonify(resp) 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(): # 编辑和添加 —— 所以有可能有ID 有可能查不出ID if request.method == "GET": # id存在时进行编辑。不存在是进行展示。 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 ) # 获取并设置权重 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 # 参数少时用args id = int(req.get("id", 0)) # 获取当前id info = None if id: # 如果uid存在,说明进入的是编辑页面,否则进入的是新增页面 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) # 下面是POST处理 resp = {'code': 200, 'msg': '操作成功', 'data': ''} req = request.values # 参数多时用values ,参数少时用args id = req['id'] if 'id' in req else 0 # 获取当前用户id 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)