def add_user(): """ 添加用户信息 :return: """ if request.method == "GET": result = Role.all_roles(validator=False) return jsonify({"code": 0, "result": result}) add_item = new_user_Form(request.form) add_item.r_id.choices = Role.all_roles() roles = Role.choiced_role_item(add_item.r_id.data) if add_item.validate(): user = User() with db.auto_commit(): user.set_attrs(add_item.data) user.role = roles db.session.add(user) # 添加用户策略到访问控制文件 ugp = [] if user.is_lock == 0: for r in roles: ugp.append([r.name]) update_group_policy = ret_all_groups() update_group_policy[user.username] = ugp file_adapter.change_policy("g", update_group_policy) return data_return() return data_return(code=1, errmsg=err_list(add_item))
def add_role(): res_data = request.form id = res_data.get("id") if id: r_data = UpdateRoleForm(request.form) role = Role.query.filter(Role.id==id).first_or_404() else: r_data = AddRoleForm(request.form) role = Role() r_data.p_id.choices = Permission.all_permission() ob_perm = Permission.choiced_permission_item(r_data.p_id.data) if r_data.validate(): with db.auto_commit(): role.set_attrs(r_data.data) role.permission = ob_perm db.session.add(role) # 添加角色权限到ACL ugp = [] for p in ob_perm: ugp.append([p.url+"/*", "*"]) update_group_policy = ret_all_policy() update_group_policy[role.name] = ugp file_adapter.change_policy("p", update_group_policy) return data_return() else: return data_return(code=1,errmsg=err_list(r_data))
def create_idc(): res = IdcForm(request.form) if res.validate(): idc = Idc() idc.set_attrs(res.data) with db.auto_commit(): db.session.add(idc) return data_return() else: err = err_list(res) return data_return(code=1, errmsg=err)
def update_idc(): if request.method == "GET": id = request.args.get("id") idc_item = Idc.query.filter(Idc.id == id).first_or_404() return data_return(result=model_serializable(idc_item)) id = request.form.get("id") idc_item = Idc.query.filter(Idc.id == id).first_or_404() res_idc = UpdateIdcForm(request.form) if res_idc.validate(): with db.auto_commit(): idc_item.set_attrs(res_idc.data) return data_return() else: return data_return(code=1,errmsg= err_list(res_idc))
def update_server(): if request.method == "GET": id = request.args.get("id") s_item = Server.query.filter(Server.id == id).first_or_404() res = model_serializable(s_item) return data_return(result=res) res_s = UpdateServerForm(request.form) if res_s.validate(): id = res_s.id.data update_s = Server.query.filter(Server.id==id).first_or_404() with db.auto_commit(): update_s.set_attrs(res_s.data) return data_return() else: return data_return(code=1,errmsg=err_list(res_s))
def add_permission(): res_data = request.form id = res_data.get("id") if id: perm = Permission.query.filter(Permission.id == id).first_or_404() p_data = UpdatePermForm(request.form) else: perm = Permission() p_data = AddPermForm(request.form) if p_data.validate(): with db.auto_commit(): perm.set_attrs(p_data.data) db.session.add(perm) return data_return() else: return data_return(code=1, errmsg=err_list(p_data))
def add_cabinet(): if request.method == "GET": res = [] res_list = Idc.query.all() for item in res_list: res.append(model_serializable(item)) return data_return(result=res) r_cab = AddCabinetForm(request.form) if r_cab.validate(): cab = Cabinet() with db.auto_commit(): cab.set_attrs(r_cab.data) db.session.add(cab) return data_return() else: return data_return(code=1, errmsg=err_list(r_cab))
def update_cabinet(): if request.method == "GET": id = request.args.get("id") update_cab = Cabinet.query.filter(Cabinet.id == id).first_or_404() res = model_serializable(update_cab) return data_return(result=res) r_cab = UpdateCabinetForm(request.form) id = r_cab.id.data if r_cab.validate(): cab = Cabinet.query.filter(Cabinet.id == id).first_or_404() with db.auto_commit(): cab.set_attrs(r_cab.data) db.session.add(cab) return data_return() else: return data_return(code=1, errmsg=err_list(r_cab))
def add_server(): if request.method == "GET": """ 获取所有机柜信息 """ all_cab = Cabinet.query.all() res = [model_serializable(c) for c in all_cab] return data_return(result=res) add_server_form = AddServerForm(request.form) if add_server_form.validate(): serv = Server() with db.auto_commit(): serv.set_attrs(add_server_form.data) db.session.add(serv) return data_return() else: return data_return(code=1,errmsg=err_list(add_server_form))
def add_switch(): if request.method == "GET": """ 获取所有机柜信息 """ all_cab = Cabinet.query.all() res = [model_serializable(c) for c in all_cab] return data_return(result=res) res_switch = AddSwitchForm(request.form) if res_switch.validate(): with db.auto_commit(): sw = Switch() sw.set_attrs(res_switch.data) db.session.add(sw) return data_return() else: return data_return(code=1, errmsg=err_list(res_switch))