from rest_framework.generics import ListAPIView, RetrieveAPIView from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated 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 if user.show_mature: queryset = PostModel.objects.all() else: queryset = PostModel.objects.filter(mature=False) return queryset.order_by("-date_created") class PostDetailView(RetrieveAPIView): permission_classes = [IsAuthenticated] serializer_class = PostSerializer lookup_field = ( "post_id" # This tells DRF to use the "post_id" URL kwarg for lookups. ) queryset = PostModel.objects.all()