From 11b1ea014f26d7a78b2a87b5745f8b72e755b0e0 Mon Sep 17 00:00:00 2001 From: urec56 Date: Fri, 9 Feb 2024 21:00:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20celery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chat_test/.env_template | 10 +++++++- chat_test/app/config.py | 8 ++++++ chat_test/app/tasks/celery.py | 10 ++++++++ chat_test/app/tasks/email_templates.py | 26 ++++++++++++++++++++ chat_test/app/tasks/tasks.py | 34 ++++++++++++++++++++++++++ chat_test/app/users/router.py | 2 ++ 6 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 chat_test/app/tasks/celery.py create mode 100644 chat_test/app/tasks/email_templates.py create mode 100644 chat_test/app/tasks/tasks.py diff --git a/chat_test/.env_template b/chat_test/.env_template index 012887d..e702b27 100644 --- a/chat_test/.env_template +++ b/chat_test/.env_template @@ -5,4 +5,12 @@ DB_PASS= DB_NAME= SECRET_KEY= -ALGORITHM= \ No newline at end of file +ALGORITHM= + +REDIS_HOST= +REDIS_PORT= + +SMTP_HOST= +SMTP_PORT= +SMTP_USER= +SMTP_PASS= \ No newline at end of file diff --git a/chat_test/app/config.py b/chat_test/app/config.py index 97cd937..1b2ba5e 100644 --- a/chat_test/app/config.py +++ b/chat_test/app/config.py @@ -11,6 +11,14 @@ class Settings(BaseSettings): SECRET_KEY: str ALGORITHM: str + REDIS_HOST: str + REDIS_PORT: int + + SMTP_HOST: str + SMTP_PORT: int + SMTP_USER: str + SMTP_PASS: str + class Config: env_file = ".env" diff --git a/chat_test/app/tasks/celery.py b/chat_test/app/tasks/celery.py new file mode 100644 index 0000000..17b7824 --- /dev/null +++ b/chat_test/app/tasks/celery.py @@ -0,0 +1,10 @@ +from celery import Celery + + +from app.config import settings + +celery = Celery( + "tasks", + broker=f'redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}', + include=['app.tasks.tasks'] +) diff --git a/chat_test/app/tasks/email_templates.py b/chat_test/app/tasks/email_templates.py new file mode 100644 index 0000000..fa58ed2 --- /dev/null +++ b/chat_test/app/tasks/email_templates.py @@ -0,0 +1,26 @@ +from email.message import EmailMessage + +from pydantic import EmailStr + +from app.config import settings + + +def create_registration_confirmation_template( + username: str, + email_to: EmailStr, + confirmation_code: str, +): + email = EmailMessage() + + email["Subject"] = "Подтверждение регистрации" + email["From"] = settings.SMTP_USER + email["To"] = email_to + + email.set_content( + f""" +

{username}, лови аптечку

+

{confirmation_code}

+ """, + subtype="html" + ) + return email diff --git a/chat_test/app/tasks/tasks.py b/chat_test/app/tasks/tasks.py new file mode 100644 index 0000000..7723bc7 --- /dev/null +++ b/chat_test/app/tasks/tasks.py @@ -0,0 +1,34 @@ +import smtplib +import random +import string +from pathlib import Path + +from PIL import Image +from pydantic import EmailStr + +from app.config import settings +from app.tasks.celery import celery +from app.tasks.email_templates import create_registration_confirmation_template + + +def generate_confirmation_code(length=6): + characters = string.ascii_letters + string.digits + confirmation_code = ''.join(random.choice(characters) for _ in range(length)) + return confirmation_code + + +@celery.task +def send_registration_confirmation_email( + username: str, + email_to: EmailStr, +): + confirmation_code = generate_confirmation_code() + + msg_content = create_registration_confirmation_template( + username=username, email_to=email_to, confirmation_code=confirmation_code + ) + + with smtplib.SMTP_SSL(settings.SMTP_HOST, settings.SMTP_PORT) as server: + server.login(settings.SMTP_USER, settings.SMTP_PASS) + server.send_message(msg_content) + diff --git a/chat_test/app/users/router.py b/chat_test/app/users/router.py index 0c3061a..4d1f5c3 100644 --- a/chat_test/app/users/router.py +++ b/chat_test/app/users/router.py @@ -10,6 +10,7 @@ from app.users.dao import UserDAO from app.users.dependencies import get_current_user from app.users.models import Users from app.users.schemas import SUserLogin, SUserRegister, SUser, SUserName, SUserPassword +from app.tasks.tasks import send_registration_confirmation_email router = APIRouter( prefix="/users", @@ -45,6 +46,7 @@ async def register_user(response: Response, user_data: SUserRegister): role=0, black_phoenix=False ) + send_registration_confirmation_email.delay(username=user_data.username, email_to=user_data.email) user = await authenticate_user_by_email(user_data.email, user_data.password) access_token = create_access_token({"sub": str(user.id)}) response.set_cookie(key="black_phoenix_access_token", value=access_token, httponly=True)