From a3fb229a4f19b26ec9e066d70cbf02008324e080 Mon Sep 17 00:00:00 2001 From: Samuel Paccoud - DINUM Date: Sat, 15 Feb 2025 13:16:35 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(backend)=20allow=20forcing=20page=20s?= =?UTF-8?q?ize=20within=20limits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to be able to increase the page size with by passing the query string parameter "page_size". --- CHANGELOG.md | 1 + src/backend/core/api/viewsets.py | 1 + .../documents/test_api_documents_list.py | 29 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15d27b25d..129bf0ddf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to ## Added +- ✨(backend) allow forcing page size within limits - 💄(frontend) add error pages #643 ## Changed diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index e9616affc..51f24e497 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -418,6 +418,7 @@ class DocumentViewSet( metadata_class = DocumentMetadata ordering = ["-updated_at"] ordering_fields = ["created_at", "updated_at", "title"] + pagination_class = Pagination permission_classes = [ permissions.DocumentAccessPermission, ] diff --git a/src/backend/core/tests/documents/test_api_documents_list.py b/src/backend/core/tests/documents/test_api_documents_list.py index f09ca58ce..4aa5c6c1c 100644 --- a/src/backend/core/tests/documents/test_api_documents_list.py +++ b/src/backend/core/tests/documents/test_api_documents_list.py @@ -328,6 +328,35 @@ def test_api_documents_list_pagination( assert document_ids == [] +def test_api_documents_list_pagination_force_page_size(): + """Page size can be set via querystring.""" + user = factories.UserFactory() + + client = APIClient() + client.force_login(user) + + document_ids = [ + str(access.document_id) + for access in factories.UserDocumentAccessFactory.create_batch(3, user=user) + ] + + # Force page size + response = client.get( + "/api/v1.0/documents/?page_size=2", + ) + + assert response.status_code == 200 + content = response.json() + + assert content["count"] == 3 + assert content["next"] == "http://testserver/api/v1.0/documents/?page=2&page_size=2" + assert content["previous"] is None + + assert len(content["results"]) == 2 + for item in content["results"]: + document_ids.remove(item["id"]) + + def test_api_documents_list_authenticated_distinct(): """A document with several related users should only be listed once.""" user = factories.UserFactory()