Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list data source users #1159 #1170

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
40 changes: 40 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
from drf_yasg.utils import swagger_serializer_method
from rest_framework import serializers

from bkuser.apps.data_source.models import DataSourceDepartmentUserRelation, DataSourceUser


class DataSourceSearchDepartmentsOutputSchema(serializers.Serializer):
Canway-shiisa marked this conversation as resolved.
Show resolved Hide resolved
id = serializers.CharField(help_text="部门ID")
name = serializers.CharField(help_text="部门名称")


class UserSearchInputSLZ(serializers.Serializer):
username = serializers.CharField(required=False, help_text="用户名", allow_blank=True)


@swagger_serializer_method(serializer_or_field=DataSourceSearchDepartmentsOutputSchema(many=True))
class UserSearchOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="用户ID")
username = serializers.CharField(help_text="用户名")
full_name = serializers.CharField(help_text="全名")
phone = serializers.CharField(help_text="手机号")
email = serializers.CharField(help_text="邮箱")
departments = serializers.SerializerMethodField(help_text="用户部门")

# TODO:考虑抽象一个函数 获取数据后传递到context
Canway-shiisa marked this conversation as resolved.
Show resolved Hide resolved
def get_departments(self, obj: DataSourceUser):
Canway-shiisa marked this conversation as resolved.
Show resolved Hide resolved
return [
{"id": department_user_relation.department.id, "name": department_user_relation.department.name}
for department_user_relation in DataSourceDepartmentUserRelation.objects.filter(user=obj)
]
Canway-shiisa marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
from django.urls import path

from bkuser.apis.web.data_source import views

urlpatterns = [
path("<int:id>/users/", views.DataSourceUserListCreateApi.as_view(), name="data_source_user.list_create"),
]
49 changes: 49 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
from drf_yasg.utils import swagger_auto_schema
from rest_framework import generics, status

from bkuser.apis.web.data_source.serializers import UserSearchInputSLZ, UserSearchOutputSLZ
from bkuser.apps.data_source.models import DataSource, DataSourceUser
from bkuser.common.error_codes import error_codes
from bkuser.common.pagination import CustomPageNumberPagination


class DataSourceUserListCreateApi(generics.ListCreateAPIView):
pagination_class = CustomPageNumberPagination
Canway-shiisa marked this conversation as resolved.
Show resolved Hide resolved
serializer_class = UserSearchOutputSLZ

def get_queryset(self):
slz = UserSearchInputSLZ(data=self.request.query_params)
slz.is_valid(raise_exception=True)
data = slz.validated_data
data_source_id = self.kwargs["id"]

# 校验数据源是否存在
try:
data_source = DataSource.objects.get(id=data_source_id)
except Exception:
raise error_codes.DATA_SOURCE_NOT_EXIST
Canway-shiisa marked this conversation as resolved.
Show resolved Hide resolved

queryset = DataSourceUser.objects.filter(data_source=data_source)

if data.get("username"):
queryset = DataSourceUser.objects.filter(username__icontains=data["username"])

return queryset

@swagger_auto_schema(
operation_description="数据源用户列表",
query_serializer=UserSearchInputSLZ(),
responses={status.HTTP_200_OK: UserSearchOutputSLZ(many=True)},
)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
Canway-shiisa marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apis/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
path("basic/", include("bkuser.apis.web.basic.urls")),
# 租户
path("tenants/", include("bkuser.apis.web.tenant.urls")),
path("data-sources/", include("bkuser.apis.web.data_source.urls")),
]
1 change: 1 addition & 0 deletions src/bk-user/bkuser/common/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ErrorCodes:
REMOTE_REQUEST_ERROR = ErrorCode(_("调用外部系统API异常"))
# 数据源
DATA_SOURCE_TYPE_NOT_SUPPORTED = ErrorCode(_("数据源类型不支持"))
DATA_SOURCE_NOT_EXIST = ErrorCode(_("数据源不存在"))
# 租户
CREATE_TENANT_FAILED = ErrorCode(_("租户创建失败"))
UPDATE_TENANT_FAILED = ErrorCode(_("租户更新失败"))
Expand Down