From eff46af5c1804f81e2a2b6193e5fc0836f878a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E4=BD=A9=E7=8F=8A=5Bshiisa=5D?= Date: Tue, 22 Aug 2023 17:01:52 +0800 Subject: [PATCH] feat: list data source departments #1162 --- .../bkuser/apis/web/data_source/__init__.py | 10 ++++ .../apis/web/data_source/serializers.py | 20 ++++++++ .../bkuser/apis/web/data_source/urls.py | 17 +++++++ .../bkuser/apis/web/data_source/views.py | 47 +++++++++++++++++++ src/bk-user/bkuser/apis/web/urls.py | 1 + src/bk-user/bkuser/common/error_codes.py | 1 + 6 files changed, 96 insertions(+) create mode 100644 src/bk-user/bkuser/apis/web/data_source/__init__.py create mode 100644 src/bk-user/bkuser/apis/web/data_source/serializers.py create mode 100644 src/bk-user/bkuser/apis/web/data_source/urls.py create mode 100644 src/bk-user/bkuser/apis/web/data_source/views.py diff --git a/src/bk-user/bkuser/apis/web/data_source/__init__.py b/src/bk-user/bkuser/apis/web/data_source/__init__.py new file mode 100644 index 000000000..1060b7bf4 --- /dev/null +++ b/src/bk-user/bkuser/apis/web/data_source/__init__.py @@ -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. +""" diff --git a/src/bk-user/bkuser/apis/web/data_source/serializers.py b/src/bk-user/bkuser/apis/web/data_source/serializers.py new file mode 100644 index 000000000..bec9ae0ba --- /dev/null +++ b/src/bk-user/bkuser/apis/web/data_source/serializers.py @@ -0,0 +1,20 @@ +# -*- 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 rest_framework import serializers + + +class DepartmentSearchInputSLZ(serializers.Serializer): + name = serializers.CharField(required=False, help_text="部门名称", allow_blank=True) + + +class DepartmentSearchOutputSLZ(serializers.Serializer): + id = serializers.CharField(help_text="部门ID") + name = serializers.CharField(help_text="部门名称") diff --git a/src/bk-user/bkuser/apis/web/data_source/urls.py b/src/bk-user/bkuser/apis/web/data_source/urls.py new file mode 100644 index 000000000..04869bfa1 --- /dev/null +++ b/src/bk-user/bkuser/apis/web/data_source/urls.py @@ -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("/departments/", views.DataSourceDepartmentsListApi.as_view(), name="data_source_departments.list"), +] diff --git a/src/bk-user/bkuser/apis/web/data_source/views.py b/src/bk-user/bkuser/apis/web/data_source/views.py new file mode 100644 index 000000000..7cba18349 --- /dev/null +++ b/src/bk-user/bkuser/apis/web/data_source/views.py @@ -0,0 +1,47 @@ +# -*- 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 DepartmentSearchInputSLZ, DepartmentSearchOutputSLZ +from bkuser.apps.data_source.models import DataSource, DataSourceDepartment +from bkuser.common.error_codes import error_codes + + +class DataSourceDepartmentsListApi(generics.ListAPIView): + serializer_class = DepartmentSearchOutputSLZ + + def get_queryset(self): + data_source_id = self.kwargs["id"] + slz = DepartmentSearchInputSLZ(data=self.request.query_params) + slz.is_valid(raise_exception=True) + data = slz.validated_data + + # 校验数据源是否存在 + try: + data_source = DataSource.objects.get(id=data_source_id) + except Exception: + raise error_codes.DATA_SOURCE_NOT_EXIST + + queryset = DataSourceDepartment.objects.filter(data_source=data_source) + + if name := data.get("name"): + queryset = queryset.filter(name__icontains=name) + + return queryset + + @swagger_auto_schema( + operation_description="数据源部门列表", + query_serializer=DepartmentSearchInputSLZ(), + responses={status.HTTP_200_OK: DepartmentSearchOutputSLZ(many=True)}, + ) + def get(self, request, *args, **kwargs): + return self.list(request, *args, **kwargs) diff --git a/src/bk-user/bkuser/apis/web/urls.py b/src/bk-user/bkuser/apis/web/urls.py index 3a11320cf..41823f0e2 100644 --- a/src/bk-user/bkuser/apis/web/urls.py +++ b/src/bk-user/bkuser/apis/web/urls.py @@ -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")), ] diff --git a/src/bk-user/bkuser/common/error_codes.py b/src/bk-user/bkuser/common/error_codes.py index b3d7a6c91..3d290c44b 100644 --- a/src/bk-user/bkuser/common/error_codes.py +++ b/src/bk-user/bkuser/common/error_codes.py @@ -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(_("租户更新失败"))