From 4f776decaf136dbe66b9149a5c507414a4f79e57 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Mon, 22 Dec 2025 14:54:33 +0330 Subject: [PATCH] arguments paginated. --- apps/hadis/docs.py | 34 ++++++++++++++++++++++++++++++---- apps/hadis/views/hadis.py | 23 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/apps/hadis/docs.py b/apps/hadis/docs.py index 1f6d70a..629b822 100644 --- a/apps/hadis/docs.py +++ b/apps/hadis/docs.py @@ -2024,6 +2024,7 @@ hadis_main_list_swagger = swagger_auto_schema( - Supports full-text search across titles, narrator titles, Arabic text, and translations - Filter by hadis status and category titles - Includes complete category and status metadata in the response + - Supports pagination for large result sets - Translations are automatically provided based on the Accept-Language header **Search Functionality:** @@ -2037,11 +2038,18 @@ hadis_main_list_swagger = swagger_auto_schema( - `status`: Filter by hadis status title (e.g., "authentic", "weak") - `category`: Filter by category title (e.g., "prayer", "faith") + **Pagination:** + - Use `page` parameter to navigate through pages (starts from 1) + - Use `page_size` parameter to control items per page + - Response includes `next` and `previous` URLs for navigation + **Response Structure:** - - `count`: Total number of hadis returned + - `count`: Total number of hadis matching the criteria (not just current page) + - `next`: URL for the next page (null if on last page) + - `previous`: URL for the previous page (null if on first page) - `categories`: List of all available categories with localized titles - `statuses`: List of all available hadis statuses with localized titles - - `results`: Array of hadis objects with full details including individual status + - `results`: Array of hadis objects for the current page with full details including individual status """, operation_summary="List All Hadis with Search & Filters", tags=['Hadis'], @@ -2078,6 +2086,22 @@ hadis_main_list_swagger = swagger_auto_schema( required=False, default='en', enum=['en', 'fa', 'ar', 'ur', 'ru'] + ), + openapi.Parameter( + 'page', + openapi.IN_QUERY, + description="Page number for pagination. Starts from 1. If not provided, returns the first page.", + type=openapi.TYPE_INTEGER, + required=False, + example=1 + ), + openapi.Parameter( + 'page_size', + openapi.IN_QUERY, + description="Number of items per page. If not provided, uses the default page size.", + type=openapi.TYPE_INTEGER, + required=False, + example=20 ) ], responses={ @@ -2086,6 +2110,8 @@ hadis_main_list_swagger = swagger_auto_schema( examples={ "application/json": { "count": 150, + "next": "http://example.com/api/hadis/arguments/?page=2", + "previous": None, "categories": [ "Faith Fundamentals", "Prayer Rites", @@ -2094,8 +2120,8 @@ hadis_main_list_swagger = swagger_auto_schema( ], "statuses": [ "Authentic (Sahih)", - "Good (Hasan)", - "Weak (Da'if)" + "Good (Hasan)", + "Weak (Da'if)" ], "results": [ { diff --git a/apps/hadis/views/hadis.py b/apps/hadis/views/hadis.py index e918161..9bc09b6 100644 --- a/apps/hadis/views/hadis.py +++ b/apps/hadis/views/hadis.py @@ -1,6 +1,7 @@ from rest_framework.generics import ListAPIView, RetrieveAPIView from django.shortcuts import get_object_or_404 from utils.pagination import NoPagination +from rest_framework.pagination import PageNumberPagination from rest_framework.response import Response from django.db.models import Count from django.db.models import Prefetch @@ -100,6 +101,7 @@ class HadisMainListView(ListAPIView): API view to list Hadis by category_id """ serializer_class = HadisListSerializer + pagination_class = PageNumberPagination @hadis_main_list_swagger def get(self, request, *args, **kwargs): @@ -129,6 +131,27 @@ class HadisMainListView(ListAPIView): def list(self, request, *args, **kwargs): queryset = self.get_queryset() + + # Apply pagination + page = self.paginate_queryset(queryset) + if page is not None: + serializer = self.get_serializer(page, many=True) + paginated_response = self.get_paginated_response(serializer.data) + + # Get category titles + category_titles = self.get_category_titles(request) + + # Get status titles + status_titles = self.get_status_titles(request) + + # Modify the paginated response to include our custom data + response_data = paginated_response.data + response_data['categories'] = category_titles + response_data['statuses'] = status_titles + + return Response(response_data) + + # Fallback for when pagination is disabled serializer = self.get_serializer(queryset, many=True) # Get category titles