Skip to content

Commit

Permalink
⚡️ Disable pagination when requesting form definitions in the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Oct 18, 2023
1 parent cc8fe70 commit 2ecce2a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/openforms/forms/api/pagination.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions src/openforms/forms/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from rest_framework.request import Request
from rest_framework.response import Response

from openforms.api.pagination import PageNumberPagination
from openforms.api.serializers import ExceptionSerializer, ValidationErrorSerializer
from openforms.translations.utils import set_language_cookie
from openforms.utils.patches.rest_framework_nested.viewsets import NestedViewSetMixin
Expand All @@ -32,6 +31,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,
Expand Down Expand Up @@ -169,7 +169,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
Expand Down
23 changes: 23 additions & 0 deletions src/openforms/forms/tests/test_api_formdefinition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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'},
Expand Down

0 comments on commit 2ecce2a

Please sign in to comment.