def add_tags(request): """ 新增tags :param request: :return: """ data, msg = utils.get_post_data(request) tag_name = data['tag'] if tag_name == None: return jsonify({'msg': 'name is required'}) tag = Tag.query.filter_by(name=tag_name).first() if tag == None: new_tag = Tag(tag_name) new_tag.status = data.get('status') new_tag.created_time = datetime.now() new_tag.modified_time = datetime.now() new_tag.creater = 'lucy' db.session.add(new_tag) db.session.commit() else: return jsonify({'msg': 'tag exist !'}) action_log(request, '添加标签') return jsonify({'msg': msg, 'code': 0, 'time': datetime.now()})
def add_user(): data = request.form.to_dict() user = User.query.filter_by(username=data.get('username')).first() if user == None: new_user = User() new_user.username = data.get('username') new_user.password = '******' # default password new_user.nick_name = data.get('nick_name') new_user.full_name = data.get('full_name') new_user.address = data.get('address') new_user.birthday = data.get('birthday') new_user.gender = data.get('gender') new_user.status = 1 # 0:禁用,1:启用 new_user.confirmed = True # 未确认 mail = Mail(data.get('mail')) mail.user_id = new_user.id mail.status = 1 db.session.add(new_user) db.session.add(mail) db.session.commit() else: return jsonify({'msg': 'username exist !'}) action_log(request, '添加用户') return jsonify({'msg': 'ok !'})
def update_tag(id, request): """ 更新tag :param id: :param request: :return: json """ data, msg = utils.get_post_data(request) #tag_id = request.args.get("id") or None if id == None: return jsonify({'msg': 'tag is not exist'}) tag = Tag.query.filter_by(id=id).first() if not tag == None: for attr, val in data.items(): if hasattr(tag, attr) and getattr(tag, "name") != val: setattr(tag, attr, val) else: return jsonify({'msg': 'tag is exist'}) db.session.add(tag) db.session.commit() else: return jsonify({'msg': 'tag is not exist'}) action_log(request, '修改标签') return jsonify({'msg': msg, 'code': 0, 'time': datetime.now()})
def change_post_status(id): if not utils.check_str(id): return jsonify({'msg': 'id is illegal'}) post = Post.query.filter_by(id=id).first() status = request.args.get('status') if not post == None: post.status = status db.session.add(post) db.session.commit() else: return jsonify({'msg': 'post is not exist'}) action_log(request, '更改文章状态') return jsonify({'msg': 'ok', 'code': 0, 'time': datetime.now()})
def change_tag_status(id): if not utils.check_str(id): return jsonify({'msg': 'id is illegal'}) tag = Tag.query.filter_by(id=id).first() status = request.args.get('status') if not tag == None: tag.status = status db.session.add(tag) db.session.commit() else: return jsonify({'msg': 'tag is not exist'}) action_log(request, '更改标签状态') return jsonify({'msg': 'ok', 'code': 0, 'time': datetime.now()})
def del_role(role_id): role = Role.query.filter_by(id=role_id).first() if not role == None: db.session.delete(role) db.session.commit() else: return jsonify({ 'msg': 'role not exist !' }) action_log(request, '删除角色') response =jsonify({'msg': 'ok !' }) response.status_code = 200 return response
def add_catalog(): data = request.form.to_dict() name = data.get('name') catlog = Catalog.query.filter(Catalog.name == name).first() if catlog == None: cat = Catalog(name) cat.code = data.get('code') cat.is_show = data.get('is_show') cat.comments = data.get('comments') cat.created_time = datetime.now() db.session.add(cat) db.session.commit() else: return jsonify({'msg': 'catalog code exist !'}) action_log(request, '添加字典目录') return jsonify({'msg': 'ok !'})
def add_dictionary(): data = request.form.to_dict() code = data.get('code') dicts = Dictionary.query.filter(Dictionary.code == code).first() if dicts == None: d = Dictionary(data.get('name')) d.code = data.get('code') d.order = data.get('oder') d.catalog_id = data.get('catalog_id') d.created_time = datetime.now() db.session.add(d) db.session.commit() else: return jsonify({'msg': 'dictionary code exist !'}) action_log(request, '添加字典') return jsonify({'msg': 'ok !'})
def del_tag(id, request): """ 删除tag :param id: :param request: :return: """ if id == None: return jsonify({'msg': 'tag is not exist'}) tag = Tag.query.filter_by(id=id).first() if not tag == None: db.session.delete(tag) db.session.commit() else: return jsonify({'msg': 'tag is not exist'}) action_log(request, '删除标签') return jsonify({'msg': 'ok', 'code': 0, 'time': datetime.now()})
def add_role(): data = request.form.to_dict() role_name = data.get('name') role = Role.query.filter_by(name=role_name).first() if role == None: role_new = Role(role_name) role_new.description = data.get('description') role_new.created_time = datetime.now() db.session.add(role_new) db.session.commit() else: return jsonify({ 'msg': 'role name exist !' }) action_log(request, '添加角色') return jsonify({ 'msg': 'ok !' })
def edit_catalog(catalog_id): data = request.form.to_dict() id = data.get('id') res = Catalog.query.filter(Catalog.id == id).first() is_update = False if not res == None: for attr, val in data.items(): if hasattr(Catalog, attr): # 检查实例是否有这个属性 if not val == None: setattr(Catalog, attr, val) # same as: a.name = is_update = True if is_update: res.modified_time = datetime.now() db.session.add(res) db.session.commit() else: return jsonify({'msg': 'res not exist !'}) action_log(request, '修改菜单') return jsonify({'msg': 'ok !'})
def add_action(): #data = json.loads(request.form) data=request.form.to_dict() code=data.get('action_code') has_action = Action.query.filter(Action.code == code).first() if has_action ==None: action1=Action(data.get('action_name')) action1.type=data.get('action_type') action1.code=code action1.comments=data.get('action_comments') db.session.add(action1) db.session.commit() else: return jsonify({ 'msg':'action code exist !' }) action_log(request,'添加操作权限') return jsonify({ 'msg':'sbumit success !' })
def edit_role(): data = request.form.to_dict() role_id = data.get('id') role_name = data.get('name') role = Role.query.filter_by(id=role_id).first() if not role == None: if not role_name ==None: role.name = role_name role.description = data.get('description') role.modified_time = datetime.now() db.session.add(role) db.session.commit() else: return jsonify({ 'msg': 'role not exist !' }) action_log(request, '修改角色') return jsonify({ 'msg': 'ok !' })
def edit_user(): data = request.form.to_dict() user = User.query.filter_by(id=data.get('id')).first() is_update = False if not user == None: for attr, val in data.items(): if hasattr(User, attr): # 检查实例是否有这个属性 if attr == 'username': logger.info("用户名不支持修改!") continue if not val == None: setattr(User, attr, val) # same as: a.name = is_update = True if is_update: user.modified_time = datetime.now() db.session.add(user) db.session.commit() else: return jsonify({'msg': 'user not exist !'}) action_log(request, '修改用户信息') return jsonify({'msg': 'ok !'})
def add_resource(): data=request.form.to_dict() name=data.get('name') menu = Resource.query.filter(Resource.name==name).first() if menu ==None: res =Resource(name) res.pid=data.get('pid') res.icon=data.get('icon') res.url=data.get('url') res.order = data.get('order') res.bg_color= data.get('bg_color') res.comments=data.get('comments') res.created_time =datetime.now() db.session.add(res) db.session.commit() else: return jsonify({ 'msg':'res code exist !' }) action_log(request,'添加菜单') return jsonify({ 'msg':'ok !' })