Skip to content

Commit

Permalink
code conflict 解决
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Aug 23, 2023
1 parent 608114a commit e9c22d5
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 8 deletions.
107 changes: 103 additions & 4 deletions src/bk-user/bkuser/apis/web/organization/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import logging
from typing import Dict, List

from django.conf import settings
from drf_yasg.utils import swagger_serializer_method
from rest_framework import serializers

from bkuser.apps.tenant.models import Tenant

logger = logging.getLogger(__name__)
from bkuser.apps.tenant.models import Tenant, TenantUser
from bkuser.biz.tenant import TenantUserHandler
from bkuser.common.serializers import PagePageNumberInputSLZ


class TenantDepartmentOutputSLZ(serializers.Serializer):
Expand All @@ -25,6 +25,105 @@ class TenantDepartmentOutputSLZ(serializers.Serializer):
has_children = serializers.BooleanField(help_text="是否有子部门")


class TenantUserDepartmentOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(help_text="租户用户ID")
name = serializers.CharField(help_text="租户部门名称")


class TenantUserLeaderOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(help_text="租户用户ID")
username = serializers.CharField(help_text="租户用户名")
full_name = serializers.CharField(help_text="租户名称")


class TenantDepartmentUserSearchInputSLZ(PagePageNumberInputSLZ):
recursive = serializers.BooleanField(help_text="包含子部门的人员", default=False)
keyword = serializers.CharField(help_text="搜索关键字", required=False)


class TenantUserInfoOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="租户用户ID")
username = serializers.CharField(help_text="租户用户名", required=False)
full_name = serializers.CharField(help_text="用户姓名", required=False)
email = serializers.EmailField(help_text="用户邮箱", required=False)
phone = serializers.CharField(help_text="用户手机号", required=False)
phone_country_code = serializers.CharField(
help_text="手机号国际区号", required=False, default=settings.DEFAULT_PHONE_COUNTRY_CODE
)
account_expired_at = serializers.DateTimeField(help_text="账号过期时间")
departments = serializers.SerializerMethodField(help_text="用户所属部门")
leaders = serializers.SerializerMethodField(help_text="用户上级")


class TenantDepartmentUserListOutputSLZ(TenantUserInfoOutputSLZ):
@swagger_serializer_method(serializer_or_field=TenantUserDepartmentOutputSLZ(many=True))
def get_departments(self, instance: TenantUser) -> List[Dict]:
departments = self.context["tenant_user_departments"].get(instance.id)
if not departments:
return []
return [{"id": i.id, "name": i.name} for i in departments]

@swagger_serializer_method(serializer_or_field=TenantUserLeaderOutputSLZ(many=True))
def get_leaders(self, instance: TenantUser) -> List[Dict]:
leader_infos = self.context["tenant_user_leaders"].get(instance.id)
if not leader_infos:
return []
return [{"id": i.id, "username": i.username, "full_name": i.full_name} for i in leader_infos]

def to_representation(self, instance: TenantUser) -> Dict:
data = super().to_representation(instance)
user_info = self.context["tenant_users_info"].get(instance.id)
if user_info is not None:
user = user_info.data_source_user
data.update(
{
"full_name": user.full_name,
"username": user.username,
"email": user.email,
"phone": user.phone,
"phone_country_code": user.phone_country_code,
"logo": user.logo or settings.DEFAULT_DATA_SOURCE_USER_LOGO,
}
)
return data


class TenantUserRetrieveOutputSLZ(TenantUserInfoOutputSLZ):
@swagger_serializer_method(serializer_or_field=TenantUserDepartmentOutputSLZ(many=True))
def get_departments(self, instance: TenantUser) -> List[Dict]:
tenant_user_departments = TenantUserHandler.get_tenant_user_departments_map_by_id([instance.id])
departments = tenant_user_departments.get(instance.id) or []
return [{"id": i.id, "name": i.name} for i in departments]

