Added some logic
This commit is contained in:
0
backend/apps/books/__init__.py
Normal file
0
backend/apps/books/__init__.py
Normal file
3
backend/apps/books/admin.py
Normal file
3
backend/apps/books/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
backend/apps/books/apps.py
Normal file
5
backend/apps/books/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class BooksConfig(AppConfig):
|
||||
name = 'apps.books'
|
||||
0
backend/apps/books/migrations/__init__.py
Normal file
0
backend/apps/books/migrations/__init__.py
Normal file
11
backend/apps/books/models.py
Normal file
11
backend/apps/books/models.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.db import models
|
||||
|
||||
from apps.shelves.models import Shelf
|
||||
from apps.authors.models import Author
|
||||
from apps.user.models import User
|
||||
|
||||
class Book(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Authors")
|
||||
authors = models.ManyToManyField(Author, related_name="books")
|
||||
name = models.CharField(max_length=255)
|
||||
isbn = models.CharField(max_length=13)
|
||||
25
backend/apps/books/serializers.py
Normal file
25
backend/apps/books/serializers.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.books.models import Book
|
||||
from apps.shelves.serializers import ShelfSerializer
|
||||
from apps.shelves.models import Shelf
|
||||
|
||||
class BookSerializer(serializers.ModelSerializer):
|
||||
#shelf = ShelfSerializer(read_only=True)
|
||||
#shelf_id = serializers.PrimaryKeyRelatedField(
|
||||
#queryset=Shelf.objects.all(),
|
||||
#source="shelf",
|
||||
#write_only=True
|
||||
#)
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Book
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"isbn",
|
||||
#"shelf",
|
||||
#"shelf_id"
|
||||
]
|
||||
read_only_fields = ["id"]
|
||||
3
backend/apps/books/tests.py
Normal file
3
backend/apps/books/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
9
backend/apps/books/urls.py
Normal file
9
backend/apps/books/urls.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from apps.books.views import BooksViewSet
|
||||
|
||||
urlpatterns = []
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("", viewset=BooksViewSet)
|
||||
urlpatterns += router.urls
|
||||
19
backend/apps/books/views.py
Normal file
19
backend/apps/books/views.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from rest_framework import viewsets
|
||||
|
||||
from apps.books.models import Book
|
||||
from apps.books.serializers import BookSerializer
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
|
||||
class BooksViewSet(viewsets.ModelViewSet):
|
||||
queryset = Book.objects.prefetch_related("authors")
|
||||
serializer_class = BookSerializer
|
||||
|
||||
authentication_classes = [JWTAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return self.queryset.filter(
|
||||
shelf__user_id=self.request.user.pk
|
||||
)
|
||||
Reference in New Issue
Block a user