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 21, 2025
2 parents 614710d + f21bcae commit 8370ea6
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/serializers/department.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class TenantDepartmentRetrieveOutputSLZ(serializers.Serializer):
ancestors = serializers.ListField(help_text="祖先部门列表", required=False, child=AncestorSLZ(), allow_empty=True)


class TenantDepartmentListOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(help_text="部门 ID")
name = serializers.CharField(help_text="部门名称", source="data_source_department.name")
parent_id = serializers.SerializerMethodField(help_text="父部门 ID", allow_null=True)

def get_parent_id(self, obj: TenantDepartment) -> int | None:
return self.context["parent_id_map"].get(obj.id)


class TenantDepartmentDescendantListInputSLZ(serializers.Serializer):
level = serializers.IntegerField(help_text="递归子部门的相对 Level 层级", required=False, default=1, min_value=1)

Expand Down
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
name="open_v3.tenant_department.retrieve",
),
path("users/", views.TenantUserListApi.as_view(), name="open_v3.tenant_user.list"),
path(
"departments/",
views.TenantDepartmentListApi.as_view(),
name="open_v3.tenant_department.list",
),
path(
"departments/<int:id>/descendants/",
views.TenantDepartmentDescendantListApi.as_view(),
Expand Down
3 changes: 2 additions & 1 deletion src/bk-user/bkuser/apis/open_v3/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
# 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 .department import TenantDepartmentDescendantListApi, TenantDepartmentRetrieveApi
from .department import TenantDepartmentDescendantListApi, TenantDepartmentListApi, TenantDepartmentRetrieveApi
from .tenant import TenantListApi
from .user import (
TenantUserDepartmentListApi,
Expand All @@ -33,4 +33,5 @@
"TenantUserListApi",
"TenantDepartmentRetrieveApi",
"TenantDepartmentDescendantListApi",
"TenantDepartmentListApi",
]
26 changes: 26 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/views/department.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from bkuser.apis.open_v3.serializers.department import (
TenantDepartmentDescendantListInputSLZ,
TenantDepartmentDescendantListOutputSLZ,
TenantDepartmentListOutputSLZ,
TenantDepartmentRetrieveInputSLZ,
TenantDepartmentRetrieveOutputSLZ,
)
Expand Down Expand Up @@ -72,6 +73,31 @@ def get(self, request, *args, **kwargs):
return Response(TenantDepartmentRetrieveOutputSLZ(info).data)


class TenantDepartmentListApi(OpenApiCommonMixin, generics.ListAPIView):
"""
获取部门列表
"""

@swagger_auto_schema(
tags=["open_v3.department"],
operation_id="list_department",
operation_description="查询部门列表",
responses={status.HTTP_200_OK: TenantDepartmentListOutputSLZ(many=True)},
)
def get(self, request, *args, **kwargs):
depts = TenantDepartment.objects.select_related("data_source_department").filter(tenant=self.tenant_id)

# 分页
page = self.paginate_queryset(depts)

# 查询 parent
parent_id_map = TenantDepartmentHandler.get_tenant_department_parent_id_map(self.tenant_id, page)

return self.get_paginated_response(
TenantDepartmentListOutputSLZ(page, many=True, context={"parent_id_map": parent_id_map}).data
)


class TenantDepartmentDescendantListApi(OpenApiCommonMixin, generics.ListAPIView):
"""
获取部门下的子部门列表信息(支持递归)
Expand Down
47 changes: 47 additions & 0 deletions src/bk-user/support-files/apidocs/en/list_department.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
### Description

(Pagination) Query list of departments

### Parameters

| Name | Type | Required | Description |
|-----------|------|----------|---------------------------------------------|
| page | int | No | Page number, default is 1 |
| page_size | int | No | The number of pages per page, default is 10 |

### Request Example

```
// URL Query Parameters
page=2&page_size=2
```

### Response Example for Status Code 200

```json5
{
"data": {
"count": 2,
"results": [
{
"id": 3,
"name": "部门B",
"parent_id": 1,
},
{
"id": 4,
"name": "中心AA",
"parent_id": 2,
}
],
}
}
```

### Response Parameters Description

| Name | Type | Description |
|-----------|--------|-------------------------------------|
| id | int | Unique identifier of the department |
| name | string | The name of the department |
| parent_id | int | The parent department ID |
47 changes: 47 additions & 0 deletions src/bk-user/support-files/apidocs/zh/list_department.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
### 描述

(分页)查询部门列表

### 输入参数

| 参数名称 | 参数类型 | 必选 | 描述 |
|-----------|------|----|-------------|
| page | int || 页码,从 1 开始 |
| page_size | int || 每页数量,默认为 10 |

### 请求示例

```
// URL Query 参数
page=2&page_size=2
```

### 状态码 200 的响应示例

```json5
{
"data": {
"count": 2,
"results": [
{
"id": 3,
"name": "部门B",
"parent_id": 1,
},
{
"id": 4,
"name": "中心AA",
"parent_id": 2,
}
],
}
}
```

### 响应参数说明

| 参数名称 | 参数类型 | 描述 |
|-----------|--------|--------|
| id | int | 部门唯一标识 |
| name | string | 部门名称 |
| parent_id | int | 父部门 ID |
25 changes: 25 additions & 0 deletions src/bk-user/support-files/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ paths:
resourcePermissionRequired: true
descriptionEn: (Pagination) Query user's list

/api/v3/open/tenant/departments/:
get:
operationId: list_department
description: 查询部门列表
tags: []
responses:
default:
description: ''
x-bk-apigateway-resource:
isPublic: true
allowApplyPermission: false
matchSubpath: false
backend:
name: default
method: get
path: /api/v3/open/tenant/departments/
matchSubpath: false
timeout: 0
pluginConfigs: []
authConfig:
userVerifiedRequired: false
appVerifiedRequired: true
resourcePermissionRequired: true
descriptionEn: Query list of the departments

/api/v3/open/tenant/departments/{department_id}/descendants/:
get:
operationId: list_department_descendant
Expand Down
53 changes: 53 additions & 0 deletions src/bk-user/tests/apis/open_v3/test_department.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,59 @@ def test_with_not_found(self, api_client):
assert resp.status_code == status.HTTP_404_NOT_FOUND


@pytest.mark.usefixtures("_init_tenant_users_depts")
class TestTenantDepartmentListApi:
def test_standard(self, api_client):
company = TenantDepartment.objects.get(data_source_department__name="公司")
dept_a = TenantDepartment.objects.get(data_source_department__name="部门A")
dept_b = TenantDepartment.objects.get(data_source_department__name="部门B")
center_aa = TenantDepartment.objects.get(data_source_department__name="中心AA")
center_ab = TenantDepartment.objects.get(data_source_department__name="中心AB")
center_ba = TenantDepartment.objects.get(data_source_department__name="中心BA")
group_aaa = TenantDepartment.objects.get(data_source_department__name="小组AAA")
group_aba = TenantDepartment.objects.get(data_source_department__name="小组ABA")
group_baa = TenantDepartment.objects.get(data_source_department__name="小组BAA")

resp = api_client.get(reverse("open_v3.tenant_department.list"))

assert resp.status_code == status.HTTP_200_OK
assert resp.data["count"] == 9
assert len(resp.data["results"]) == 9
assert [x["id"] for x in resp.data["results"]] == [
company.id,
dept_a.id,
dept_b.id,
center_aa.id,
center_ab.id,
center_ba.id,
group_aaa.id,
group_aba.id,
group_baa.id,
]
assert [x["name"] for x in resp.data["results"]] == [
"公司",
"部门A",
"部门B",
"中心AA",
"中心AB",
"中心BA",
"小组AAA",
"小组ABA",
"小组BAA",
]
assert [x["parent_id"] for x in resp.data["results"]] == [
None,
company.id,
company.id,
dept_a.id,
dept_a.id,
dept_b.id,
center_aa.id,
center_ab.id,
center_ba.id,
]


@pytest.mark.usefixtures("_init_tenant_users_depts")
class TestTenantDepartmentDescendantListApi:
def test_with_not_level(self, api_client):
Expand Down

0 comments on commit 8370ea6

Please sign in to comment.