diff --git a/backend/api/posts/views.py b/backend/api/posts/views.py index 1149c3f..a1c230d 100644 --- a/backend/api/posts/views.py +++ b/backend/api/posts/views.py @@ -6,12 +6,32 @@ from apps.archive.models import PostModel from .serializers import PostPreviewSerializer, PostSerializer +from rest_framework.pagination import PageNumberPagination + + +class PostListPagination(PageNumberPagination): + page_size = 10 # number of items per page + page_size_query_param = "page_size" # allows clients to specify page size + max_page_size = 100 # maximum page size allowed + + def get_paginated_response(self, data): + return Response( + { + "count": self.page.paginator.count, + "totalPages": self.page.paginator.num_pages, # total number of pages + "next": self.get_next_link(), + "previous": self.get_previous_link(), + "results": data, + } + ) + class PostListView(ListAPIView): permission_classes = [IsAuthenticated] serializer_class = ( PostPreviewSerializer # Each post will be serialized using this serializer ) + pagination_class = PostListPagination def get_queryset(self): user = self.request.user.userprofile