Fixed pagination and average score in swagger
This commit is contained in:
parent
2a11a30cd5
commit
7892c8358e
32
project/pagination.py
Normal file
32
project/pagination.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from rest_framework.pagination import PageNumberPagination
|
||||||
|
from rest_framework.response import Response
|
||||||
|
import math
|
||||||
|
|
||||||
|
class CustomPagination(PageNumberPagination):
|
||||||
|
page_size = 20
|
||||||
|
page_size_query_param = 'limit'
|
||||||
|
|
||||||
|
def get_paginated_response(self, data):
|
||||||
|
total_items = self.page.paginator.count
|
||||||
|
total_pages = math.ceil(total_items / self.page_size)
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
'count': total_items,
|
||||||
|
'page_size': self.page_size,
|
||||||
|
'page': self.page.number,
|
||||||
|
'total_pages': total_pages,
|
||||||
|
'results': data
|
||||||
|
})
|
||||||
|
|
||||||
|
# drf spectacular swagger
|
||||||
|
def get_paginated_response_schema(self, schema):
|
||||||
|
return {
|
||||||
|
'type': 'object',
|
||||||
|
'properties': {
|
||||||
|
'count': {'type': 'integer'},
|
||||||
|
'page': {'type': 'integer'},
|
||||||
|
'page_size': {'type': 'integer'},
|
||||||
|
'total_pages': {'type': 'integer'},
|
||||||
|
'results': schema,
|
||||||
|
},
|
||||||
|
}
|
||||||
@ -75,7 +75,7 @@ SPECTACULAR_SETTINGS = {
|
|||||||
CORS_ALLOW_ALL_ORIGINS = True
|
CORS_ALLOW_ALL_ORIGINS = True
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
'DEFAULT_PAGINATION_CLASS': 'project.pagination.CustomPagination',
|
||||||
'PAGE_SIZE': 20,
|
'PAGE_SIZE': 20,
|
||||||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||||
'rest_framework.authentication.TokenAuthentication',
|
'rest_framework.authentication.TokenAuthentication',
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
|
from drf_spectacular.utils import extend_schema_field
|
||||||
|
|
||||||
from users.serializers import PublicUserSerializer
|
from users.serializers import PublicUserSerializer
|
||||||
from .models import Publication, Rating, Category
|
from .models import Publication, Rating, Category
|
||||||
@ -14,6 +15,7 @@ class PublicationSerializer(serializers.ModelSerializer):
|
|||||||
user_detail = PublicUserSerializer(source="user", read_only=True)
|
user_detail = PublicUserSerializer(source="user", read_only=True)
|
||||||
average_score = serializers.SerializerMethodField(read_only=True)
|
average_score = serializers.SerializerMethodField(read_only=True)
|
||||||
|
|
||||||
|
@extend_schema_field(serializers.FloatField())
|
||||||
def get_average_score(self, obj):
|
def get_average_score(self, obj):
|
||||||
return getattr(obj, 'average_score', 0)
|
return getattr(obj, 'average_score', 0)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user