Skip to content

Commit

Permalink
resolve: change ORM query
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 committed Nov 20, 2024
1 parent f1261c3 commit a3d808c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 57 deletions.
11 changes: 9 additions & 2 deletions src/bk-user/bkuser/apis/web/audit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ 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 @@ -62,10 +69,10 @@ def get_queryset(self):
if object_name := params.get("object_name"):
filters["object_name__icontains"] = object_name

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

def get_serializer_context(self) -> Dict[str, Any]:
tenant_user_ids = self.paginate_queryset(self.get_queryset().values_list("creator", flat=True))
tenant_user_ids = list(self.get_queryset().values_list("creator", flat=True))
return {
"user_display_name_map": TenantUserHandler.get_tenant_user_display_name_map_by_ids(tenant_user_ids),
}
Expand Down
107 changes: 52 additions & 55 deletions src/bk-user/tests/apis/web/audit/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,98 +27,97 @@ 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 resp.data["count"] == 4
assert len(resp.data) == 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 resp.data["count"] == 4
assert all(record["creator"] == bk_user.username for record in resp.data["results"])
assert len(resp.data) == 4
assert all(record["creator"] == bk_user.username for record in resp.data)

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 resp.data["count"] == 1
assert resp.data["results"][0]["operation"] == "create_data_source"
assert len(resp.data) == 1
assert resp.data[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 resp.data["count"] == 4
assert all(record["object_type"] == "data_source" for record in resp.data["results"])
assert len(resp.data) == 4
assert all(record["object_type"] == "data_source" for record in resp.data)

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 resp.data["count"] == 1
assert resp.data["results"][0]["object_name"] == "DataSource1"
assert len(resp.data) == 1
assert resp.data[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 resp.data["count"] == 4
assert all("DataSource" in record["object_name"] for record in resp.data["results"])
assert len(resp.data) == 4
assert all("DataSource" in record["object_name"] for record in resp.data)

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 resp.data["count"] == 1
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"
assert len(resp.data) == 1
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[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 resp.data["count"] == 1
assert resp.data["results"][0]["object_type"] == "data_source"
assert resp.data["results"][0]["operation"] == "create_data_source"
assert len(resp.data) == 1
assert resp.data[0]["object_type"] == "data_source"
assert resp.data[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 resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["operation"] == "create_data_source"
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[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 resp.data["count"] == 4
assert len(resp.data) == 4
assert all(
record["creator"] == bk_user.username and record["object_type"] == "data_source"
for record in resp.data["results"]
record["creator"] == bk_user.username and record["object_type"] == "data_source" for record in resp.data
)

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 resp.data["count"] == 1
assert resp.data["results"][0]["creator"] == bk_user.username
assert resp.data["results"][0]["object_name"] == "DataSource1"
assert len(resp.data) == 1
assert resp.data[0]["creator"] == bk_user.username
assert resp.data[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 resp.data["count"] == 1
assert resp.data["results"][0]["operation"] == "create_data_source"
assert resp.data["results"][0]["object_name"] == "DataSource1"
assert len(resp.data) == 1
assert resp.data[0]["operation"] == "create_data_source"
assert resp.data[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 @@ -127,10 +126,10 @@ def test_audit_record_list_filter_by_creator_object_type_and_operation(self, api
)

assert resp.status_code == status.HTTP_200_OK
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 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"

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 @@ -139,10 +138,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 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"
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"

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 @@ -151,10 +150,10 @@ def test_audit_record_list_filter_by_creator_operation_and_object_name(self, api
)

assert resp.status_code == status.HTTP_200_OK
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"
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"

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

assert resp.status_code == status.HTTP_200_OK
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"
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"

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

assert resp.status_code == status.HTTP_200_OK
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"
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"

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 @@ -202,18 +201,16 @@ 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 resp.data["count"] == 0
assert len(resp.data) == 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 resp.data["count"] == 4
assert len(resp.data["results"]) == 2
assert len(resp.data) == 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 resp.data["count"] == 4
assert len(resp.data["results"]) == 2
assert len(resp.data) == 2

0 comments on commit a3d808c

Please sign in to comment.