Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done: bs-194 - migrating book APIs #222

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
done: bs-194 - delete, update, search API done
  • Loading branch information
rkshaon committed Jan 16, 2025
commit 0eb1f4be9ac4891bdedd5ed5805216913a239a95
55 changes: 30 additions & 25 deletions backend/book_api/views/v1/book.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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 rest_framework.exceptions import NotFound

from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator
# 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
Expand All @@ -24,13 +23,37 @@ class BookViewSet(ModelViewSet):
IsAdminOrModerator,
]
filter_backends = [SearchFilter]
search_fields = ['title']
pagination_class = Pagination
search_fields = [
'title', 'description',
'authors__first_name',
'authors__middle_name',
'authors__last_name',
'authors__biography',
'genres__name',
'genres__description',
'topics__name',
'publisher__name',
]
lookup_field = 'book_code'


def perform_create(self, serializer):
serializer.save(added_by=self.request.user)

# def perform_update(self, serializer):
# serializer.save(updated_by=self.request.user)

def perform_destroy(self, instance):
instance.is_deleted = True
instance.save()

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
return Response({
"message": f"Book '{instance.title}' has been successfully deleted." # noqa
}, status=status.HTTP_200_OK)

def retrieve(self, request, *args, **kwargs):
"""Log the event when retrieving a single item."""
response = super().retrieve(request, *args, **kwargs)
Expand Down Expand Up @@ -61,21 +84,3 @@ def list(self, request, *args, **kwargs):
)

return response



# @method_decorator(cache_page(60*1), name='get')
# class BookView(APIView):
# permission_classes = [AllowAny]

# def post(self, request, *args, **kwargs):
# self.permission_classes = [IsAdminOrModerator]
# self.check_permissions(request)
# request.data['added_by'] = request.user.id
# serializer = BookSerializer(
# data=request.data,
# )
# serializer.is_valid(raise_exception=True)
# serializer.save()

# return Response(serializer.data, status=201)
Loading