-
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 branch 'master' of github.com:rkshaon/BookShelf into 124-add-bo…
…ok-upload-feature
- Loading branch information
Showing
5 changed files
with
90 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
from django.urls import path | ||
from rest_framework.routers import DefaultRouter | ||
|
||
from book_api.views import v1 | ||
|
||
|
||
router = DefaultRouter() | ||
|
||
router.register('genre', v1.GenreViewSet, basename='genre') | ||
router.register('topic', v1.TopicViewSet, basename='topic') | ||
|
||
urlpatterns = [ | ||
path('', v1.BookView.as_view()), | ||
path('<int:book_code>/', v1.BookView.as_view()), | ||
path('genre/', v1.GenreView.as_view()), | ||
path('genre/<int:pk>/', v1.GenreView.as_view()), | ||
path('topic/', v1.TopicView.as_view()), | ||
path('topic/<int:pk>/', v1.TopicView.as_view()), | ||
path('search', v1.BookSearchView.as_view()), | ||
] | ||
|
||
urlpatterns += router.urls |
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,12 +1,12 @@ | ||
from book_api.views.v1.book import BookView | ||
from book_api.views.v1.genre import GenreView | ||
from book_api.views.v1.topic import TopicView | ||
from book_api.views.v1.genre import GenreViewSet | ||
from book_api.views.v1.topic import TopicViewSet | ||
from book_api.views.v1.search import BookSearchView | ||
|
||
|
||
__all__ = [ | ||
BookView, | ||
GenreView, | ||
TopicView, | ||
GenreViewSet, | ||
TopicViewSet, | ||
BookSearchView, | ||
] |
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,40 +1,64 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.viewsets import ModelViewSet | ||
from rest_framework.response import Response | ||
from rest_framework.permissions import AllowAny | ||
from rest_framework.exceptions import NotFound | ||
from rest_framework import status | ||
|
||
from django.views.decorators.cache import cache_page | ||
from django.utils.decorators import method_decorator | ||
|
||
from BookShelf.utilities.pagination import Pagination | ||
from BookShelf.utilities.permissions import IsAdminOrModerator | ||
from BookShelf.utilities.filters import SearchFilter | ||
|
||
from book_api.models import Genre | ||
|
||
from book_api.serializers.v1 import GenreSerializer | ||
|
||
|
||
@method_decorator(cache_page(60*15), name='get') | ||
class GenreView(APIView): | ||
permission_classes = [AllowAny] | ||
class GenreViewSet(ModelViewSet): | ||
queryset = Genre.objects.filter(is_deleted=False) | ||
serializer_class = GenreSerializer | ||
permission_classes = [ | ||
IsAdminOrModerator, | ||
] | ||
filter_backends = [SearchFilter] | ||
search_fields = ['name'] | ||
|
||
def get(self, request, *args, **kwargs): | ||
pk = kwargs.get('pk', None) | ||
def perform_create(self, serializer): | ||
serializer.save(added_by=self.request.user) | ||
|
||
if pk: | ||
try: | ||
genre = Genre.objects.get( | ||
pk=pk, is_deleted=False | ||
) | ||
except Genre.DoesNotExist: | ||
raise NotFound(detail='Genre not found.') | ||
def perform_update(self, serializer): | ||
serializer.save(updated_by=self.request.user) | ||
|
||
return Response(GenreSerializer(genre).data) | ||
def perform_destroy(self, instance): | ||
instance.is_deleted = True | ||
instance.save() | ||
|
||
genres = Genre.objects.filter( | ||
is_deleted=False | ||
).order_by('-id') | ||
paginator = Pagination() | ||
page = paginator.paginate_queryset(genres, request) | ||
return paginator.get_paginated_response( | ||
GenreSerializer(page, many=True).data | ||
def destroy(self, request, *args, **kwargs): | ||
instance = self.get_object() | ||
self.perform_destroy(instance) | ||
return Response( | ||
{"message": f"Genre '{instance.name}' has been successfully deleted."}, # noqa | ||
status=status.HTTP_200_OK, | ||
) | ||
|
||
# @method_decorator(cache_page(60*15), name='get') | ||
# class GenreView(APIView): | ||
# permission_classes = [AllowAny] | ||
|
||
# def get(self, request, *args, **kwargs): | ||
# pk = kwargs.get('pk', None) | ||
|
||
# if pk: | ||
# try: | ||
# genre = Genre.objects.get( | ||
# pk=pk, is_deleted=False | ||
# ) | ||
# except Genre.DoesNotExist: | ||
# raise NotFound(detail='Genre not found.') | ||
|
||
# return Response(GenreSerializer(genre).data) | ||
|
||
# genres = Genre.objects.filter( | ||
# is_deleted=False | ||
# ).order_by('-id') | ||
# paginator = Pagination() | ||
# page = paginator.paginate_queryset(genres, request) | ||
# return paginator.get_paginated_response( | ||
# GenreSerializer(page, many=True).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,40 +1,38 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.permissions import AllowAny | ||
from rest_framework.exceptions import NotFound | ||
from rest_framework.viewsets import ModelViewSet | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
|
||
from django.views.decorators.cache import cache_page | ||
from django.utils.decorators import method_decorator | ||
from BookShelf.utilities.permissions import IsAdminOrModerator | ||
from BookShelf.utilities.filters import SearchFilter | ||
|
||
from book_api.models import Topic | ||
|
||
from book_api.serializers.v1 import TopicSerializer | ||
|
||
from BookShelf.utilities.pagination import Pagination | ||
|
||
class TopicViewSet(ModelViewSet): | ||
queryset = Topic.objects.filter(is_deleted=False) | ||
serializer_class = TopicSerializer | ||
permission_classes = [ | ||
IsAdminOrModerator, | ||
] | ||
filter_backends = [SearchFilter] | ||
search_fields = ['name'] | ||
|
||
@method_decorator(cache_page(60*15), name='get') | ||
class TopicView(APIView): | ||
permission_classes = [AllowAny] | ||
def perform_create(self, serializer): | ||
serializer.save(added_by=self.request.user) | ||
|
||
def get(self, request, *args, **kwargs): | ||
pk = kwargs.get('pk', None) | ||
def perform_update(self, serializer): | ||
serializer.save(updated_by=self.request.user) | ||
|
||
if pk: | ||
try: | ||
topic = Topic.objects.get( | ||
pk=pk, is_deleted=False | ||
) | ||
except Topic.DoesNotExist: | ||
raise NotFound(detail='Topic not found.') | ||
def perform_destroy(self, instance): | ||
instance.is_deleted = True | ||
instance.save() | ||
|
||
return Response(TopicSerializer(topic).data) | ||
|
||
topics = Topic.objects.filter( | ||
is_deleted=False | ||
).order_by('-id') | ||
paginator = Pagination() | ||
page = paginator.paginate_queryset(topics, request) | ||
return paginator.get_paginated_response( | ||
TopicSerializer(page, many=True).data | ||
def destroy(self, request, *args, **kwargs): | ||
instance = self.get_object() | ||
self.perform_destroy(instance) | ||
return Response( | ||
{"message": f"Topic '{instance.name}' has been successfully deleted."}, # noqa | ||
status=status.HTTP_200_OK, | ||
) |