Changed work of shelf books
This commit is contained in:
84
backend/apps/shelf_books/views.py
Normal file
84
backend/apps/shelf_books/views.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from rest_framework import generics
|
||||
|
||||
from apps.shelf_books.models import ShelfBook
|
||||
from apps.shelves.models import Shelf
|
||||
from apps.shelf_books.serializers import ShelfBookSerializer
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
from drf_spectacular.utils import extend_schema, extend_schema_view
|
||||
|
||||
|
||||
@extend_schema_view(
|
||||
get=extend_schema(tags=["Shelf Books"]),
|
||||
post=extend_schema(tags=["Shelf Books"]),
|
||||
)
|
||||
class ShelfBooks(generics.ListCreateAPIView):
|
||||
queryset = ShelfBook.objects.all()
|
||||
serializer_class = ShelfBookSerializer
|
||||
|
||||
authentication_classes = [JWTAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
shelf_id = self.kwargs["shelf_id"]
|
||||
|
||||
return ShelfBook.objects.filter(
|
||||
shelf__id=shelf_id,
|
||||
shelf__user_id=self.request.user.pk,
|
||||
).select_related("book", "shelf").prefetch_related("book__authors")
|
||||
|
||||
def get_serializer_context(self):
|
||||
context = super().get_serializer_context()
|
||||
|
||||
shelf_id = self.kwargs["shelf_id"]
|
||||
|
||||
shelf = Shelf.objects.filter(
|
||||
id=shelf_id,
|
||||
user=self.request.user
|
||||
).first()
|
||||
|
||||
context["shelf"] = shelf
|
||||
return context
|
||||
|
||||
|
||||
@extend_schema_view(
|
||||
get=extend_schema(tags=["Shelf Books"]),
|
||||
put=extend_schema(tags=["Shelf Books"]),
|
||||
patch=extend_schema(tags=["Shelf Books"]),
|
||||
delete=extend_schema(tags=["Shelf Books"]),
|
||||
)
|
||||
class SelectedShelfBook(generics.RetrieveUpdateDestroyAPIView):
|
||||
queryset = ShelfBook.objects.all()
|
||||
serializer_class = ShelfBookSerializer
|
||||
|
||||
authentication_classes = [JWTAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
lookup_url_kwarg = "book_id"
|
||||
|
||||
def get_queryset(self):
|
||||
return ShelfBook.objects.filter(
|
||||
shelf_id=self.kwargs["shelf_id"],
|
||||
shelf__user=self.request.user,
|
||||
).select_related("book", "shelf").prefetch_related("book__authors")
|
||||
|
||||
def get_object(self):
|
||||
queryset = self.get_queryset()
|
||||
|
||||
return generics.get_object_or_404(
|
||||
queryset,
|
||||
book_id=self.kwargs["book_id"]
|
||||
)
|
||||
|
||||
def get_serializer_context(self):
|
||||
context = super().get_serializer_context()
|
||||
|
||||
shelf = Shelf.objects.filter(
|
||||
id=self.kwargs["shelf_id"],
|
||||
user=self.request.user
|
||||
).first()
|
||||
|
||||
context["shelf"] = shelf
|
||||
return context
|
||||
|
||||
|
||||
Reference in New Issue
Block a user