Skip to content

Commit

Permalink
cr 调整
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Aug 23, 2023
2 parents e4fcd62 + 209c4be commit 7bad3dd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/bk-user/bkuser/apis/web/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _get_tenant_id(self) -> str:

def get_serializer_context(self):
tenant_ids = list(self.queryset.values_list("id", flat=True))
tenant_root_departments_map = TenantDepartmentHandler.get_tenant_root_departments_by_id(
tenant_root_departments_map = TenantDepartmentHandler.get_tenant_root_department_map_by_tenant_id(
tenant_ids, self._get_tenant_id()
)
return {"tenant_root_departments_map": tenant_root_departments_map}
Expand Down Expand Up @@ -107,4 +107,4 @@ def get(self, request, *args, **kwargs):
# 拉取子部门信息列表
tenant_department_children = TenantDepartmentHandler.get_tenant_department_children_by_id(tenant_department_id)
data = [item.model_dump(include={"id", "name", "has_children"}) for item in tenant_department_children]
return Response(self.get_serializer(data, many=True).data)
return Response(TenantDepartmentChildrenListOutputSLZ(data, many=True).data)
25 changes: 7 additions & 18 deletions src/bk-user/bkuser/biz/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class DataSourceDepartmentInfoWithChildren(BaseModel):
id: int
name: str
children: List[int]
children_ids: List[int]


class DataSourceSimpleInfo(BaseModel):
Expand All @@ -45,34 +45,23 @@ def get_data_source_map_by_owner(

return data

@staticmethod
def get_data_sources_by_tenant(tenant_ids: List[str]) -> Dict[str, List[int]]:
# 当前属于租户的数据源
tenant_data_source_map: Dict = {}
data_sources = DataSource.objects.filter(owner_tenant_id__in=tenant_ids).values("id", "owner_tenant_id")
for item in data_sources:
tenant_id = item["owner_tenant_id"]
if tenant_id in tenant_data_source_map:
tenant_data_source_map[tenant_id].append(item["id"])
else:
tenant_data_source_map[tenant_id] = [item["id"]]
# TODO 协同数据源获取
return tenant_data_source_map


class DataSourceDepartmentHandler:
@staticmethod
def get_department_info_by_id(department_ids: List[int]) -> Dict[int, DataSourceDepartmentInfoWithChildren]:
def get_department_info_map_by_id(department_ids: List[int]) -> Dict[int, DataSourceDepartmentInfoWithChildren]:
"""
获取部门基础信息
"""
departments = DataSourceDepartment.objects.filter(id__in=department_ids)
departments_map: Dict = {}
for item in departments:
children = DataSourceDepartmentRelation.objects.get(department=item).get_children()
departments_map[item.id] = DataSourceDepartmentInfoWithChildren(
id=item.id,
name=item.name,
children=list(children.values_list("department_id", flat=True)),
children_ids=list(
DataSourceDepartmentRelation.objects.get(department=item)
.get_children()
.values_list("department_id", flat=True)
),
)
return departments_map
39 changes: 26 additions & 13 deletions src/bk-user/bkuser/biz/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from bkuser.apps.data_source.models import DataSource, DataSourceDepartmentRelation, DataSourcePlugin, DataSourceUser
from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantManager, TenantUser
from bkuser.biz.data_source import DataSourceDepartmentHandler, DataSourceHandler
from bkuser.biz.data_source import DataSourceDepartmentHandler, DataSourceHandler, DataSourceSimpleInfo
from bkuser.utils.uuid import generate_uuid


Expand Down Expand Up @@ -135,10 +135,8 @@ def retrieve_tenant_managers(tenant_id: str) -> List[TenantUserWithInheritedInfo
"""
查询单个租户的租户管理员
"""
tenant_managers = TenantManager.objects.filter(tenant_id=tenant_id)
# 查询管理员对应的信息
tenant_user_ids = [i.tenant_user_id for i in tenant_managers]
return TenantUserHandler.list_tenant_user_by_id(tenant_user_ids)
# 查询单个租户的管理员对应的信息
return TenantHandler.get_tenant_manager_map([tenant_id]).get(tenant_id) or []

@staticmethod
def create_with_managers(tenant_info: TenantBaseInfo, managers: List[TenantManagerWithoutID]) -> str:
Expand Down Expand Up @@ -204,6 +202,19 @@ def update_with_managers(tenant_id: str, tenant_info: TenantEditableBaseInfo, ma
batch_size=100,
)

@staticmethod
def get_data_source_ids_map_by_id(tenant_ids: List[str]) -> Dict[str, List[int]]:
# 当前属于租户的数据源
tenant_data_source_map: Dict = {}
data_sources: Dict[str, List[DataSourceSimpleInfo]] = DataSourceHandler.get_data_source_map_by_owner(
tenant_ids
)
for tenant_id, data_source_list in data_sources.items():
data_source_ids: List = [data_source.id for data_source in data_source_list]
tenant_data_source_map[tenant_id] = data_source_ids
# TODO 协同数据源获取
return tenant_data_source_map


class TenantDepartmentHandler:
@staticmethod
Expand All @@ -217,12 +228,12 @@ def convert_data_source_department_to_tenant_department(
tenant_departments = TenantDepartment.objects.filter(tenant_id=tenant_id)

# 获取数据源部门基础信息
data_source_departments = DataSourceDepartmentHandler.get_department_info_by_id(data_source_department_ids)
data_source_departments = DataSourceDepartmentHandler.get_department_info_map_by_id(data_source_department_ids)

# data_source_departments中包含了父子部门的ID,协同数据源需要查询绑定了该租户
department_ids = list(data_source_departments.keys())
for department in data_source_departments.values():
department_ids += department.children
department_ids += department.children_ids

# NOTE: 协同数据源,可能存在未授权全部子部门
# 提前拉取所有映射, 过滤绑定的租户部门
Expand All @@ -243,7 +254,9 @@ def convert_data_source_department_to_tenant_department(
# 部门基础信息
data_source_department_info = data_source_departments[data_source_department_id]
# 只要一个子部门被授权,都是存在子部门
children_flag = [True for child in data_source_department_info.children if child in bound_departments_ids]
children_flag = [
True for child in data_source_department_info.children_ids if child in bound_departments_ids
]
data.append(
TenantDepartmentBaseInfo(
id=tenant_department.id,
Expand All @@ -254,10 +267,10 @@ def convert_data_source_department_to_tenant_department(
return data

@staticmethod
def get_tenant_root_departments_by_id(
tenant_ids: List[str], convert_tenant_id: str
def get_tenant_root_department_map_by_tenant_id(
tenant_ids: List[str], current_tenant_id: str
) -> Dict[str, List[TenantDepartmentBaseInfo]]:
data_source_map = DataSourceHandler.get_data_sources_by_tenant(tenant_ids)
data_source_map = TenantHandler.get_data_source_ids_map_by_id(tenant_ids)

# 通过获取数据源的根节点
tenant_root_department_map: Dict = {}
Expand All @@ -267,9 +280,9 @@ def get_tenant_root_departments_by_id(
.filter(data_source_id__in=data_source_ids)
.values_list("department_id", flat=True)
)
# 转换数据源部门为当前为 current_tenant_id 租户部门
# 转换数据源部门为当前为 current_tenant_id 租户的租户部门
tenant_root_department = TenantDepartmentHandler.convert_data_source_department_to_tenant_department(
tenant_id=convert_tenant_id, data_source_department_ids=list(root_department_ids)
tenant_id=current_tenant_id, data_source_department_ids=list(root_department_ids)
)
tenant_root_department_map[tenant_id] = tenant_root_department
return tenant_root_department_map
Expand Down

0 comments on commit 7bad3dd

Please sign in to comment.