-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from djangocameroon/refactor/serializers
Feat: updates to serializers
- Loading branch information
Showing
12 changed files
with
208 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from rest_framework.pagination import PageNumberPagination | ||
from rest_framework.response import Response | ||
|
||
|
||
class CustomPagination(PageNumberPagination): | ||
page_size_query_param = 'page_size' | ||
page_query_param = 'page'.lower() | ||
max_page_size = 100 | ||
|
||
def get_paginated_response(self, data): | ||
return Response({ | ||
'status': True, | ||
'message': 'Data retrieved successfully', | ||
'status_code': 200, | ||
'page': self.page.number, | ||
'page_size': self.page.paginator.per_page, | ||
'total': self.page.paginator.count, | ||
'pagination': { | ||
'next': self.get_next_link(), | ||
'previous': self.get_previous_link(), | ||
'count': self.page.paginator.count, | ||
'current_page': self.page.number, | ||
'total_pages': self.page.paginator.num_pages | ||
}, | ||
'data': data | ||
}) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import ModelViewSet | ||
|
||
|
||
class BaseModelViewSet(ModelViewSet): | ||
""" | ||
A base viewset that you can extend to add custom pagination handling. | ||
""" | ||
|
||
def paginated_response( | ||
self, queryset, request, | ||
serializer_class, message="Success", | ||
status_code=status.HTTP_200_OK, | ||
): | ||
""" | ||
Custom paginated response method. | ||
""" | ||
page = self.paginate_queryset(queryset) | ||
serializer = serializer_class(page, many=True) | ||
response_data = { | ||
"status": True, | ||
"message": message, | ||
"status_code": status_code, | ||
"page": self.paginator.page.number, | ||
"page_size": self.paginator.page_size, | ||
"total": self.paginator.page.paginator.count, | ||
"pagination": { | ||
"next": self.paginator.get_next_link(), | ||
"previous": self.paginator.get_previous_link(), | ||
"count": self.paginator.page.paginator.count, | ||
"current_page": self.paginator.page.number, | ||
"total_pages": self.paginator.page.paginator.num_pages, | ||
}, | ||
"results": serializer.data | ||
} | ||
return Response(response_data, status=status_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters