diff --git a/src/bk-user/bkuser/apis/open_v2/views/profilers.py b/src/bk-user/bkuser/apis/open_v2/views/profilers.py index d09cf0c52..bee2c11a1 100644 --- a/src/bk-user/bkuser/apis/open_v2/views/profilers.py +++ b/src/bk-user/bkuser/apis/open_v2/views/profilers.py @@ -346,6 +346,7 @@ def _gen_target_lookups(self, lookup_field: str, lookup_values: List[str], is_ex domain_query = self._convert_domain_lookup_to_query(lookup_values, is_exact) return None if domain_query is None else [domain_query] + # 先将部门(3.x 版本中的租户部门)转换为 3.x 版本中的数据源部门 ID,再查询出对应的数据源用户 ID if lookup_field == "departments": department_query = self._convert_departments_lookup_to_query(lookup_values, is_exact) return None if department_query is None else [department_query] @@ -391,16 +392,17 @@ def _convert_departments_lookup_to_query(values: List[str], is_exact: bool) -> Q if not is_exact: raise error_codes.VALIDATION_ERROR.f("unsupported fuzzy lookup field: departments") - # 租户部门 ID 列表 - department_ids = [int(v) for v in values if v.isdigit()] + # 3.x 版本中的租户部门 ID 列表 + tenant_department_ids = [int(v) for v in values if v.isdigit()] # 根据租户部门 ID 查询 3.x 版本中的数据源部门 ID - data_source_department_ids = TenantDepartment.objects.filter(id__in=department_ids).values_list( + data_source_department_ids = TenantDepartment.objects.filter(id__in=tenant_department_ids).values_list( "data_source_department_id", flat=True ) # 根据数据源部门 ID 查询 3.x 版本中的数据源用户 ID data_source_user_ids = DataSourceDepartmentUserRelation.objects.filter( department_id__in=data_source_department_ids ).values_list("user_id", flat=True) + # 不存在,则说明查询不到任何用户 if not data_source_user_ids: return None diff --git a/src/bk-user/tests/apis/open_v2/test_profiles.py b/src/bk-user/tests/apis/open_v2/test_profiles.py index f23c1380e..bdd4aaff8 100644 --- a/src/bk-user/tests/apis/open_v2/test_profiles.py +++ b/src/bk-user/tests/apis/open_v2/test_profiles.py @@ -27,8 +27,8 @@ class TestListProfiles: def test_standard(self, api_client, local_data_source, collaboration_data_source): resp = api_client.get(reverse("open_v2.list_profiles"), data={"page": 1, "page_size": 10}) assert resp.status_code == status.HTTP_200_OK - assert resp.data["count"] == 22 # noqa: PLR2004 - assert len(resp.data["results"]) == 10 # noqa: PLR2004 + assert resp.data["count"] == 22 + assert len(resp.data["results"]) == 10 def test_list_with_exist_departments(self, api_client, local_data_source, collaboration_data_source): department_ids = TenantDepartment.objects.values_list("id", flat=True)