HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/unlimited-leads/Unlimited-Leads-Be/Admin/paginations.py
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework import status

class LeadPagination(PageNumberPagination):
    """
    Pagination for LeadListView with SPA-friendly response.
    """
    page_size = 10  # Number of items per page
    page_size_query_param = 'page_size'  # Optional: Allows clients to set page size using a query parameter
    max_page_size = 1000  # Optional: Maximum limit for page size

    def get_paginated_response(self, data):
        """
        Customize the pagination response to include only relative paths for SPA compatibility.
        """
        return Response({
            'success' : True,
            'message' : 'Leads retrieved successfully.',
            'statusCode' : status.HTTP_200_OK,
            'count': self.page.paginator.count,
            'current_page': self.page.number,
            'next_page': self.page.number + 1 if self.page.has_next() else None,
            'previous_page': self.page.number - 1 if self.page.has_previous() else None,
            'total_pages': self.page.paginator.num_pages,
            'data': data
        })
    
class UserPagination(PageNumberPagination):
    """
    Pagination for UserListView with SPA-friendly response.
    """
    page_size = 10  
    page_size_query_param = 'page_size' 
    max_page_size = 100 

    def get_paginated_response(self, data):
        """
        Customize the pagination response.
        """
        return Response({
            'success': True,
            'message': 'Users retrieved successfully.',
            'statusCode': status.HTTP_200_OK,
            'count': self.page.paginator.count,
            'current_page': self.page.number,
            'next_page': self.page.number + 1 if self.page.has_next() else None,
            'previous_page': self.page.number - 1 if self.page.has_previous() else None,
            'total_pages': self.page.paginator.num_pages,
            'data': data,
        })