@swagger_serializer_method(serializer_or_field=TenantUserLeaderOutputSLZ(many=True))
def get_leaders(self, instance: TenantUser) -> List[Dict]:
leaders = TenantUserHandler.get_tenant_user_leaders_map_by_id([instance.id]).get(instance.id) or []
return [
{
"id": i.id,
"username": i.username,
"full_name": i.full_name,
}
for i in leaders
]

def to_representation(self, instance: TenantUser) -> Dict:
data = super().to_representation(instance)
user = instance.data_source_user
if user is not None:
data.update(
{
"full_name": user.full_name,
"username": user.username,
"email": user.email,
"phone": user.phone,
"phone_country_code": user.phone_country_code,
"logo": user.logo or settings.DEFAULT_DATA_SOURCE_USER_LOGO,
}
)
return data


class TenantListOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="租户ID")
name = serializers.CharField(help_text="租户名称")
Expand Down
3 changes: 3 additions & 0 deletions src/bk-user/bkuser/apis/web/organization/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from . import views

urlpatterns = [
# 租户用户
path("departments/<int:id>/users/", views.TenantDepartmentUserListApi.as_view(), name="departments.users.list"),
path("users/<str:id>/", views.TenantUsersRetrieveApi.as_view(), name="department.users.retrieve"),
# 租户
path("tenants/", views.TenantListApi.as_view(), name="organization.tenant.list"),
path("tenants/<str:id>/", views.TenantRetrieveUpdateApi.as_view(), name="organization.tenant.retrieve_update"),
Expand Down
83 changes: 81 additions & 2 deletions src/bk-user/bkuser/apis/web/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,104 @@
"""
import logging

from django.db.models import Q
from drf_yasg.utils import swagger_auto_schema
from rest_framework import generics, status
from rest_framework.response import Response

from bkuser.apis.web.organization.serializers import TenantDepartmentChildrenListOutputSLZ, TenantListOutputSLZ
from bkuser.apis.web.organization.serializers import (
TenantDepartmentChildrenListOutputSLZ,
TenantDepartmentUserListOutputSLZ,
TenantDepartmentUserSearchInputSLZ,
TenantListOutputSLZ,
TenantUserRetrieveOutputSLZ,
)
from bkuser.apis.web.tenant.serializers import TenantRetrieveOutputSLZ, TenantUpdateInputSLZ
from bkuser.apps.tenant.models import Tenant
from bkuser.apps.tenant.models import Tenant, TenantUser
from bkuser.biz.tenant import (
TenantDepartmentHandler,
TenantEditableBaseInfo,
TenantFeatureFlag,
TenantHandler,
TenantUserHandler,
)
from bkuser.common.error_codes import error_codes
from bkuser.common.pagination import CustomPageNumberPagination
from bkuser.common.views import ExcludePatchAPIViewMixin

logger = logging.getLogger(__name__)


class TenantDepartmentUserListApi(generics.ListAPIView):
queryset = TenantUser.objects.all()
lookup_url_kwarg = "id"
pagination_class = CustomPageNumberPagination
serializer_class = TenantDepartmentUserListOutputSLZ

def get_serializer_context(self):
# 过滤出该租户部门(包括子部门)的租户用户
tenant_user_ids = TenantUserHandler.get_tenant_user_ids_by_tenant_department(
tenant_department_id=self.kwargs["id"], recursive=self.request.query_params.get("recursive", True)
)

# 租户用户基础信息
tenant_users = TenantUserHandler.list_tenant_user_by_id(tenant_user_ids)
tenant_users_info_map = {i.id: i for i in tenant_users}

# 租户用户所属租户组织
tenant_user_departments_map = TenantUserHandler.get_tenant_user_departments_map_by_id(tenant_user_ids)

# 租户用户上级信息
tenant_user_leaders_map = TenantUserHandler.get_tenant_user_leaders_map_by_id(tenant_user_ids)
return {
"tenant_users_info": tenant_users_info_map,
"tenant_user_departments": tenant_user_departments_map,
"tenant_user_leaders": tenant_user_leaders_map,
}

@swagger_auto_schema(
operation_description="租户部门下用户详情列表",
responses={status.HTTP_200_OK: TenantDepartmentUserListOutputSLZ(many=True)},
)
def get(self, request, *args, **kwargs):
slz = TenantDepartmentUserSearchInputSLZ(data=self.request.query_params)
slz.is_valid(raise_exception=True)
data = slz.validated_data
# 过滤该租户部门下的用户
tenant_user_ids = TenantUserHandler.get_tenant_user_ids_by_tenant_department(
tenant_department_id=self.kwargs["id"], recursive=data.get("recursive")
)

# build response
queryset = self.filter_queryset(self.get_queryset().filter(id__in=tenant_user_ids))
if keyword := data.get("keyword"):
queryset = queryset.select_related("data_source_user").filter(
Q(data_source_user__username__icontains=keyword)
| Q(data_source_user__email__icontains=keyword)
| Q(data_source_user__phone__icontains=keyword),
)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)


class TenantUsersRetrieveApi(generics.RetrieveAPIView):
queryset = TenantUser.objects.all()
lookup_url_kwarg = "id"
serializer_class = TenantUserRetrieveOutputSLZ

@swagger_auto_schema(
operation_description="租户部门下单个用户详情",
responses={status.HTTP_200_OK: TenantUserRetrieveOutputSLZ()},
)
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)


class TenantListApi(generics.ListAPIView):
pagination_class = None
queryset = Tenant.objects.all()
Expand Down
24 changes: 23 additions & 1 deletion src/bk-user/bkuser/biz/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

from pydantic import BaseModel

from bkuser.apps.data_source.models import DataSource, DataSourceDepartment, DataSourceDepartmentRelation
from bkuser.apps.data_source.models import (
DataSource,
DataSourceDepartment,
DataSourceDepartmentRelation,
DataSourceDepartmentUserRelation,
)


class DataSourceDepartmentInfoWithChildren(BaseModel):
Expand Down Expand Up @@ -65,3 +70,20 @@ def get_department_info_map_by_id(department_ids: List[int]) -> Dict[int, DataSo
),
)
return departments_map

@staticmethod
def get_user_ids_by_department_id(department_id: int, recursive: bool = True) -> List[str]:
# 是否返回子部门用户
if not recursive:
user_ids = DataSourceDepartmentUserRelation.objects.filter(department_id=department_id).values_list(
"user_id"
)
else:
department = DataSourceDepartmentRelation.objects.get(department_id=department_id)
recursive_department_ids = department.get_descendants(include_self=True).values_list(
"department_id", flat=True
)
user_ids = DataSourceDepartmentUserRelation.objects.filter(
department_id__in=recursive_department_ids
).values_list("user_id")
return list(user_ids)
87 changes: 86 additions & 1 deletion src/bk-user/bkuser/biz/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
from django.db import transaction
from pydantic import BaseModel

from bkuser.apps.data_source.models import DataSource, DataSourceDepartmentRelation, DataSourcePlugin, DataSourceUser
from bkuser.apps.data_source.models import (
DataSource,
DataSourceDepartmentRelation,
DataSourceDepartmentUserRelation,
DataSourcePlugin,
DataSourceUser,
DataSourceUserLeaderRelation,
)
from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantManager, TenantUser
from bkuser.biz.data_source import DataSourceDepartmentHandler, DataSourceHandler, DataSourceSimpleInfo
from bkuser.utils.uuid import generate_uuid
Expand All @@ -28,6 +35,7 @@ class DataSourceUserInfo(BaseModel):
email: str
phone: str
phone_country_code: str
logo: str


class TenantUserWithInheritedInfo(BaseModel):
Expand Down Expand Up @@ -74,6 +82,12 @@ class TenantDepartmentBaseInfo(BaseModel):
has_children: bool


class TenantUserLeaderInfo(BaseModel):
id: str
username: str
full_name: str


class TenantUserHandler:
@staticmethod
def list_tenant_user_by_id(tenant_user_ids: List[str]) -> List[TenantUserWithInheritedInfo]:
Expand All @@ -99,12 +113,83 @@ def list_tenant_user_by_id(tenant_user_ids: List[str]) -> List[TenantUserWithInh
email=data_source_user.email,
phone=data_source_user.phone,
phone_country_code=data_source_user.phone_country_code,
logo=data_source_user.logo,
),
)
)

return data

@staticmethod
def get_tenant_user_leaders_map_by_id(tenant_user_ids: List[str]) -> Dict[str, List[TenantUserLeaderInfo]]:
tenant_users = TenantUser.objects.filter(id__in=tenant_user_ids)
# 从数据源中获取租户用户每个上级
data_source_leaders = DataSourceUserLeaderRelation.objects.filter(
user_id__in=tenant_users.values_list("data_source_user_id")
)
# 数据源上下级关系映射
data_source_leaders_map: Dict[int, List[int]] = {}
for item in data_source_leaders:
if item.user_id in data_source_leaders_map:
data_source_leaders_map[item.user_id].append(item.leader_id)
else:
data_source_leaders_map[item.user_id] = [item.leader_id]

# NOTE:如果用户通过协同数据源而来,被授权的上级一定会绑定该租户
tenant_user_leaders = TenantUser.objects.select_related("data_source_user").filter(
data_source_user_id__in=data_source_leaders.values_list("leader_id", flat=True),
tenant_id=tenant_users.first().tenant_id,
)
# 构建数据源上级id-租户上级数据映射
data_source_leader_convert_tenant_user_map: Dict[int, TenantUserLeaderInfo] = {}
for leader in tenant_user_leaders:
user = leader.data_source_user
data_source_leader_convert_tenant_user_map[leader.data_source_user_id] = TenantUserLeaderInfo(
id=leader.id,
username=user.username,
full_name=user.full_name,
)

# 租户用户-租户用户上级数据整合
tenant_user_leaders_map: Dict = {}
for item in tenant_users:
data_source_leaders = data_source_leaders_map.get(item.data_source_user_id)
if not data_source_leaders:
continue
data = [
data_source_leader_convert_tenant_user_map[i]
for i in data_source_leaders
if data_source_leader_convert_tenant_user_map.get(i)
]
tenant_user_leaders_map[item.id] = data
return tenant_user_leaders_map

@staticmethod
def get_tenant_user_departments_map_by_id(tenant_user_ids: List[str]) -> Dict[str, List[TenantDepartmentBaseInfo]]:
tenant_users = TenantUser.objects.select_related("data_source_user").filter(id__in=tenant_user_ids)
data: Dict = {}
for tenant_user in tenant_users:
departments = DataSourceDepartmentUserRelation.objects.filter(user=tenant_user.data_source_user)
if not departments.exists():
continue
department_ids = list(departments.values_list("department_id", flat=True))
tenant_department_infos = TenantDepartmentHandler.convert_data_source_department_to_tenant_department(
tenant_id=tenant_user.tenant_id, data_source_department_ids=department_ids
)

data[tenant_user.id] = tenant_department_infos
return data

@staticmethod
def get_tenant_user_ids_by_tenant_department(tenant_department_id: int, recursive: bool = True):
tenant_department = TenantDepartment.objects.get(id=tenant_department_id)
data_source_user_ids = DataSourceDepartmentHandler.get_user_ids_by_department_id(
department_id=tenant_department.data_source_department_id, recursive=recursive
)
return list(
TenantUser.objects.filter(data_source_user_id__in=data_source_user_ids).values_list("id", flat=True)
)


class TenantHandler:
@staticmethod
Expand Down
Loading

0 comments on commit e9c22d5

Please sign in to comment.