Added some logic
This commit is contained in:
37
backend/apps/authentication/serializers.py
Normal file
37
backend/apps/authentication/serializers.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from django.contrib.auth.password_validation import validate_password as django_validate_password
|
||||
from django.core.exceptions import ValidationError
|
||||
from rest_framework import serializers
|
||||
from apps.user.models import User
|
||||
|
||||
|
||||
class RegisterSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["username", "email", "password"]
|
||||
extra_kwargs = {
|
||||
"password": {"write_only": True}
|
||||
}
|
||||
|
||||
def validate_email(self, value):
|
||||
if User.objects.filter(email=value).exists():
|
||||
raise serializers.ValidationError("Email already taken")
|
||||
return value
|
||||
|
||||
def validate_password(self, value):
|
||||
try:
|
||||
django_validate_password(value)
|
||||
except ValidationError as error:
|
||||
raise serializers.ValidationError(error.messages)
|
||||
|
||||
return value
|
||||
|
||||
def create(self, validated_data):
|
||||
user = User.objects.create_user(
|
||||
validated_data["username"],
|
||||
validated_data["email"],
|
||||
validated_data["password"]
|
||||
)
|
||||
|
||||
|
||||
return user
|
||||
@@ -5,9 +5,14 @@ from rest_framework_simplejwt.views import (
|
||||
TokenVerifyView
|
||||
)
|
||||
|
||||
from apps.authentication.views import RegisterView
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
|
||||
#path('signup/', ),
|
||||
path('register', RegisterView.as_view()),
|
||||
|
||||
|
||||
|
||||
|
||||
path('refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||
path('verify/', TokenVerifyView.as_view(), name='token_verify'),
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework import generics
|
||||
|
||||
from apps.authentication.serializers import RegisterSerializer
|
||||
|
||||
# Create your views here.
|
||||
class RegisterView(generics.CreateAPIView):
|
||||
serializer_class = RegisterSerializer
|
||||
authentication_classes = []
|
||||
permission_classes = []
|
||||
0
backend/apps/authors/__init__.py
Normal file
0
backend/apps/authors/__init__.py
Normal file
3
backend/apps/authors/admin.py
Normal file
3
backend/apps/authors/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
backend/apps/authors/apps.py
Normal file
5
backend/apps/authors/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AuthorsConfig(AppConfig):
|
||||
name = 'apps.authors'
|
||||
0
backend/apps/authors/migrations/__init__.py
Normal file
0
backend/apps/authors/migrations/__init__.py
Normal file
6
backend/apps/authors/models.py
Normal file
6
backend/apps/authors/models.py
Normal file
@@ -0,0 +1,6 @@
|
||||
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)
|
||||
0
backend/apps/authors/serializers.py
Normal file
0
backend/apps/authors/serializers.py
Normal file
3
backend/apps/authors/tests.py
Normal file
3
backend/apps/authors/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
9
backend/apps/authors/urls.py
Normal file
9
backend/apps/authors/urls.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from apps.authors.views import AuthorsViewSet
|
||||
|
||||
urlpatterns = []
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("", viewset=AuthorsViewSet)
|
||||
urlpatterns += router.urls
|
||||
20
backend/apps/authors/views.py
Normal file
20
backend/apps/authors/views.py
Normal file
@@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
class AuthorsViewSet(viewsets.ModelViewSet):
|
||||
queryset = Author.objects.select_related("shelf")
|
||||
serializer_class = AuthorSerializer
|
||||
|
||||
authentication_classes = [JWTAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return self.queryset.filter(
|
||||
shelf__user_id=self.request.user.pk
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
0
backend/apps/categories/__init__.py
Normal file
0
backend/apps/categories/__init__.py
Normal file
3
backend/apps/categories/admin.py
Normal file
3
backend/apps/categories/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
backend/apps/categories/apps.py
Normal file
5
backend/apps/categories/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CategoriesConfig(AppConfig):
|
||||
name = 'categories'
|
||||
0
backend/apps/categories/migrations/__init__.py
Normal file
0
backend/apps/categories/migrations/__init__.py
Normal file
3
backend/apps/categories/models.py
Normal file
3
backend/apps/categories/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
backend/apps/categories/tests.py
Normal file
3
backend/apps/categories/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
backend/apps/categories/views.py
Normal file
3
backend/apps/categories/views.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
0
backend/apps/notes/__init__.py
Normal file
0
backend/apps/notes/__init__.py
Normal file
3
backend/apps/notes/admin.py
Normal file
3
backend/apps/notes/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
backend/apps/notes/apps.py
Normal file
5
backend/apps/notes/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class NotesConfig(AppConfig):
|
||||
name = 'notes'
|
||||
0
backend/apps/notes/migrations/__init__.py
Normal file
0
backend/apps/notes/migrations/__init__.py
Normal file
3
backend/apps/notes/models.py
Normal file
3
backend/apps/notes/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
backend/apps/notes/tests.py
Normal file
3
backend/apps/notes/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
backend/apps/notes/views.py
Normal file
3
backend/apps/notes/views.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
0
backend/apps/shelf_items/__init__.py
Normal file
0
backend/apps/shelf_items/__init__.py
Normal file
3
backend/apps/shelf_items/admin.py
Normal file
3
backend/apps/shelf_items/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
backend/apps/shelf_items/apps.py
Normal file
5
backend/apps/shelf_items/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ShelfItemsConfig(AppConfig):
|
||||
name = 'shelf_items'
|
||||
0
backend/apps/shelf_items/migrations/__init__.py
Normal file
0
backend/apps/shelf_items/migrations/__init__.py
Normal file
3
backend/apps/shelf_items/models.py
Normal file
3
backend/apps/shelf_items/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
backend/apps/shelf_items/tests.py
Normal file
3
backend/apps/shelf_items/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
backend/apps/shelf_items/views.py
Normal file
3
backend/apps/shelf_items/views.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
@@ -14,6 +14,8 @@ class ShelvesViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Shelf.objects.filter(user_id=self.request.user.pk)
|
||||
|
||||
return self.queryset.filter(
|
||||
user_id=self.request.user.pk
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@ from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
|
||||
from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("auth", include("apps.authentication.urls")),
|
||||
path("shelves", include("apps.shelves.urls")),
|
||||
path("auth/", include("apps.authentication.urls")),
|
||||
path("shelves/", include("apps.shelves.urls")),
|
||||
path("books/", include("apps.books.urls")),
|
||||
path("authors/", include("apps.authors.urls")),
|
||||
|
||||
path('schema/', SpectacularAPIView.as_view(), name='schema'),
|
||||
path('swagger/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
||||
|
||||
@@ -3,3 +3,9 @@ from django.apps import AppConfig
|
||||
|
||||
class UserConfig(AppConfig):
|
||||
name = 'apps.user'
|
||||
|
||||
|
||||
def ready(self):
|
||||
|
||||
|
||||
return super().ready()
|
||||
0
backend/apps/user/signals.py
Normal file
0
backend/apps/user/signals.py
Normal file
@@ -19,6 +19,8 @@ INSTALLED_APPS = [
|
||||
"apps.authentication.apps.AuthenticationConfig",
|
||||
"apps.user.apps.UserConfig",
|
||||
"apps.shelves.apps.ShelvesConfig",
|
||||
"apps.books.apps.BooksConfig",
|
||||
"apps.authors.apps.AuthorsConfig",
|
||||
"django.contrib.admin",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
|
||||
@@ -4,4 +4,5 @@ djangorestframework==3.16.1
|
||||
django-filter==25.2
|
||||
djangorestframework-simplejwt==5.5.1
|
||||
drf-spectacular==0.29.0
|
||||
pillow==12.1.1
|
||||
pillow==12.1.1
|
||||
celery==5.6.2
|
||||
Reference in New Issue
Block a user