Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rkshaon/BookShelf into 124-add-bo…
Browse files Browse the repository at this point in the history
…ok-upload-feature
  • Loading branch information
rkshaon committed Nov 29, 2024
2 parents e981fc6 + 929a7ba commit a8ce512
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 65 deletions.
5 changes: 2 additions & 3 deletions backend/book_api/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.contrib import admin
from django.utils.html import format_html

# import frontend base url from settings
from BookShelf.settings import FRONTEND_BASE_URL

from book_api.models import Genre
Expand All @@ -12,7 +11,7 @@
@admin.register(Genre)
class GenreAdmin(admin.ModelAdmin):
list_display = [
'id', 'name', 'slug',
'id', 'name', 'slug', 'is_deleted',
]
list_display_links = ['name']
search_fields = ['name']
Expand All @@ -24,7 +23,7 @@ class GenreAdmin(admin.ModelAdmin):
@admin.register(Topic)
class TopicAdmin(admin.ModelAdmin):
list_display = [
'id', 'name', 'slug',
'id', 'name', 'slug', 'is_deleted',
]
list_display_links = ['name']
search_fields = ['name']
Expand Down
12 changes: 8 additions & 4 deletions backend/book_api/urls/v1.py
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
8 changes: 4 additions & 4 deletions backend/book_api/views/v1/__init__.py
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,
]
78 changes: 51 additions & 27 deletions backend/book_api/views/v1/genre.py
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
# )
52 changes: 25 additions & 27 deletions backend/book_api/views/v1/topic.py
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,
)

0 comments on commit a8ce512

Please sign in to comment.