Skip to content

Commit

Permalink
resolve: added pagination for queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 committed Nov 20, 2024
1 parent a5608be commit 88ba9db
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 73 deletions.
13 changes: 4 additions & 9 deletions src/bk-user/bkuser/apis/web/audit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,13 @@ class AuditRecordListAPIView(CurrentUserTenantMixin, generics.ListAPIView):

permission_classes = [IsAuthenticated, perm_class(PermAction.MANAGE_TENANT)]

pagination_class = None

serializer_class = AuditRecordListOutputSLZ

def get_queryset(self):
slz = AuditRecordListInputSLZ(data=self.request.query_params)
slz.is_valid(raise_exception=True)
params = slz.validated_data

# 分页所需参数
page = int(self.request.query_params.get("page", 1))
page_size = int(self.request.query_params.get("page_size", 10))
offset = (page - 1) * page_size

filters = {
"tenant_id": self.get_current_tenant_id(),
}
Expand All @@ -69,10 +62,12 @@ def get_queryset(self):
if object_name := params.get("object_name"):
filters["object_name__icontains"] = object_name

return OperationAuditRecord.objects.filter(**filters)[offset : offset + page_size]
return OperationAuditRecord.objects.filter(**filters)

def get_serializer_context(self) -> Dict[str, Any]:
tenant_user_ids = list(self.get_queryset().values_list("creator", flat=True))
# 对于 tenant_user_id 的查询也采取分页的方式,不需要全量查询
paginated_queryset = self.paginate_queryset(self.get_queryset().only("creator"))
tenant_user_ids = [obj.creator for obj in paginated_queryset]
return {
"user_display_name_map": TenantUserHandler.get_tenant_user_display_name_map_by_ids(tenant_user_ids),
}
Expand Down
119 changes: 55 additions & 64 deletions src/bk-user/tests/apis/web/audit/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,97 +27,98 @@ def test_audit_record_list(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"))

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 4
assert resp.data["count"] == 4

def test_audit_record_list_filter_by_creator(self, api_client, bk_user, audit_records):
resp = api_client.get(reverse("audit.list"), data={"creator": bk_user.username})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 4
assert all(record["creator"] == bk_user.username for record in resp.data)
assert resp.data["count"] == 4
assert all(record["creator"] == bk_user.username for record in resp.data["results"])

def test_audit_record_list_filter_by_operation(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"operation": "create_data_source"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data["count"] == 1
assert resp.data["results"][0]["operation"] == "create_data_source"

def test_audit_record_list_filter_by_object_type(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"object_type": "data_source"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 4
assert all(record["object_type"] == "data_source" for record in resp.data)
assert resp.data["count"] == 4
assert all(record["object_type"] == "data_source" for record in resp.data["results"])

def test_audit_record_list_filter_by_object_name(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"object_name": "DataSource1"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_fuzzy_object_name(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"object_name": "DataSource"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 4
assert all("DataSource" in record["object_name"] for record in resp.data)
assert resp.data["count"] == 4
assert all("DataSource" in record["object_name"] for record in resp.data["results"])

def test_audit_record_list_filter_by_object_type_and_name(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"object_type": "data_source", "object_name": "DataSource1"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_object_type_and_operation(self, api_client, audit_records):
resp = api_client.get(
reverse("audit.list"), data={"object_type": "data_source", "operation": "create_data_source"}
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data["count"] == 1
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["operation"] == "create_data_source"

def test_audit_record_list_filter_by_creator_and_operation(self, api_client, bk_user, audit_records):
resp = api_client.get(
reverse("audit.list"), data={"creator": bk_user.username, "operation": "create_data_source"}
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["operation"] == "create_data_source"

def test_audit_record_list_filter_by_creator_and_object_type(self, api_client, bk_user, audit_records):
resp = api_client.get(reverse("audit.list"), data={"creator": bk_user.username, "object_type": "data_source"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 4
assert resp.data["count"] == 4
assert all(
record["creator"] == bk_user.username and record["object_type"] == "data_source" for record in resp.data
record["creator"] == bk_user.username and record["object_type"] == "data_source"
for record in resp.data["results"]
)

def test_audit_record_list_filter_by_creator_and_object_name(self, api_client, bk_user, audit_records):
resp = api_client.get(reverse("audit.list"), data={"creator": bk_user.username, "object_name": "DataSource1"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_operation_and_object_name(self, api_client, audit_records):
resp = api_client.get(
reverse("audit.list"), data={"operation": "create_data_source", "object_name": "DataSource1"}
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["operation"] == "create_data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_creator_object_type_and_operation(self, api_client, bk_user, audit_records):
resp = api_client.get(
Expand All @@ -126,10 +127,10 @@ def test_audit_record_list_filter_by_creator_object_type_and_operation(self, api
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["operation"] == "create_data_source"

def test_audit_record_list_filter_by_creator_object_type_and_object_name(self, api_client, bk_user, audit_records):
resp = api_client.get(
Expand All @@ -138,10 +139,10 @@ def test_audit_record_list_filter_by_creator_object_type_and_object_name(self, a
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_creator_operation_and_object_name(self, api_client, bk_user, audit_records):
resp = api_client.get(
Expand All @@ -150,10 +151,10 @@ def test_audit_record_list_filter_by_creator_operation_and_object_name(self, api
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["operation"] == "create_data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_object_type_operation_and_object_name(self, api_client, audit_records):
resp = api_client.get(
Expand All @@ -162,10 +163,10 @@ def test_audit_record_list_filter_by_object_type_operation_and_object_name(self,
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["operation"] == "create_data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_creator_object_type_operation_and_object_name(
self, api_client, bk_user, audit_records
Expand All @@ -181,11 +182,11 @@ def test_audit_record_list_filter_by_creator_object_type_operation_and_object_na
)

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data[0]["object_name"] == "DataSource1"
assert resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["operation"] == "create_data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"

def test_audit_record_list_filter_by_invalid_operation(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"operation": "non_existent_operation"})
Expand All @@ -201,28 +202,18 @@ def test_audit_record_list_filter_by_non_existent_object_name(self, api_client,
resp = api_client.get(reverse("audit.list"), data={"object_name": "non_existent_object_name"})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 0
assert resp.data["count"] == 0

def test_audit_record_list_pagination_first_page(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"page": 1, "page_size": 2})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 2
assert resp.data["count"] == 4
assert len(resp.data["results"]) == 2

def test_audit_record_list_pagination_second_page(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"page": 2, "page_size": 2})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 2

def test_audit_record_list_pagination_third_page(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"page": 3, "page_size": 2})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 0

def test_audit_record_list_pagination_large_page(self, api_client, audit_records):
resp = api_client.get(reverse("audit.list"), data={"page": 100, "page_size": 2})

assert resp.status_code == status.HTTP_200_OK
assert len(resp.data) == 0
assert resp.data["count"] == 4
assert len(resp.data["results"]) == 2

0 comments on commit 88ba9db

Please sign in to comment.