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: added query user's list #2035

Merged
merged 13 commits into from
Jan 17, 2025
9 changes: 9 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ class TenantUserLeaderListOutputSLZ(serializers.Serializer):

def get_display_name(self, obj: TenantUser) -> str:
return TenantUserHandler.generate_tenant_user_display_name(obj)


class TenantUserListOutputSLZ(serializers.Serializer):
bk_username = serializers.CharField(help_text="蓝鲸用户唯一标识", source="id")
full_name = serializers.CharField(help_text="姓名", source="data_source_user.full_name")
display_name = serializers.SerializerMethodField(help_text="用户展示名称")

def get_display_name(self, obj: TenantUser) -> str:
return TenantUserHandler.generate_tenant_user_display_name(obj)
rolin999 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apis/open_v3/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
views.TenantDepartmentRetrieveApi.as_view(),
name="open_v3.tenant_department.retrieve",
),
path("users/", views.TenantUserListApi.as_view(), name="open_v3.tenant_user.list"),
]
),
),
Expand Down
2 changes: 2 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
TenantUserDepartmentListApi,
TenantUserDisplayNameListApi,
TenantUserLeaderListApi,
TenantUserListApi,
TenantUserRetrieveApi,
)

Expand All @@ -29,5 +30,6 @@
"TenantUserRetrieveApi",
"TenantUserDepartmentListApi",
"TenantUserLeaderListApi",
"TenantUserListApi",
"TenantDepartmentRetrieveApi",
]
29 changes: 29 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
TenantUserDisplayNameListInputSLZ,
TenantUserDisplayNameListOutputSLZ,
TenantUserLeaderListOutputSLZ,
TenantUserListOutputSLZ,
TenantUserRetrieveOutputSLZ,
)
from bkuser.apps.data_source.models import (
Expand All @@ -39,6 +40,7 @@
)
from bkuser.apps.tenant.models import TenantDepartment, TenantUser
from bkuser.biz.organization import DataSourceDepartmentHandler
from bkuser.common.pagination import CustomPageNumberPagination

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -209,3 +211,30 @@ def get_queryset(self) -> QuerySet[TenantUser]:
)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)


class TenantUserListApi(OpenApiCommonMixin, generics.ListAPIView):
"""
查询用户列表
"""

pagination_class = CustomPageNumberPagination
pagination_class.max_page_size = 1000

serializer_class = TenantUserListOutputSLZ

def get_queryset(self) -> QuerySet[TenantUser]:
return (
TenantUser.objects.select_related("data_source_user")
.filter(tenant_id=self.tenant_id)
.only("id", "data_source_user__full_name")
)

@swagger_auto_schema(
tags=["open_v3.user"],
operation_id="list_user",
operation_description="查询用户列表",
responses={status.HTTP_200_OK: TenantUserListOutputSLZ(many=True)},
)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
rolin999 marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 51 additions & 0 deletions src/bk-user/support-files/apidocs/en/list_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### Description

(Pagination) Query user's list

### 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, maximum is 1000 |

### Request Example

```
// URL Query Parameters
page=1&page_size=5
```

### Response Example for Status Code 200

```json5
{
"data": {
"count": 2,
"results": [
{
"bk_username": "q9k6bhqks0ckl5ew",
"full_name": "张三",
"display_name": "张三",
},
{
"bk_username": "er0ugcammqwf1q5w",
"full_name": "李四",
"display_name": "李四",
}
],
}
}
```

### Response Parameters Description

| Name | Type | Description |
|--------------|--------|-----------------------------------|
| bk_username | string | Blueking user's unique identifier |
| full_name | string | User's name |
| display_name | string | User's display name |

# Response Example for Non-200 Status Code

No response example
51 changes: 51 additions & 0 deletions src/bk-user/support-files/apidocs/zh/list_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### 描述

(分页)查询用户列表

### 输入参数

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

### 请求示例

```
// URL Query 参数
page=1&page_size=5
```

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

```json5
{
"data": {
"count": 2,
"results": [
{
"bk_username": "q9k6bhqks0ckl5ew",
"full_name": "张三",
"display_name": "张三",
},
{
"bk_username": "er0ugcammqwf1q5w",
"full_name": "李四",
"display_name": "李四",
}
],
}
}
```

### 响应参数说明

| 参数名称 | 参数类型 | 描述 |
|--------------|--------|----------|
| bk_username | string | 蓝鲸用户唯一标识 |
| full_name | string | 用户姓名 |
| display_name | string | 用户展示名 |

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

暂无
rolin999 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -156,3 +156,28 @@ paths:
appVerifiedRequired: true
resourcePermissionRequired: true
descriptionEn: Query information of the department

/api/v3/open/tenant/users/:
get:
operationId: list_user
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/users/
matchSubpath: false
timeout: 0
pluginConfigs: []
authConfig:
userVerifiedRequired: false
appVerifiedRequired: true
resourcePermissionRequired: true
descriptionEn: (Pagination) Query user's list
36 changes: 36 additions & 0 deletions src/bk-user/tests/apis/open_v3/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,39 @@ def test_with_no_leader(self, api_client):
def test_with_invalid_user(self, api_client):
resp = api_client.get(reverse("open_v3.tenant_user.leader.list", kwargs={"id": "a1e5b2f6c3g7d4h8"}))
assert resp.status_code == status.HTTP_404_NOT_FOUND


@pytest.mark.usefixtures("_init_tenant_users_depts")
class TestTenantUserListApi:
def test_standard(self, api_client, random_tenant):
resp = api_client.get(reverse("open_v3.tenant_user.list"), data={"page": 1, "page_size": 11})
assert resp.status_code == status.HTTP_200_OK
assert resp.data["count"] == 11
assert len(resp.data["results"]) == 11
assert all("bk_username" in t for t in resp.data["results"])
assert {t["full_name"] for t in resp.data["results"]} == {
"张三",
"李四",
"王五",
"赵六",
"柳七",
"麦八",
"杨九",
"鲁十",
"林十一",
"白十二",
"自由人",
}
assert {t["display_name"] for t in resp.data["results"]} == {
"张三",
"李四",
"王五",
"赵六",
"柳七",
"麦八",
"杨九",
"鲁十",
"林十一",
"白十二",
"自由人",
}
Loading