Skip to content

Commit

Permalink
minor: merge tencent/ft_tenant into local_data_source_io
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Sep 12, 2023
2 parents ce60877 + 19b5ec2 commit 64a2ff2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/apis/web/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 13 additions & 12 deletions src/bk-user/bkuser/apis/web/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,31 @@ 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"],
operation_description="租户列表",
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)

Expand Down

0 comments on commit 64a2ff2

Please sign in to comment.