Skip to content

Commit

Permalink
resolve: solve some problems
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 committed Jan 14, 2025
1 parent 9b2ccd2 commit 79b61b6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/bk-user/bkuser/apis/open_v3/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
from functools import cached_property

from apigw_manager.drf.authentication import ApiGatewayJWTAuthentication
from rest_framework.exceptions import ValidationError
from rest_framework.request import Request
Expand All @@ -25,11 +27,10 @@ class OpenApiCommonMixin:
authentication_classes = [ApiGatewayJWTAuthentication]
permission_classes = [ApiGatewayAppVerifiedPermission]


class OpenApiTenantIDMixin:
request: Request

def get_tenant_id(self):
@cached_property
def tenant_id(self) -> str:
tenant_id = self.request.headers.get("X-Bk-Tenant-Id")

if not tenant_id:
Expand Down
20 changes: 9 additions & 11 deletions src/bk-user/bkuser/apis/open_v3/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response

from bkuser.apis.open_v3.mixins import OpenApiCommonMixin, OpenApiTenantIDMixin
from bkuser.apis.open_v3.mixins import OpenApiCommonMixin
from bkuser.apis.open_v3.serializers.user import (
TenantUserDepartmentListInputSLZ,
TenantUserDepartmentListOutputSLZ,
Expand All @@ -43,7 +43,7 @@
logger = logging.getLogger(__name__)


class TenantUserDisplayNameListApi(OpenApiCommonMixin, OpenApiTenantIDMixin, generics.ListAPIView):
class TenantUserDisplayNameListApi(OpenApiCommonMixin, generics.ListAPIView):
"""
批量根据用户 bk_username 获取用户展示名
TODO: 性能较高,只查询所需字段,后续开发 DisplayName 支持表达式配置时添加 Cache 方案
Expand All @@ -61,7 +61,7 @@ def get_queryset(self):
# TODO: 由于目前 DisplayName 渲染只与 full_name 相关,所以只查询 full_name
# 后续支持表达式,则需要查询表达式可配置的所有字段
return (
TenantUser.objects.filter(id__in=data["bk_usernames"], tenant_id=self.get_tenant_id())
TenantUser.objects.filter(id__in=data["bk_usernames"], tenant_id=self.tenant_id)
.select_related("data_source_user")
.only("id", "data_source_user__full_name")
)
Expand All @@ -77,7 +77,7 @@ def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)


class TenantUserRetrieveApi(OpenApiCommonMixin, OpenApiTenantIDMixin, generics.RetrieveAPIView):
class TenantUserRetrieveApi(OpenApiCommonMixin, generics.RetrieveAPIView):
"""
根据用户 bk_username 获取用户信息
"""
Expand All @@ -93,11 +93,11 @@ class TenantUserRetrieveApi(OpenApiCommonMixin, OpenApiTenantIDMixin, generics.R
responses={status.HTTP_200_OK: TenantUserRetrieveOutputSLZ()},
)
def get(self, request, *args, **kwargs):
tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.get_tenant_id()), id=kwargs["id"])
tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.tenant_id), id=kwargs["id"])
return Response(TenantUserRetrieveOutputSLZ(tenant_user).data)


class TenantUserDepartmentListApi(OpenApiCommonMixin, OpenApiTenantIDMixin, generics.ListAPIView):
class TenantUserDepartmentListApi(OpenApiCommonMixin, generics.ListAPIView):
"""
根据用户 bk_username 获取用户所在部门列表信息(支持是否包括祖先部门)
"""
Expand All @@ -118,7 +118,7 @@ def get(self, request, *args, **kwargs):
slz.is_valid(raise_exception=True)
data = slz.validated_data

tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.get_tenant_id()), id=kwargs["id"])
tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.tenant_id), id=kwargs["id"])

return Response(
TenantUserDepartmentListOutputSLZ(self._get_dept_info(tenant_user, data["with_ancestors"]), many=True).data
Expand Down Expand Up @@ -193,7 +193,7 @@ def _get_dept_ancestors(dept_id: int) -> List[int]:
return list(relation.get_ancestors().values_list("department_id", flat=True))


class TenantUserLeaderListApi(OpenApiCommonMixin, OpenApiTenantIDMixin, generics.ListAPIView):
class TenantUserLeaderListApi(OpenApiCommonMixin, generics.ListAPIView):
"""
根据用户 bk_username 获取用户 Leader 列表信息
"""
Expand All @@ -203,9 +203,7 @@ class TenantUserLeaderListApi(OpenApiCommonMixin, OpenApiTenantIDMixin, generics
serializer_class = TenantUserLeaderListOutputSLZ

def get_queryset(self) -> QuerySet[TenantUser]:
tenant_user = get_object_or_404(
TenantUser.objects.filter(tenant_id=self.get_tenant_id()), id=self.kwargs["id"]
)
tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.tenant_id), id=self.kwargs["id"])

leader_ids = list(
DataSourceUserLeaderRelation.objects.filter(user=tenant_user.data_source_user).values_list(
Expand Down

0 comments on commit 79b61b6

Please sign in to comment.