Added some logic
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# from django.db import models
|
||||
# from apps.user.models import User
|
||||
from django.db import models
|
||||
from apps.user.models import User
|
||||
|
||||
# class Author(models.Model):
|
||||
# user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Authors")
|
||||
# name = models.CharField(max_length=255)
|
||||
class Author(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="authors")
|
||||
name = models.CharField(max_length=255)
|
||||
@@ -0,0 +1,16 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.authors.models import Author
|
||||
|
||||
class AuthorSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Author
|
||||
fields = ["id", "name"]
|
||||
read_only_fields = ["id"]
|
||||
|
||||
def create(self, validated_data):
|
||||
request = self.context["request"]
|
||||
validated_data["user"] = request.user
|
||||
|
||||
return super().create(validated_data)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
# from apps.authors.views import AuthorsViewSet
|
||||
from apps.authors.views import AuthorsViewSet
|
||||
|
||||
urlpatterns = []
|
||||
|
||||
# router = DefaultRouter()
|
||||
# router.register("", viewset=AuthorsViewSet)
|
||||
# urlpatterns += router.urls
|
||||
router = DefaultRouter()
|
||||
router.register("", viewset=AuthorsViewSet)
|
||||
urlpatterns += router.urls
|
||||
@@ -1,20 +1,20 @@
|
||||
# from rest_framework import viewsets
|
||||
from rest_framework import viewsets
|
||||
|
||||
# from apps.authors.models import Author
|
||||
# from apps.authors.serializers import AuthorSerializer
|
||||
# from rest_framework import viewsets
|
||||
# from rest_framework.permissions import IsAuthenticated
|
||||
# from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
from apps.authors.models import Author
|
||||
from apps.authors.serializers import AuthorSerializer
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
|
||||
# class AuthorsViewSet(viewsets.ModelViewSet):
|
||||
# queryset = Author.objects.select_related("shelf")
|
||||
# serializer_class = AuthorSerializer
|
||||
class AuthorsViewSet(viewsets.ModelViewSet):
|
||||
queryset = Author.objects.all()
|
||||
serializer_class = AuthorSerializer
|
||||
|
||||
# authentication_classes = [JWTAuthentication]
|
||||
# permission_classes = [IsAuthenticated]
|
||||
authentication_classes = [JWTAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
# def get_queryset(self):
|
||||
# return self.queryset.filter(
|
||||
# shelf__user_id=self.request.user.pk
|
||||
# )
|
||||
def get_queryset(self):
|
||||
return self.queryset.filter(
|
||||
user_id=self.request.user.pk
|
||||
)
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from django.db import models
|
||||
|
||||
from apps.shelves.models import Shelf
|
||||
# from apps.authors.models import Author
|
||||
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")
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="books")
|
||||
authors = models.ManyToManyField(Author, related_name="books")
|
||||
name = models.CharField(max_length=255)
|
||||
isbn = models.CharField(max_length=13)
|
||||
@@ -1,17 +1,27 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.books.models import Book
|
||||
from apps.shelves.serializers import ShelfSerializer
|
||||
from apps.authors.serializers import AuthorSerializer
|
||||
from apps.books.models import Book
|
||||
from apps.shelves.models import Shelf
|
||||
from apps.authors.models import Author
|
||||
|
||||
class BookSerializer(serializers.ModelSerializer):
|
||||
#shelf = ShelfSerializer(read_only=True)
|
||||
#shelf_id = serializers.PrimaryKeyRelatedField(
|
||||
#queryset=Shelf.objects.all(),
|
||||
#source="shelf",
|
||||
#write_only=True
|
||||
#)
|
||||
shelf = ShelfSerializer(read_only=True)
|
||||
shelf_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Shelf.objects.all(),
|
||||
source="shelf",
|
||||
write_only=True
|
||||
)
|
||||
|
||||
authors = AuthorSerializer(many=True, read_only=True)
|
||||
authors_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Author.objects.all(),
|
||||
source="authors",
|
||||
many=True,
|
||||
allow_null=True,
|
||||
write_only=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Book
|
||||
@@ -19,7 +29,30 @@ class BookSerializer(serializers.ModelSerializer):
|
||||
"id",
|
||||
"name",
|
||||
"isbn",
|
||||
#"shelf",
|
||||
#"shelf_id"
|
||||
"shelf",
|
||||
"shelf_id",
|
||||
"authors",
|
||||
"authors_id"
|
||||
]
|
||||
read_only_fields = ["id"]
|
||||
read_only_fields = ["id"]
|
||||
|
||||
|
||||
def __init__(self, instance=None, data=..., **kwargs):
|
||||
super().__init__(instance, data, **kwargs)
|
||||
|
||||
request = self.context.get("request")
|
||||
if request and request.user:
|
||||
self.fields["shelf_id"].queryset = Shelf.objects.filter(
|
||||
user=request.user
|
||||
)
|
||||
|
||||
self.fields["authors_id"].queryset = Author.objects.filter(
|
||||
user=request.user
|
||||
)
|
||||
|
||||
|
||||
def create(self, validated_data):
|
||||
request = self.context["request"]
|
||||
validated_data["user"] = request.user
|
||||
|
||||
return super().create(validated_data)
|
||||
@@ -16,4 +16,4 @@ class BooksViewSet(viewsets.ModelViewSet):
|
||||
def get_queryset(self):
|
||||
return self.queryset.filter(
|
||||
shelf__user_id=self.request.user.pk
|
||||
)
|
||||
).prefetch_related("authors")
|
||||
|
||||
@@ -2,4 +2,4 @@ from django.apps import AppConfig
|
||||
|
||||
|
||||
class ShelfItemsConfig(AppConfig):
|
||||
name = 'shelf_items'
|
||||
name = 'apps.shelf_items'
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
from django.db.models import Q, F
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
from apps.books.models import Book
|
||||
from apps.shelves.models import Shelf
|
||||
|
||||
class ShelfItem(models.Model):
|
||||
book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name="shelf_items")
|
||||
shelf = models.ForeignKey(Shelf, on_delete=models.CASCADE, related_name="book_items")
|
||||
|
||||
quantity = models.PositiveIntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["book", "shelf"],
|
||||
name="unique_book_per_shelf"
|
||||
),
|
||||
]
|
||||
77
backend/apps/shelf_items/serializers.py
Normal file
77
backend/apps/shelf_items/serializers.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.shelf_items.models import ShelfItem
|
||||
from apps.books.models import Book
|
||||
from apps.shelves.models import Shelf
|
||||
from apps.shelves.serializers import ShelfSerializer
|
||||
from apps.books.serializers import BookSerializer
|
||||
|
||||
class ShelfItemSerializer(serializers.ModelSerializer):
|
||||
shelf = ShelfSerializer(read_only=True)
|
||||
shelf_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Shelf.objects.all(),
|
||||
source="shelf",
|
||||
write_only=True
|
||||
)
|
||||
|
||||
book = BookSerializer(read_only=True)
|
||||
book_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Book.objects.all(),
|
||||
source="book",
|
||||
write_only=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = ShelfItem
|
||||
fields = [
|
||||
"id",
|
||||
"shelf",
|
||||
"shelf_id",
|
||||
"book",
|
||||
"book_id",
|
||||
"quantity"
|
||||
]
|
||||
read_only_fields = ["id"]
|
||||
|
||||
def __init__(self, instance=None, data=..., **kwargs):
|
||||
super().__init__(instance, data, **kwargs)
|
||||
|
||||
request = self.context.get("request")
|
||||
if request and request.user:
|
||||
self.fields["shelf_id"].queryset = Shelf.objects.filter(
|
||||
user=request.user
|
||||
)
|
||||
|
||||
self.fields["book_id"].queryset = Book.objects.filter(
|
||||
user=request.user
|
||||
)
|
||||
|
||||
def validate(self, attrs):
|
||||
attrs = super().validate(attrs)
|
||||
|
||||
book = attrs.get("book")
|
||||
shelf = attrs.get("shelf")
|
||||
|
||||
if book and shelf:
|
||||
qs = ShelfItem.objects.filter(
|
||||
book=book,
|
||||
shelf=shelf
|
||||
)
|
||||
|
||||
if self.instance:
|
||||
qs = qs.exclude(pk=self.instance.pk)
|
||||
|
||||
if qs.exists():
|
||||
raise serializers.ValidationError(
|
||||
"This Book already exists in shelf!"
|
||||
)
|
||||
|
||||
if book.user_id != shelf.user_id:
|
||||
raise serializers.ValidationError(
|
||||
"Book and Shelf must be from the same user!"
|
||||
)
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
|
||||
9
backend/apps/shelf_items/urls.py
Normal file
9
backend/apps/shelf_items/urls.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from apps.shelf_items.views import ShelfItemsViewSet
|
||||
|
||||
urlpatterns = []
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("", viewset=ShelfItemsViewSet)
|
||||
urlpatterns += router.urls
|
||||
@@ -1,3 +1,20 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework import viewsets
|
||||
|
||||
# Create your views here.
|
||||
from apps.shelf_items.models import ShelfItem
|
||||
from apps.shelf_items.serializers import ShelfItemSerializer
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
|
||||
|
||||
class ShelfItemsViewSet(viewsets.ModelViewSet):
|
||||
queryset = ShelfItem.objects.all()
|
||||
serializer_class = ShelfItemSerializer
|
||||
|
||||
authentication_classes = [JWTAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return ShelfItem.objects.filter(
|
||||
shelf__user_id=self.request.user.pk,
|
||||
).select_related("book", "shelf").prefetch_related("book__authors")
|
||||
|
||||
Reference in New Issue
Block a user