From 19b5ec2195d753f79dcbed0cad22edd4631f04e0 Mon Sep 17 00:00:00 2001 From: neronkl <49228807+neronkl@users.noreply.github.com> Date: Tue, 12 Sep 2023 10:32:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(organization):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E6=B2=A1=E6=9C=89=E5=8D=8F=E5=90=8C=E5=85=B3?= =?UTF-8?q?=E7=B3=BB=E7=9A=84=E7=A7=9F=E6=88=B7=20(#1223)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bk-user/bkuser/apis/web/mixins.py | 2 +- .../bkuser/apis/web/organization/views.py | 25 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/bk-user/bkuser/apis/web/mixins.py b/src/bk-user/bkuser/apis/web/mixins.py index 4e85a761e..f4b6a903f 100644 --- a/src/bk-user/bkuser/apis/web/mixins.py +++ b/src/bk-user/bkuser/apis/web/mixins.py @@ -18,7 +18,7 @@ class CurrentUserTenantMixin: request: Request - def get_current_tenant_id(self): + def get_current_tenant_id(self) -> str: tenant_id = self.request.user.get_property("tenant_id") if not tenant_id: raise error_codes.GET_CURRENT_TENANT_FAILED diff --git a/src/bk-user/bkuser/apis/web/organization/views.py b/src/bk-user/bkuser/apis/web/organization/views.py index a7937a192..1e5bdd725 100644 --- a/src/bk-user/bkuser/apis/web/organization/views.py +++ b/src/bk-user/bkuser/apis/web/organization/views.py @@ -113,15 +113,6 @@ def get(self, request, *args, **kwargs): class TenantListApi(CurrentUserTenantMixin, generics.ListAPIView): pagination_class = None - queryset = Tenant.objects.all() - serializer_class = TenantListOutputSLZ - - def get_serializer_context(self): - tenant_ids = list(self.queryset.values_list("id", flat=True)) - tenant_root_departments_map = TenantDepartmentHandler.get_tenant_root_department_map_by_tenant_id( - tenant_ids, self.get_current_tenant_id() - ) - return {"tenant_root_departments_map": tenant_root_departments_map} @swagger_auto_schema( tags=["tenant-organization"], @@ -129,14 +120,24 @@ def get_serializer_context(self): responses={status.HTTP_200_OK: TenantListOutputSLZ(many=True)}, ) def get(self, request, *args, **kwargs): - queryset = self.filter_queryset(self.get_queryset()) - # 将当前租户置顶 current_tenant_id: str = self.get_current_tenant_id() + # 获取当前租户以及有协同关系的租户 + # TODO 过滤出与当前租户有协同关系的租户 + queryset = Tenant.objects.filter(id__in=[current_tenant_id]) + + # 将当前租户置顶 # 通过比对租户id, 当等于当前登录用户的租户id,将其排序到查询集的顶部, 否则排序到查询集的底部 sorted_queryset = sorted(queryset, key=lambda t: t.id != current_tenant_id) - serializer = self.get_serializer(sorted_queryset, many=True) + # 获取租户根组织 + tenant_root_departments_map = TenantDepartmentHandler.get_tenant_root_department_map_by_tenant_id( + list(queryset.values_list("id", flat=True)), current_tenant_id + ) + + serializer = TenantListOutputSLZ( + sorted_queryset, many=True, context={"tenant_root_departments_map": tenant_root_departments_map} + ) return Response(serializer.data)