Skip to content

Commit

Permalink
Add page_size query_param to Reasons and Tags list views (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
willemarcel authored Jun 4, 2024
1 parent 680789c commit aee0c7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
18 changes: 16 additions & 2 deletions osmchadjango/changeset/tests/test_reasons_tags_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def setUp(self):
name='suspect word',
is_visible=False
)
SuspicionReasons.objects.create(name="another word", is_visible=True)
self.user = User.objects.create_user(
username='test',
password='password',
Expand All @@ -33,7 +34,7 @@ def setUp(self):
def test_view(self):
response = self.client.get(reverse('changeset:suspicion-reasons-list'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data.get('results')), 1)
self.assertEqual(len(response.data.get('results')), 2)
reason_dict = {
'id': self.reason_1.id,
'name': 'possible import',
Expand All @@ -44,11 +45,16 @@ def test_view(self):
}
self.assertIn(reason_dict, response.data.get('results'))

def test_pagination_params(self):
response = self.client.get(reverse("changeset:suspicion-reasons-list"), {"page_size": 1})
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data.get("results")), 1)

def test_admin_user_request(self):
self.client.login(username=self.user.username, password='password')
response = self.client.get(reverse('changeset:suspicion-reasons-list'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data.get('results')), 2)
self.assertEqual(len(response.data.get('results')), 3)


class TestTagAPIListView(APITestCase):
Expand Down Expand Up @@ -90,6 +96,14 @@ def test_view(self):
response.data.get('results')
)

def test_pagination_params(self):
Tag.objects.create(
name="Bad change", description="A changeset that added bad data."
)
response = self.client.get(reverse("changeset:tags-list"), {"page_size": 1})
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data.get("results")), 1)

def test_admin_user_request(self):
self.client.login(username=self.user.username, password='password')
response = self.client.get(reverse('changeset:tags-list'))
Expand Down
9 changes: 9 additions & 0 deletions osmchadjango/changeset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DestroyAPIView
)
from rest_framework.parsers import JSONParser, MultiPartParser, FormParser
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer
Expand Down Expand Up @@ -46,6 +47,12 @@ class StandardResultsSetPagination(GeoJsonPagination):
max_page_size = 500


class DefaultPagination(PageNumberPagination):
page_size = 50
page_size_query_param = 'page_size'
max_page_size = 500


class PaginatedCSVRenderer (CSVRenderer):
results_field = 'features'

Expand Down Expand Up @@ -157,6 +164,7 @@ def get_queryset(self):
class SuspicionReasonsListAPIView(ListAPIView):
"""List SuspicionReasons."""
serializer_class = SuspicionReasonsSerializer
pagination_class = DefaultPagination

def get_queryset(self):
if self.request and self.request.user.is_staff:
Expand Down Expand Up @@ -214,6 +222,7 @@ def remove_reason_from_changesets(self, request, pk):
class TagListAPIView(ListAPIView):
"""List Tags."""
serializer_class = TagSerializer
pagination_class = DefaultPagination

def get_queryset(self):
if self.request and self.request.user.is_staff:
Expand Down

0 comments on commit aee0c7f

Please sign in to comment.