diff --git a/src/openforms/forms/api/pagination.py b/src/openforms/forms/api/pagination.py new file mode 100644 index 0000000000..9ee63c673a --- /dev/null +++ b/src/openforms/forms/api/pagination.py @@ -0,0 +1,19 @@ +from rest_framework import pagination + + +class FormDefinitionPagination(pagination.PageNumberPagination): + """A page number pagination that will be disabled if the specific query parameter + for page size is set to 0. + """ + + page_size = 25 + page_size_query_param = "page_size" + max_page_size = None + + def get_page_size(self, request) -> int | None: + if ( + self.page_size_query_param + and request.query_params.get(self.page_query_param) == 0 + ): + return None + return super().get_page_size(request) diff --git a/src/openforms/forms/api/viewsets.py b/src/openforms/forms/api/viewsets.py index 99aef7fe27..527418a936 100644 --- a/src/openforms/forms/api/viewsets.py +++ b/src/openforms/forms/api/viewsets.py @@ -32,6 +32,7 @@ from .datastructures import FormVariableWrapper from .documentation import get_admin_fields_markdown from .filters import FormDefinitionFilter, FormVariableFilter +from .pagination import FormDefinitionPagination from .parsers import ( FormCamelCaseJSONParser, FormVariableJSONParser, @@ -169,7 +170,7 @@ class FormDefinitionViewSet(viewsets.ModelViewSet): renderer_classes = (IgnoreConfigurationFieldCamelCaseJSONRenderer,) queryset = FormDefinition.objects.order_by("slug") serializer_class = FormDefinitionSerializer - pagination_class = PageNumberPagination + pagination_class = FormDefinitionPagination lookup_field = "uuid" # anonymous clients must be able to get the form definitions in the browser # The DRF settings apply some default throttling to mitigate abuse diff --git a/src/openforms/forms/tests/test_api_formdefinition.py b/src/openforms/forms/tests/test_api_formdefinition.py index 60752dc4ad..f5e4125e7c 100644 --- a/src/openforms/forms/tests/test_api_formdefinition.py +++ b/src/openforms/forms/tests/test_api_formdefinition.py @@ -39,6 +39,29 @@ def test_list(self): self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + def test_list_pagination(self): + user = StaffUserFactory.create(user_permissions=["change_form"]) + self.client.force_authenticate(user=user) + + FormDefinitionFactory.create_batch(100) + + url = reverse("api:formdefinition-list") + + with self.subTest("no pagination"): + response = self.client.get(url, {"page_size": "0"}, format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.json()["results"]), 100) + + with self.subTest("default pagination"): + response = self.client.get(url, format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.json()["results"]), 25) + + with self.subTest("explicit pagination"): + response = self.client.get(url, {"page_size": "30"}, format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.json()["results"]), 30) + def test_user_without_permission_cant_update(self): user = UserFactory.create() self.client.force_authenticate(user=user) diff --git a/src/openforms/js/components/admin/form_design/form-creation-form.js b/src/openforms/js/components/admin/form_design/form-creation-form.js index 82082c40d7..4ba3d37020 100644 --- a/src/openforms/js/components/admin/form_design/form-creation-form.js +++ b/src/openforms/js/components/admin/form_design/form-creation-form.js @@ -187,7 +187,7 @@ function reducer(draft, action) { * Form-level actions */ case 'BACKEND_DATA_LOADED': { - const {formData, supportingData} = action.payload; + const {supportingData, formData} = action.payload; const {form, selectedAuthPlugins, steps, variables, logicRules, priceRules} = formData; for (const [stateVar, data] of Object.entries(supportingData)) { @@ -955,7 +955,7 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl}) => { {endpoint: PAYMENT_PLUGINS_ENDPOINT, stateVar: 'availablePaymentBackends'}, { endpoint: FORM_DEFINITIONS_ENDPOINT, - query: {is_reusable: true, used_in: formUuid || ''}, + query: {is_reusable: true, used_in: formUuid || '', page_size: 0}, stateVar: 'formDefinitions', }, {endpoint: REGISTRATION_BACKENDS_ENDPOINT, stateVar: 'availableRegistrationBackends'},