Browse Source

arguments paginated.

master
Mohsen Taba 5 months ago
parent
commit
4f776decaf
  1. 34
      apps/hadis/docs.py
  2. 23
      apps/hadis/views/hadis.py

34
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 - Supports full-text search across titles, narrator titles, Arabic text, and translations
- Filter by hadis status and category titles - Filter by hadis status and category titles
- Includes complete category and status metadata in the response - 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 - Translations are automatically provided based on the Accept-Language header
**Search Functionality:** **Search Functionality:**
@ -2037,11 +2038,18 @@ hadis_main_list_swagger = swagger_auto_schema(
- `status`: Filter by hadis status title (e.g., "authentic", "weak") - `status`: Filter by hadis status title (e.g., "authentic", "weak")
- `category`: Filter by category title (e.g., "prayer", "faith") - `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:** **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 - `categories`: List of all available categories with localized titles
- `statuses`: List of all available hadis statuses 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", operation_summary="List All Hadis with Search & Filters",
tags=['Hadis'], tags=['Hadis'],
@ -2078,6 +2086,22 @@ hadis_main_list_swagger = swagger_auto_schema(
required=False, required=False,
default='en', default='en',
enum=['en', 'fa', 'ar', 'ur', 'ru'] 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={ responses={
@ -2086,6 +2110,8 @@ hadis_main_list_swagger = swagger_auto_schema(
examples={ examples={
"application/json": { "application/json": {
"count": 150, "count": 150,
"next": "http://example.com/api/hadis/arguments/?page=2",
"previous": None,
"categories": [ "categories": [
"Faith Fundamentals", "Faith Fundamentals",
"Prayer Rites", "Prayer Rites",
@ -2094,8 +2120,8 @@ hadis_main_list_swagger = swagger_auto_schema(
], ],
"statuses": [ "statuses": [
"Authentic (Sahih)", "Authentic (Sahih)",
"Good (Hasan)",
"Weak (Da'if)"
"Good (Hasan)",
"Weak (Da'if)"
], ],
"results": [ "results": [
{ {

23
apps/hadis/views/hadis.py

@ -1,6 +1,7 @@
from rest_framework.generics import ListAPIView, RetrieveAPIView from rest_framework.generics import ListAPIView, RetrieveAPIView
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from utils.pagination import NoPagination from utils.pagination import NoPagination
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response from rest_framework.response import Response
from django.db.models import Count from django.db.models import Count
from django.db.models import Prefetch from django.db.models import Prefetch
@ -100,6 +101,7 @@ class HadisMainListView(ListAPIView):
API view to list Hadis by category_id API view to list Hadis by category_id
""" """
serializer_class = HadisListSerializer serializer_class = HadisListSerializer
pagination_class = PageNumberPagination
@hadis_main_list_swagger @hadis_main_list_swagger
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -129,6 +131,27 @@ class HadisMainListView(ListAPIView):
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
queryset = self.get_queryset() 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) serializer = self.get_serializer(queryset, many=True)
# Get category titles # Get category titles

Loading…
Cancel
Save