mirror of
https://github.com/veops/cmdb.git
synced 2025-08-06 23:34:27 +08:00
style: format common setting
This commit is contained in:
@@ -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
|
||||
|
@@ -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(
|
||||
|
@@ -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=[])
|
||||
|
@@ -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 = "邮箱格式错误"
|
||||
|
||||
|
Reference in New Issue
Block a user