-
Notifications
You must be signed in to change notification settings - Fork 1
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 #16 from rkshaon/rkshaon
backend
- Loading branch information
Showing
12 changed files
with
82 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,3 +143,4 @@ | |
|
||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | ||
AUTH_USER_MODEL = 'user_api.User' | ||
APPEND_SLASH = True |
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,20 @@ | ||
# BookShelf/utilities/pagination | ||
from rest_framework.pagination import PageNumberPagination | ||
|
||
|
||
class Pagination(PageNumberPagination): | ||
page_size = 10 | ||
page_size_query_param = 'page_size' | ||
max_page_size = 100 | ||
|
||
def get_page_size(self, request): | ||
""" | ||
Handle dynamic page_size via query parameter | ||
or fall back to default. | ||
""" | ||
if request.query_params.get(self.page_size_query_param): | ||
return min( | ||
int(request.query_params[self.page_size_query_param]), | ||
self.max_page_size | ||
) | ||
return self.page_size |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from django.shortcuts import render | ||
# from django.shortcuts import render | ||
|
||
# Create your views here. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import serializers | ||
|
||
from book_api.models import Book | ||
|
||
|
||
class BookSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Book | ||
fields = '__all__' |
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,7 @@ | ||
from django.urls import path, include | ||
from book_api.urls.v1 import urlpatterns as v1 | ||
|
||
|
||
urlpatterns = [ | ||
path('v1/', include(v1)), | ||
] |
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,9 @@ | ||
from django.urls import path | ||
|
||
from book_api.views import v1 | ||
|
||
|
||
urlpatterns = [ | ||
path('', v1.BookView.as_view()), | ||
path('<int:pk>/', v1.BookView.as_view()), | ||
] |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework.permissions import AllowAny | ||
from rest_framework.exceptions import NotFound | ||
|
||
from BookShelf.utilities.pagination import Pagination | ||
|
||
from book_api.models import Book | ||
|
||
from book_api.serializers.v1 import BookSerializer | ||
|
||
|
||
class BookView(APIView): | ||
permission_classes = [AllowAny] | ||
|
||
def get(self, request, *args, **kwargs): | ||
pk = kwargs.get('pk', None) | ||
|
||
if pk: | ||
try: | ||
book = Book.objects.get(pk=pk, is_deleted=False) | ||
except Book.DoesNotExist: | ||
raise NotFound(detail="Book not found.") | ||
|
||
return Response(BookSerializer(book).data) | ||
|
||
books = Book.objects.filter( | ||
is_deleted=False | ||
).order_by('id') | ||
paginator = Pagination() | ||
page = paginator.paginate_queryset(books, request) | ||
serializer = BookSerializer(page, many=True) | ||
return paginator.get_paginated_response(serializer.data) |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from django.shortcuts import render | ||
# from django.shortcuts import render | ||
|
||
# Create your views here. |