diff --git a/cmdb-api/api/lib/common_setting/const.py b/cmdb-api/api/lib/common_setting/const.py index c9edccd..2768f56 100644 --- a/cmdb-api/api/lib/common_setting/const.py +++ b/cmdb-api/api/lib/common_setting/const.py @@ -4,11 +4,11 @@ COMMON_SETTING_QUEUE = "common_setting_async" class OperatorType(BaseEnum): - EQUAL = 1 # 等于 - NOT_EQUAL = 2 # 不等于 - IN = 3 # 包含 - NOT_IN = 4 # 不包含 - GREATER_THAN = 5 # 大于 - LESS_THAN = 6 # 小于 - IS_EMPTY = 7 # 为空 - IS_NOT_EMPTY = 8 # 不为空 + EQUAL = 1 + NOT_EQUAL = 2 + IN = 3 + NOT_IN = 4 + GREATER_THAN = 5 + LESS_THAN = 6 + IS_EMPTY = 7 + IS_NOT_EMPTY = 8 diff --git a/cmdb-api/api/lib/common_setting/department.py b/cmdb-api/api/lib/common_setting/department.py index ce149b9..e810b06 100644 --- a/cmdb-api/api/lib/common_setting/department.py +++ b/cmdb-api/api/lib/common_setting/department.py @@ -7,6 +7,7 @@ from wtforms import IntegerField from wtforms import StringField from wtforms import validators +from api.extensions import db from api.lib.common_setting.resp_format import ErrFormat from api.lib.perm.acl.role import RoleCRUD from api.models.common_setting import Department, Employee @@ -94,11 +95,9 @@ class DepartmentTree(object): for top_d in top_departments: department_id = top_d['department_id'] - # 检查 department_id 是否作为其他部门的 parent sub_deps = self.get_department_by_parent_id(department_id) employees = [] if self.append_employee: - # 要包含员工 employees = self.get_employees_by_d_id(department_id) top_d['employees'] = employees @@ -127,7 +126,6 @@ class DepartmentTree(object): sub_deps = self.get_department_by_parent_id(d['department_id']) employees = [] if self.append_employee: - # 要包含员工 employees = self.get_employees_by_d_id(d['department_id']) d['employees'] = employees @@ -184,7 +182,6 @@ class DepartmentCRUD(object): def check_department_parent_id_allow(d_id, department_parent_id): if department_parent_id == 0: return - # 检查 department_parent_id 是否在许可范围内 allow_p_d_id_list = DepartmentCRUD.get_allow_parent_d_id_by(d_id) target = list( filter(lambda d: d['department_id'] == department_parent_id, allow_p_d_id_list)) @@ -263,9 +260,6 @@ class DepartmentCRUD(object): @staticmethod def get_allow_parent_d_id_by(department_id): - """ - 获取可以成为 department_id 的 department_parent_id 的 list - """ tree_list = DepartmentCRUD.get_department_tree_list() allow_d_id_list = [] @@ -307,7 +301,6 @@ class DepartmentCRUD(object): if len(all_deps) == 0: return [] - # 一级部门 top_deps = list(filter(lambda d: d['department_parent_id'] == -1, all_deps)) if len(top_deps) == 0: return [] @@ -321,7 +314,6 @@ class DepartmentCRUD(object): top_d['department_name'], identifier_root ) - # 检查 department_id 是否作为其他部门的 parent sub_ds = list(filter(lambda d: d['department_parent_id'] == identifier_root, all_deps)) if len(sub_ds) == 0: tree_list.append(tree) @@ -350,6 +342,13 @@ class DepartmentCRUD(object): DepartmentCRUD.parse_sub_department_node( next_sub_ds, all_ds, tree, d['department_id']) + @staticmethod + def get_department_by_query(query, to_dict=True): + results = query.all() + if not results: + return [] + return results if not to_dict else [r.to_dict() for r in results] + @staticmethod def get_departments_and_ids(department_parent_id, block): query = Department.query.filter( diff --git a/cmdb-api/api/lib/common_setting/employee.py b/cmdb-api/api/lib/common_setting/employee.py index a59cc5a..6cf27f8 100644 --- a/cmdb-api/api/lib/common_setting/employee.py +++ b/cmdb-api/api/lib/common_setting/employee.py @@ -18,6 +18,18 @@ from api.lib.common_setting.const import COMMON_SETTING_QUEUE, OperatorType from api.lib.common_setting.resp_format import ErrFormat from api.models.common_setting import Employee, Department +acl_user_columns = [ + 'email', + 'mobile', + 'nickname', + 'username', + 'password', + 'block', + 'avatar', +] +employee_pop_columns = ['password'] +can_not_edit_columns = ['email'] + def edit_acl_user(uid, **kwargs): user_data = {column: kwargs.get( @@ -68,9 +80,6 @@ class EmployeeCRUD(object): @staticmethod def get_employee_by_uid_with_create(_uid): - """ - 根据 uid 获取员工信息,不存在则创建 - """ try: return EmployeeCRUD.get_employee_by_uid(_uid).to_dict() except Exception as e: @@ -100,7 +109,6 @@ class EmployeeCRUD(object): acl_uid=user_info['uid'], ) return existed.to_dict() - # 创建员工 if not user_info.get('nickname', None): user_info['nickname'] = user_info['name'] @@ -143,9 +151,6 @@ class EmployeeCRUD(object): if len(e_list) > 0: from api.tasks.common_setting import edit_employee_department_in_acl - # fixme: comment next line - # edit_employee_department_in_acl(e_list, new_department_id, current_user.uid) - edit_employee_department_in_acl.apply_async( args=(e_list, new_department_id, current_user.uid), queue=COMMON_SETTING_QUEUE @@ -218,8 +223,6 @@ class EmployeeCRUD(object): else: employees = Employee.get_by(to_dict=False) - keep_cols = EmployeeCRUD.get_current_user_view_columns() - all_departments = Department.get_by(to_dict=False) d_id_map = {d.department_id: d.department_name for d in all_departments} e_id_map = {e.employee_id: e.nickname for e in employees} @@ -249,8 +252,7 @@ class EmployeeCRUD(object): data['department_name'] = department_name data['nickname_direct_supervisor'] = nickname_direct_supervisor - tmp = {export_columns_map[k]: data[k] for k in export_columns_map.keys() if - k in keep_cols or k in sub_columns} + tmp = {export_columns_map[k]: data[k] for k in export_columns_map.keys()} data_list.append(tmp) @@ -355,7 +357,6 @@ class EmployeeCRUD(object): existed = EmployeeCRUD.get_employee_by_id(_id) value = get_block_value(kwargs.get('block')) if value is True: - # 判断该用户是否为 部门负责人,或者员工的直接上级 check_department_director_id_or_direct_supervisor_id(_id) if is_acl: @@ -449,7 +450,7 @@ class EmployeeCRUD(object): @staticmethod def get_expr_by_condition(column, operator, value, relation): """ - 根据conditions返回expr: (and_list, or_list) + get expr: (and_list, or_list) """ attr = EmployeeCRUD.get_attr_by_column(column) # 根据operator生成条件表达式 @@ -483,7 +484,6 @@ class EmployeeCRUD(object): else: abort(400, ErrFormat.not_support_operator.format(operator)) - # 根据relation生成复合条件 if relation == "&": return expr, [] elif relation == "|": @@ -493,7 +493,6 @@ class EmployeeCRUD(object): @staticmethod def check_condition(column, operator, value, relation): - # 对于condition中column为空的,报错 if column is None or operator is None or relation is None: return abort(400, ErrFormat.conditions_field_missing) @@ -642,19 +641,6 @@ def get_user_map(key='uid', acl=None): return data -acl_user_columns = [ - 'email', - 'mobile', - 'nickname', - 'username', - 'password', - 'block', - 'avatar', -] -employee_pop_columns = ['password'] -can_not_edit_columns = ['email'] - - def format_params(params): for k in ['_key', '_secret']: params.pop(k, None) @@ -664,19 +650,22 @@ def format_params(params): class CreateEmployee(object): def __init__(self): self.acl = ACLManager() - self.useremail_map = {} + self.all_acl_users = self.acl.get_all_users() - def check_acl_user(self, email): - user_info = self.useremail_map.get(email, None) - if user_info: - return user_info - return None + def check_acl_user(self, user_data): + target_email = list(filter(lambda x: x['email'] == user_data['email'], self.all_acl_users)) + if target_email: + return target_email[0] + + target_username = list(filter(lambda x: x['username'] == user_data['username'], self.all_acl_users)) + if target_username: + return target_username[0] def add_acl_user(self, **kwargs): user_data = {column: kwargs.get( column, '') for column in acl_user_columns if kwargs.get(column, '')} try: - existed = self.check_acl_user(user_data['email']) + existed = self.check_acl_user(user_data) if not existed: return self.acl.create_user(user_data) return existed @@ -685,8 +674,6 @@ class CreateEmployee(object): def create_single(self, **kwargs): EmployeeCRUD.check_email_unique(kwargs['email']) - self.useremail_map = self.useremail_map if self.useremail_map else get_user_map( - 'email', self.acl) user = self.add_acl_user(**kwargs) kwargs['acl_uid'] = user['uid'] kwargs['last_login'] = user['last_login'] @@ -699,8 +686,6 @@ class CreateEmployee(object): ) def create_single_with_import(self, **kwargs): - self.useremail_map = self.useremail_map if self.useremail_map else get_user_map( - 'email', self.acl) user = self.add_acl_user(**kwargs) kwargs['acl_uid'] = user['uid'] kwargs['last_login'] = user['last_login'] @@ -743,9 +728,6 @@ class CreateEmployee(object): return end_d_id def format_department_id(self, employee): - """ - 部门名称转化为ID,不存在则创建 - """ department_name_map = {} try: department_name = employee.get('department_name', '') @@ -762,16 +744,13 @@ class CreateEmployee(object): def batch_create(self, employee_list): err_list = [] - self.useremail_map = get_user_map('email', self.acl) for employee in employee_list: try: - # 获取username username = employee.get('username', None) if username is None: employee['username'] = employee['email'] - # 校验通过后获取department_id employee = self.format_department_id(employee) err = employee.get('err', None) if err: @@ -783,7 +762,7 @@ class CreateEmployee(object): raise Exception( ','.join(['{}: {}'.format(filed, ','.join(msg)) for filed, msg in form.errors.items()])) - data = self.create_single_with_import(**form.data) + self.create_single_with_import(**form.data) except Exception as e: err_list.append({ 'email': employee.get('email', ''), @@ -797,12 +776,12 @@ class CreateEmployee(object): class EmployeeAddForm(Form): username = StringField(validators=[ - validators.DataRequired(message="username不能为空"), + validators.DataRequired(message=ErrFormat.username_is_required), validators.Length(max=255), ]) email = StringField(validators=[ - validators.DataRequired(message="邮箱不能为空"), - validators.Email(message="邮箱格式不正确"), + validators.DataRequired(message=ErrFormat.email_is_required), + validators.Email(message=ErrFormat.email_format_error), validators.Length(max=255), ]) password = StringField(validators=[ @@ -811,7 +790,7 @@ class EmployeeAddForm(Form): position_name = StringField(validators=[]) nickname = StringField(validators=[ - validators.DataRequired(message="用户名不能为空"), + validators.DataRequired(message=ErrFormat.nickname_is_required), validators.Length(max=255), ]) sex = StringField(validators=[]) @@ -822,7 +801,7 @@ class EmployeeAddForm(Form): class EmployeeUpdateByUidForm(Form): nickname = StringField(validators=[ - validators.DataRequired(message="用户名不能为空"), + validators.DataRequired(message=ErrFormat.nickname_is_required), validators.Length(max=255), ]) avatar = StringField(validators=[]) diff --git a/cmdb-api/api/lib/common_setting/resp_format.py b/cmdb-api/api/lib/common_setting/resp_format.py index 88f5284..1d3b8d9 100644 --- a/cmdb-api/api/lib/common_setting/resp_format.py +++ b/cmdb-api/api/lib/common_setting/resp_format.py @@ -49,3 +49,8 @@ class ErrFormat(CommonErrFormat): acl_add_user_to_role_failed = "ACL 添加用户到角色失败: {}" acl_import_user_failed = "ACL 导入用户[{}]失败: {}" + nickname_is_required = "用户名不能为空" + username_is_required = "username不能为空" + email_is_required = "邮箱不能为空" + email_format_error = "邮箱格式错误" + diff --git a/cmdb-api/api/models/common_setting.py b/cmdb-api/api/models/common_setting.py index 741f664..2bcc4f6 100644 --- a/cmdb-api/api/models/common_setting.py +++ b/cmdb-api/api/models/common_setting.py @@ -13,40 +13,39 @@ class Department(ModelWithoutPK): __tablename__ = 'common_department' department_id = db.Column(db.Integer, primary_key=True, autoincrement=True) - department_name = db.Column(db.VARCHAR(255), default='', comment='部门名称') + department_name = db.Column(db.VARCHAR(255), default='') department_director_id = db.Column( - db.Integer, default=0, comment='部门负责人ID') - department_parent_id = db.Column(db.Integer, default=1, comment='上级部门ID') + db.Integer, default=0) + department_parent_id = db.Column(db.Integer, default=1) - sort_value = db.Column(db.Integer, default=0, comment='排序值') + sort_value = db.Column(db.Integer, default=0) - acl_rid = db.Column(db.Integer, comment='ACL中rid', default=0) + acl_rid = db.Column(db.Integer, default=0) class Employee(ModelWithoutPK): __tablename__ = 'common_employee' employee_id = db.Column(db.Integer, primary_key=True, autoincrement=True) - email = db.Column(db.VARCHAR(255), default='', comment='邮箱') - username = db.Column(db.VARCHAR(255), default='', comment='用户名') - nickname = db.Column(db.VARCHAR(255), default='', comment='姓名') - sex = db.Column(db.VARCHAR(64), default='', comment='性别') - position_name = db.Column(db.VARCHAR(255), default='', comment='职位名称') - mobile = db.Column(db.VARCHAR(255), default='', comment='电话号码') - avatar = db.Column(db.VARCHAR(255), default='', comment='头像') + email = db.Column(db.VARCHAR(255), default='') + username = db.Column(db.VARCHAR(255), default='') + nickname = db.Column(db.VARCHAR(255), default='') + sex = db.Column(db.VARCHAR(64), default='') + position_name = db.Column(db.VARCHAR(255), default='') + mobile = db.Column(db.VARCHAR(255), default='') + avatar = db.Column(db.VARCHAR(255), default='') - direct_supervisor_id = db.Column(db.Integer, default=0, comment='直接上级ID') + direct_supervisor_id = db.Column(db.Integer, default=0) department_id = db.Column(db.Integer, - db.ForeignKey('common_department.department_id'), - comment='部门ID', + db.ForeignKey('common_department.department_id') ) - acl_uid = db.Column(db.Integer, comment='ACL中uid', default=0) - acl_rid = db.Column(db.Integer, comment='ACL中rid', default=0) - acl_virtual_rid = db.Column(db.Integer, comment='ACL中虚拟角色rid', default=0) - last_login = db.Column(db.TIMESTAMP, nullable=True, comment='上次登录时间') - block = db.Column(db.Integer, comment='锁定状态', default=0) + acl_uid = db.Column(db.Integer, default=0) + acl_rid = db.Column(db.Integer, default=0) + acl_virtual_rid = db.Column(db.Integer, default=0) + last_login = db.Column(db.TIMESTAMP, nullable=True) + block = db.Column(db.Integer, default=0) _department = db.relationship( 'Department', backref='common_employee.department_id', @@ -55,14 +54,11 @@ class Employee(ModelWithoutPK): class EmployeeInfo(Model): - """ - 员工信息 - """ __tablename__ = 'common_employee_info' - info = db.Column(db.JSON, default={}, comment='员工信息') + info = db.Column(db.JSON, default={}) employee_id = db.Column(db.Integer, db.ForeignKey( - 'common_employee.employee_id'), comment='员工ID') + 'common_employee.employee_id')) employee = db.relationship( 'Employee', backref='common_employee.employee_id', lazy='joined') @@ -74,16 +70,13 @@ class CompanyInfo(Model): class InternalMessage(Model): - """ - 内部消息 - """ __tablename__ = "common_internal_message" - title = db.Column(db.VARCHAR(255), nullable=True, comment='标题') - content = db.Column(db.TEXT, nullable=True, comment='内容') - path = db.Column(db.VARCHAR(255), nullable=True, comment='跳转路径') - is_read = db.Column(db.Boolean, default=False, comment='是否已读') - app_name = db.Column(db.VARCHAR(128), nullable=False, comment='应用名称') - category = db.Column(db.VARCHAR(128), nullable=False, comment='分类') - message_data = db.Column(db.JSON, nullable=True, comment='数据') + title = db.Column(db.VARCHAR(255), nullable=True) + content = db.Column(db.TEXT, nullable=True) + path = db.Column(db.VARCHAR(255), nullable=True) + is_read = db.Column(db.Boolean, default=False) + app_name = db.Column(db.VARCHAR(128), nullable=False) + category = db.Column(db.VARCHAR(128), nullable=False) + message_data = db.Column(db.JSON, nullable=True) employee_id = db.Column(db.Integer, db.ForeignKey('common_employee.employee_id'), comment='ID') diff --git a/cmdb-api/api/tasks/common_setting.py b/cmdb-api/api/tasks/common_setting.py index 4594218..ca0f669 100644 --- a/cmdb-api/api/tasks/common_setting.py +++ b/cmdb-api/api/tasks/common_setting.py @@ -13,13 +13,9 @@ from api.models.common_setting import Department @celery.task(name="common_setting.edit_employee_department_in_acl", queue=COMMON_SETTING_QUEUE) def edit_employee_department_in_acl(e_list, new_d_id, op_uid): """ - 在 ACL 员工更换部门 - :param e_list: 员工列表 {acl_rid: 11, department_id: 22} - :param new_d_id: 新部门 ID - :param op_uid: 操作人 ID - - 在老部门中删除员工 - 在新部门中添加员工 + :param e_list:{acl_rid: 11, department_id: 22} + :param new_d_id + :param op_uid """ db.session.remove() @@ -43,7 +39,6 @@ def edit_employee_department_in_acl(e_list, new_d_id, op_uid): new_department_acl_rid = new_department.acl_rid if new_d_rid_in_acl == new_department.acl_rid else new_d_rid_in_acl for employee in e_list: - # 根据 部门ID获取部门 acl_rid old_department = Department.get_by( first=True, department_id=employee.get('department_id'), to_dict=False) if not old_department: @@ -61,7 +56,6 @@ def edit_employee_department_in_acl(e_list, new_d_id, op_uid): acl_rid=old_d_rid_in_acl ) d_acl_rid = old_department.acl_rid if old_d_rid_in_acl == old_department.acl_rid else old_d_rid_in_acl - # 在老部门中删除员工 payload = { 'app_id': 'acl', 'parent_id': d_acl_rid, @@ -71,7 +65,6 @@ def edit_employee_department_in_acl(e_list, new_d_id, op_uid): except Exception as e: result.append(ErrFormat.acl_remove_user_from_role_failed.format(str(e))) - # 在新部门中添加员工 payload = { 'app_id': 'acl', 'child_ids': [employee_acl_rid], diff --git a/cmdb-api/api/views/common_setting/department.py b/cmdb-api/api/views/common_setting/department.py index f9d9c7b..9a8dd4a 100644 --- a/cmdb-api/api/views/common_setting/department.py +++ b/cmdb-api/api/views/common_setting/department.py @@ -100,7 +100,7 @@ class DepartmentSortView(APIView): def put(self): """ - 修改部门排序,只能在同一个上级内排序 + only can sort in the same parent """ department_list = request.json.get('department_list', None) if department_list is None: diff --git a/cmdb-api/api/views/common_setting/employee.py b/cmdb-api/api/views/common_setting/employee.py index 85aa0b0..804f54b 100644 --- a/cmdb-api/api/views/common_setting/employee.py +++ b/cmdb-api/api/views/common_setting/employee.py @@ -150,12 +150,10 @@ class EmployeeViewExportExcel(APIView): url_prefix = (f'{prefix}/export_all',) def get(self): - # 规定了静态文件的存储位置 excel_filename = 'all_employee_info.xlsx' excel_path = current_app.config['UPLOAD_DIRECTORY_FULL'] excel_path_with_filename = os.path.join(excel_path, excel_filename) - # 根据parameter查表,自连接通过上级id获取上级名字列 block_status = int(request.args.get('block_status', -1)) data_list = EmployeeCRUD.get_export_employee_list(block_status)