From dc4043acd9477a2626f6bf267ef682a3a2d2b6ac Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Oct 2023 16:17:37 +0200 Subject: [PATCH] add router mail --- app/main.py | 3 ++- app/routers/mail.py | 40 ++++++++++++++++++++++++++++++++++++++++ app/routers/users.py | 42 +++--------------------------------------- requierements.txt | 3 ++- 4 files changed, 47 insertions(+), 41 deletions(-) create mode 100644 app/routers/mail.py diff --git a/app/main.py b/app/main.py index 8297dde..f49d8ae 100644 --- a/app/main.py +++ b/app/main.py @@ -1,12 +1,13 @@ from fastapi import FastAPI -from .routers import users, token +from .routers import users, token, mail from .dependencies import user_add app = FastAPI() app.include_router(users.router) app.include_router(token.router) +app.include_router(mail.router) @app.on_event("startup") diff --git a/app/routers/mail.py b/app/routers/mail.py new file mode 100644 index 0000000..247210c --- /dev/null +++ b/app/routers/mail.py @@ -0,0 +1,40 @@ +from fastapi import APIRouter, HTTPException, status +from ..dependencies import users_token, database, mail +from ..models import users, email +from fastapi.responses import JSONResponse +from fastapi_mail import MessageSchema, MessageType, FastMail +import random +router = APIRouter() + + +@router.post("/mail",tags=["mail"]) +async def read_users_me(userSingle: users.UserCreate | None = None): + if userSingle is None: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Body request is empty" + ) + user_repository = users.UserRepository(database=database.database) + user = user_repository.find_one_by({"username": {'$eq': userSingle.username}}) + if user is not None: + raise HTTPException( + status_code=status.HTTP_204_NO_CONTENT, + detail="User is already exist" + ) + + fm = FastMail(mail.conf) + numberkey = str(random.Random()) + key_hashed = users_token.get_password_hash(numberkey) + email_body = {"key":key_hashed, "username":userSingle.username} + email_schema = email.EmailSchema(email=[userSingle.email], body=email_body) + message = MessageSchema( + subject="Fastapi-Mail module", + recipients=email_schema.dict().get("email"), + template_body=email_schema.dict().get("body"), + subtype=MessageType.html, + ) + + await fm.send_message(message, template_name="mailer.html") + current_user = users.User(username=userSingle.username, password=users_token.get_password_hash(userSingle.password), email=userSingle.email) + user_repository.save(current_user) + return JSONResponse(status_code=200, content={"message": "email has been sent"}) \ No newline at end of file diff --git a/app/routers/users.py b/app/routers/users.py index 8f365be..6b79929 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -1,11 +1,8 @@ from fastapi import APIRouter, Depends, HTTPException, status -from ..dependencies import users_token, permissions_checker, database, mail -from ..models import users, email +from ..dependencies import users_token, permissions_checker, database +from ..models import users from typing import Annotated from bson import ObjectId -from fastapi.responses import JSONResponse -from fastapi_mail import MessageSchema, MessageType, FastMail -import random router = APIRouter() @@ -105,37 +102,4 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P user.roles = userSingle.roles user.email = userSingle.email user_repository.save(user) - return user - - -@router.post("/users",tags=["users"]) -async def read_users_me(userSingle: users.UserCreate | None = None): - if userSingle is None: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Body request is empty" - ) - user_repository = users.UserRepository(database=database.database) - user = user_repository.find_one_by({"username": {'$eq': userSingle.username}}) - if user is not None: - raise HTTPException( - status_code=status.HTTP_204_NO_CONTENT, - detail="User is already exist" - ) - - fm = FastMail(mail.conf) - numberkey = str(random.Random()) - key_hashed = users_token.get_password_hash(numberkey) - email_body = {"key":key_hashed, "username":userSingle.username} - email_schema = email.EmailSchema(email=[userSingle.email], body=email_body) - message = MessageSchema( - subject="Fastapi-Mail module", - recipients=email_schema.dict().get("email"), - template_body=email_schema.dict().get("body"), - subtype=MessageType.html, - ) - - await fm.send_message(message, template_name="mailer.html") - current_user = users.User(username=userSingle.username, password=users_token.get_password_hash(userSingle.password), email=userSingle.email) - user_repository.save(current_user) - return JSONResponse(status_code=200, content={"message": "email has been sent"}) \ No newline at end of file + return user \ No newline at end of file diff --git a/requierements.txt b/requierements.txt index ad8071b..2645894 100644 --- a/requierements.txt +++ b/requierements.txt @@ -4,4 +4,5 @@ pydantic-mongo python-jose[cryptography] passlib[bcrypt] python-multipart -fastapi-mail \ No newline at end of file +fastapi-mail +redis \ No newline at end of file