Skip to content

Commit

Permalink
Merge pull request #179 from rkshaon/177-topic-apis
Browse files Browse the repository at this point in the history
done: #177 - topic 6 APIs
  • Loading branch information
rkshaon authored Nov 29, 2024
2 parents 49b8764 + 643ee02 commit 929a7ba
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
3 changes: 1 addition & 2 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 @@ -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
3 changes: 1 addition & 2 deletions backend/book_api/urls/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
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('topic/', v1.TopicView.as_view()),
path('topic/<int:pk>/', v1.TopicView.as_view()),
path('search', v1.BookSearchView.as_view()),
]

Expand Down
4 changes: 2 additions & 2 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 GenreViewSet
from book_api.views.v1.topic import TopicView
from book_api.views.v1.topic import TopicViewSet
from book_api.views.v1.search import BookSearchView


__all__ = [
BookView,
GenreViewSet,
TopicView,
TopicViewSet,
BookSearchView,
]
7 changes: 0 additions & 7 deletions backend/book_api/views/v1/genre.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
# from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from rest_framework import status
# from rest_framework.permissions import AllowAny
# from rest_framework.exceptions import NotFound

# 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

Expand Down
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 929a7ba

Please sign in to comment.