From 6fa86cb5b2308702a5246ad6ea6b7b35e79770d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Wed, 20 Nov 2024 14:27:18 +0100 Subject: [PATCH] refactor: PEP20 flat is better than nested --- canaille/app/mails_sending_conditions.py | 71 +++++++++++++----------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/canaille/app/mails_sending_conditions.py b/canaille/app/mails_sending_conditions.py index 5f979072..14b210bd 100644 --- a/canaille/app/mails_sending_conditions.py +++ b/canaille/app/mails_sending_conditions.py @@ -6,41 +6,46 @@ from canaille.core.mails import send_compromised_password_check_failure_mail def check_if_send_mail_to_admins(form, api_url, hashed_password_suffix): - if current_app.features.has_smtp and current_app.config["CANAILLE"]["ADMIN_EMAIL"]: + if ( + not current_app.features.has_smtp + or not current_app.config["CANAILLE"]["ADMIN_EMAIL"] + ): + return + + flash( + _( + "Password compromise investigation failed. " + "Please contact the administrators." + ), + "error", + ) + + if form.user is not None: + user_name = form.user.user_name + user_email = form.user.emails[0] + else: + user_name = form["user_name"].data + user_email = form["emails"].data[0] + + if send_compromised_password_check_failure_mail( + api_url, + user_name, + user_email, + hashed_password_suffix, + current_app.config["CANAILLE"]["ADMIN_EMAIL"], + ): flash( _( - "Password compromise investigation failed. " - "Please contact the administrators." + "We have informed your administrator about the failure of the password compromise investigation." + ), + "info", + ) + else: + flash( + _( + "An error occurred while communicating the incident to the administrators. " + "Please update your password as soon as possible. " + "If this still happens, please contact the administrators." ), "error", ) - - if form.user is not None: - user_name = form.user.user_name - user_email = form.user.emails[0] - else: - user_name = form["user_name"].data - user_email = form["emails"].data[0] - - if send_compromised_password_check_failure_mail( - api_url, - user_name, - user_email, - hashed_password_suffix, - current_app.config["CANAILLE"]["ADMIN_EMAIL"], - ): - flash( - _( - "We have informed your administrator about the failure of the password compromise investigation." - ), - "info", - ) - else: - flash( - _( - "An error occurred while communicating the incident to the administrators. " - "Please update your password as soon as possible. " - "If this still happens, please contact the administrators." - ), - "error", - )