Added celery and rabbitmq

This commit is contained in:
maciejrusek
2026-03-17 20:42:58 +01:00
parent 8b55be1b9c
commit e0ff3e1cf6
11 changed files with 59 additions and 27 deletions

View File

@@ -2,6 +2,7 @@ from django.contrib.auth.password_validation import validate_password as django_
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from rest_framework import serializers from rest_framework import serializers
from apps.user.models import User from apps.user.models import User
from apps.tasks import send_email
class RegisterSerializer(serializers.ModelSerializer): class RegisterSerializer(serializers.ModelSerializer):
@@ -33,5 +34,6 @@ class RegisterSerializer(serializers.ModelSerializer):
validated_data["password"] validated_data["password"]
) )
send_email.delay(user.pk)
return user return user

View File

@@ -1,6 +1,6 @@
from django.db import models # from django.db import models
from apps.user.models import User # from apps.user.models import User
class Author(models.Model): # class Author(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Authors") # user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Authors")
name = models.CharField(max_length=255) # name = models.CharField(max_length=255)

View File

@@ -1,9 +1,9 @@
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from apps.authors.views import AuthorsViewSet # from apps.authors.views import AuthorsViewSet
urlpatterns = [] urlpatterns = []
router = DefaultRouter() # router = DefaultRouter()
router.register("", viewset=AuthorsViewSet) # router.register("", viewset=AuthorsViewSet)
urlpatterns += router.urls # urlpatterns += router.urls

View File

@@ -1,20 +1,20 @@
from rest_framework import viewsets # from rest_framework import viewsets
from apps.authors.models import Author # from apps.authors.models import Author
from apps.authors.serializers import AuthorSerializer # from apps.authors.serializers import AuthorSerializer
from rest_framework import viewsets # from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated # from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication # from rest_framework_simplejwt.authentication import JWTAuthentication
class AuthorsViewSet(viewsets.ModelViewSet): # class AuthorsViewSet(viewsets.ModelViewSet):
queryset = Author.objects.select_related("shelf") # queryset = Author.objects.select_related("shelf")
serializer_class = AuthorSerializer # serializer_class = AuthorSerializer
authentication_classes = [JWTAuthentication] # authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated] # permission_classes = [IsAuthenticated]
def get_queryset(self): # def get_queryset(self):
return self.queryset.filter( # return self.queryset.filter(
shelf__user_id=self.request.user.pk # shelf__user_id=self.request.user.pk
) # )

View File

@@ -1,11 +1,11 @@
from django.db import models from django.db import models
from apps.shelves.models import Shelf from apps.shelves.models import Shelf
from apps.authors.models import Author # from apps.authors.models import Author
from apps.user.models import User from apps.user.models import User
class Book(models.Model): class Book(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Authors") user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Authors")
authors = models.ManyToManyField(Author, related_name="books") # authors = models.ManyToManyField(Author, related_name="books")
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
isbn = models.CharField(max_length=13) isbn = models.CharField(max_length=13)

11
backend/apps/tasks.py Normal file
View File

@@ -0,0 +1,11 @@
from celery import shared_task
@shared_task
def send_email(user_pk):
print(user_pk)
# if sended
# zmienna info itp, jesli sie straci
return None

View File

@@ -0,0 +1,3 @@
from config.celery import app as celery_app
__all__ = ('celery_app',)

11
backend/config/celery.py Normal file
View File

@@ -0,0 +1,11 @@
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.django.dev')
app = Celery('library')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

View File

@@ -131,3 +131,5 @@ USE_TZ = True
# https://docs.djangoproject.com/en/6.0/howto/static-files/ # https://docs.djangoproject.com/en/6.0/howto/static-files/
STATIC_URL = "static/" STATIC_URL = "static/"
from config.settings.celery import *

View File

View File

@@ -0,0 +1,3 @@
from config.env import env
CELERY_BROKER_URL = env("CELERY_BROKER_URL")