Added some logic

This commit is contained in:
maciejrusek
2026-03-12 23:24:14 +01:00
parent ca605ac59d
commit 8b55be1b9c
48 changed files with 240 additions and 7 deletions

View 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

View File

@@ -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'),

View File

@@ -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 = []

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class AuthorsConfig(AppConfig):
name = 'apps.authors'

View 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)

View File

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View 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

View 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
)

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class BooksConfig(AppConfig):
name = 'apps.books'

View 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)

View 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"]

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View 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

View 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
)

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class CategoriesConfig(AppConfig):
name = 'categories'

View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class NotesConfig(AppConfig):
name = 'notes'

View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class ShelfItemsConfig(AppConfig):
name = 'shelf_items'

View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@@ -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
)

View File

@@ -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'),

View File

@@ -3,3 +3,9 @@ from django.apps import AppConfig
class UserConfig(AppConfig):
name = 'apps.user'
def ready(self):
return super().ready()

View File

View 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",

View File

@@ -5,3 +5,4 @@ django-filter==25.2
djangorestframework-simplejwt==5.5.1
drf-spectacular==0.29.0
pillow==12.1.1
celery==5.6.2