From a938219b7b41d98ac4d3db641012fde74577492a Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 14:35:08 +0000 Subject: [PATCH 1/9] chore: refactor reset password to avoid duplicate code --- backend/app/api/routes/login.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index 980c66f86f..a31473b9be 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -1,22 +1,18 @@ from datetime import timedelta from typing import Annotated, Any -from fastapi import APIRouter, Depends, HTTPException -from fastapi.responses import HTMLResponse -from fastapi.security import OAuth2PasswordRequestForm - from app import crud from app.api.deps import CurrentUser, SessionDep, get_current_active_superuser from app.core import security from app.core.config import settings from app.core.security import get_password_hash from app.models import Message, NewPassword, Token, UserPublic -from app.utils import ( - generate_password_reset_token, - generate_reset_password_email, - send_email, - verify_password_reset_token, -) +from app.utils import (generate_password_reset_token, + generate_reset_password_email, send_email, + verify_password_reset_token) +from fastapi import APIRouter, Depends, HTTPException +from fastapi.responses import HTMLResponse +from fastapi.security import OAuth2PasswordRequestForm router = APIRouter(tags=["login"]) @@ -86,15 +82,16 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message: user = crud.get_user_by_email(session=session, email=email) if not user: raise HTTPException( - status_code=404, - detail="The user with this email does not exist in the system.", + status_code=404, detail="The user with this email does not exist." ) - elif not user.is_active: + if not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") - hashed_password = get_password_hash(password=body.new_password) - user.hashed_password = hashed_password - session.add(user) - session.commit() + + crud.update_user( + session=session, + db_user=user, + user_in={"password": body.new_password}, + ) return Message(message="Password updated successfully") From 83f6d292068328e08a3e4b55ef05b2d63359744b Mon Sep 17 00:00:00 2001 From: Younes Henni Date: Tue, 3 Dec 2024 14:38:54 +0000 Subject: [PATCH 2/9] chore: rollback auto import changes --- backend/app/api/routes/login.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index a31473b9be..c5570ea6b8 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -1,18 +1,22 @@ from datetime import timedelta from typing import Annotated, Any +from fastapi import APIRouter, Depends, HTTPException +from fastapi.responses import HTMLResponse +from fastapi.security import OAuth2PasswordRequestForm + from app import crud from app.api.deps import CurrentUser, SessionDep, get_current_active_superuser from app.core import security from app.core.config import settings from app.core.security import get_password_hash from app.models import Message, NewPassword, Token, UserPublic -from app.utils import (generate_password_reset_token, - generate_reset_password_email, send_email, - verify_password_reset_token) -from fastapi import APIRouter, Depends, HTTPException -from fastapi.responses import HTMLResponse -from fastapi.security import OAuth2PasswordRequestForm +from app.utils import ( + generate_password_reset_token, + generate_reset_password_email, + send_email, + verify_password_reset_token, +) router = APIRouter(tags=["login"]) From cbd91390d12ec543652f592d2358ccbd5e752617 Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 15:20:47 +0000 Subject: [PATCH 3/9] lint: fix linting error --- backend/app/api/routes/login.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index c5570ea6b8..57e1b8a38d 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -90,11 +90,12 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message: ) if not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") - + + user_in_update = UserUpdate(password=body.new_password) crud.update_user( session=session, db_user=user, - user_in={"password": body.new_password}, + user_in=user_in_update, ) return Message(message="Password updated successfully") From a72727429904ae72bf7d7a60ff7debf19ecd2725 Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 15:25:38 +0000 Subject: [PATCH 4/9] lint: add missing import --- backend/app/api/routes/login.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index 57e1b8a38d..fbbbb6bc02 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -10,7 +10,7 @@ from app.core import security from app.core.config import settings from app.core.security import get_password_hash -from app.models import Message, NewPassword, Token, UserPublic +from app.models import Message, NewPassword, Token, UserPublic, UserUpdate from app.utils import ( generate_password_reset_token, generate_reset_password_email, @@ -90,7 +90,6 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message: ) if not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") - user_in_update = UserUpdate(password=body.new_password) crud.update_user( session=session, From ffb3d80d3f30859687724a3e6c3cda7231000b67 Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 15:32:44 +0000 Subject: [PATCH 5/9] lint: remove unused import --- backend/app/api/routes/login.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index fbbbb6bc02..0e470b9e67 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -9,7 +9,6 @@ from app.api.deps import CurrentUser, SessionDep, get_current_active_superuser from app.core import security from app.core.config import settings -from app.core.security import get_password_hash from app.models import Message, NewPassword, Token, UserPublic, UserUpdate from app.utils import ( generate_password_reset_token, From 12d92c5a0b3029fe2330b4acab86176c9fcb3510 Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 21:48:57 +0000 Subject: [PATCH 6/9] chore: refactor if statement --- backend/app/api/routes/login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index 0e470b9e67..0d7c0a7b3a 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -87,7 +87,7 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message: raise HTTPException( status_code=404, detail="The user with this email does not exist." ) - if not user.is_active: + elif not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") user_in_update = UserUpdate(password=body.new_password) crud.update_user( From f80ae2be6049ca85e186eeeedeeead13b72baed6 Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 21:51:47 +0000 Subject: [PATCH 7/9] chore: edit error msg --- backend/app/api/routes/login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index 0d7c0a7b3a..0656042ce1 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -85,7 +85,7 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message: user = crud.get_user_by_email(session=session, email=email) if not user: raise HTTPException( - status_code=404, detail="The user with this email does not exist." + status_code=404, detail="The user with this email does not exist in the system." ) elif not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") From 113b6bebff09a7f3eb5dd77dbf77824889c29377 Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 21:55:21 +0000 Subject: [PATCH 8/9] lint: fix formatting --- backend/app/api/routes/login.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index 0656042ce1..17eb774c86 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -85,7 +85,8 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message: user = crud.get_user_by_email(session=session, email=email) if not user: raise HTTPException( - status_code=404, detail="The user with this email does not exist in the system." + status_code=404, + detail="The user with this email does not exist in the system." ) elif not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") From 29517571f480b9d053b6461453be6ecb7f1a316d Mon Sep 17 00:00:00 2001 From: youneshenniwrites Date: Tue, 3 Dec 2024 21:57:03 +0000 Subject: [PATCH 9/9] lint: fix formatting --- backend/app/api/routes/login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/api/routes/login.py b/backend/app/api/routes/login.py index 17eb774c86..87b04d11a7 100644 --- a/backend/app/api/routes/login.py +++ b/backend/app/api/routes/login.py @@ -86,7 +86,7 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message: if not user: raise HTTPException( status_code=404, - detail="The user with this email does not exist in the system." + detail="The user with this email does not exist in the system.", ) elif not user.is_active: raise HTTPException(status_code=400, detail="Inactive user")