♻️ Refactor email logic to allow re-using util functions for testing and development (#663)
This commit is contained in:

committed by
GitHub

parent
c048a61d81
commit
d4e35a0dfd
@@ -3,7 +3,7 @@ from typing import Annotated
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jose import jwt
|
||||
from jose import JWTError, jwt
|
||||
from pydantic import ValidationError
|
||||
from sqlmodel import Session
|
||||
|
||||
@@ -32,7 +32,7 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
|
||||
token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
|
||||
)
|
||||
token_data = TokenPayload(**payload)
|
||||
except (jwt.JWTError, ValidationError):
|
||||
except (JWTError, ValidationError):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Could not validate credentials",
|
||||
|
@@ -12,7 +12,8 @@ from app.core.security import get_password_hash
|
||||
from app.models import Message, NewPassword, Token, User, UserOut
|
||||
from app.utils import (
|
||||
generate_password_reset_token,
|
||||
send_reset_password_email,
|
||||
generate_reset_password_email,
|
||||
send_email,
|
||||
verify_password_reset_token,
|
||||
)
|
||||
|
||||
@@ -62,9 +63,14 @@ def recover_password(email: str, session: SessionDep) -> Message:
|
||||
detail="The user with this username does not exist in the system.",
|
||||
)
|
||||
password_reset_token = generate_password_reset_token(email=email)
|
||||
send_reset_password_email(
|
||||
email_data = generate_reset_password_email(
|
||||
email_to=user.email, email=email, token=password_reset_token
|
||||
)
|
||||
send_email(
|
||||
email_to=user.email,
|
||||
subject=email_data.subject,
|
||||
html_content=email_data.html_content,
|
||||
)
|
||||
return Message(message="Password recovery email sent")
|
||||
|
||||
|
||||
|
@@ -23,7 +23,7 @@ from app.models import (
|
||||
UserUpdate,
|
||||
UserUpdateMe,
|
||||
)
|
||||
from app.utils import send_new_account_email
|
||||
from app.utils import generate_new_account_email, send_email
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -61,9 +61,14 @@ def create_user(*, session: SessionDep, user_in: UserCreate) -> Any:
|
||||
|
||||
user = crud.create_user(session=session, user_create=user_in)
|
||||
if settings.EMAILS_ENABLED and user_in.email:
|
||||
send_new_account_email(
|
||||
email_data = generate_new_account_email(
|
||||
email_to=user_in.email, username=user_in.email, password=user_in.password
|
||||
)
|
||||
send_email(
|
||||
email_to=user_in.email,
|
||||
subject=email_data.subject,
|
||||
html_content=email_data.html_content,
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
@@ -185,7 +190,9 @@ def delete_user(
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
if (user == current_user and not current_user.is_superuser) or (user != current_user and current_user.is_superuser):
|
||||
if (user == current_user and not current_user.is_superuser) or (
|
||||
user != current_user and current_user.is_superuser
|
||||
):
|
||||
statement = delete(Item).where(Item.owner_id == user_id)
|
||||
session.exec(statement)
|
||||
session.delete(user)
|
||||
|
@@ -4,7 +4,7 @@ from pydantic.networks import EmailStr
|
||||
from app.api.deps import get_current_active_superuser
|
||||
from app.core.celery_app import celery_app
|
||||
from app.models import Message
|
||||
from app.utils import send_test_email
|
||||
from app.utils import generate_test_email, send_email
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -31,5 +31,10 @@ def test_email(email_to: EmailStr) -> Message:
|
||||
"""
|
||||
Test emails.
|
||||
"""
|
||||
send_test_email(email_to=email_to)
|
||||
email_data = generate_test_email(email_to=email_to)
|
||||
send_email(
|
||||
email_to=email_to,
|
||||
subject=email_data.subject,
|
||||
html_content=email_data.html_content,
|
||||
)
|
||||
return Message(message="Test email sent")
|
||||
|
Reference in New Issue
Block a user