From 4922adf5c939d0490317e765f1934f451180b249 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Tue, 27 Feb 2024 13:25:55 +0100 Subject: [PATCH] refactor(chalice): optimised forget password --- api/chalicelib/core/reset_password.py | 8 ++++++-- api/routers/core.py | 11 +++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/api/chalicelib/core/reset_password.py b/api/chalicelib/core/reset_password.py index 4fe3c5548..cf3035494 100644 --- a/api/chalicelib/core/reset_password.py +++ b/api/chalicelib/core/reset_password.py @@ -1,5 +1,7 @@ import logging +from fastapi import BackgroundTasks + import schemas from chalicelib.core import users from chalicelib.utils import email_helper, captcha, helper, smtp @@ -7,7 +9,7 @@ from chalicelib.utils import email_helper, captcha, helper, smtp logger = logging.getLogger(__name__) -def reset(data: schemas.ForgetPasswordPayloadSchema): +def reset(data: schemas.ForgetPasswordPayloadSchema, background_tasks: BackgroundTasks): logger.info(f"forget password request for: {data.email}") if helper.allow_captcha() and not captcha.is_valid(data.g_recaptcha_response): return {"errors": ["Invalid captcha."]} @@ -16,7 +18,9 @@ def reset(data: schemas.ForgetPasswordPayloadSchema): a_users = users.get_by_email_only(data.email) if a_users: invitation_link = users.generate_new_invitation(user_id=a_users["userId"]) - email_helper.send_forgot_password(recipient=data.email, invitation_link=invitation_link) + background_tasks.add_task(email_helper.send_forgot_password, + recipient=data.email, + invitation_link=invitation_link) else: logger.warning(f"!!!invalid email address [{data.email}]") return {"data": {"state": "A reset link will be sent if this email exists in our system."}} diff --git a/api/routers/core.py b/api/routers/core.py index 3580827f0..7ef67194e 100644 --- a/api/routers/core.py +++ b/api/routers/core.py @@ -1,7 +1,7 @@ from typing import Union from decouple import config -from fastapi import Depends, Body +from fastapi import Depends, Body, BackgroundTasks import schemas from chalicelib.core import log_tool_rollbar, sourcemaps, events, sessions_assignments, projects, \ @@ -448,7 +448,7 @@ def edit_gdpr(projectId: int, data: schemas.GdprSchema = Body(...), @public_app.post('/password/reset-link', tags=["reset password"]) -def reset_password_handler(data: schemas.ForgetPasswordPayloadSchema = Body(...)): +def reset_password_handler(background_tasks: BackgroundTasks, data: schemas.ForgetPasswordPayloadSchema = Body(...)): if len(data.email) < 5: return {"errors": ["please provide a valid email address"]} return reset_password.reset(data=data) @@ -872,16 +872,19 @@ async def check_recording_status(project_id: int): def health_check(): return {} + # tags @app.post('/{projectId}/tags', tags=["tags"]) -def tags_create(projectId: int, data: schemas.TagCreate = Body(), context: schemas.CurrentContext = Depends(OR_context)): +def tags_create(projectId: int, data: schemas.TagCreate = Body(), + context: schemas.CurrentContext = Depends(OR_context)): data = tags.create_tag(project_id=projectId, data=data) return {'data': data} @app.put('/{projectId}/tags/{tagId}', tags=["tags"]) -def tags_update(projectId: int, tagId: int, data: schemas.TagUpdate = Body(), context: schemas.CurrentContext = Depends(OR_context)): +def tags_update(projectId: int, tagId: int, data: schemas.TagUpdate = Body(), + context: schemas.CurrentContext = Depends(OR_context)): data = tags.update_tag(project_id=projectId, tag_id=tagId, data=data) return {'data': data}