From d0117b5a913daedfb3cd4714b20dcae5c24b99ab Mon Sep 17 00:00:00 2001 From: Bai Date: Mon, 11 Mar 2024 18:40:57 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E9=82=AE=E7=AE=B1?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/users/utils.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/apps/users/utils.py b/apps/users/utils.py index d75a8d761..006fbc3dd 100644 --- a/apps/users/utils.py +++ b/apps/users/utils.py @@ -230,24 +230,19 @@ class LoginIpBlockUtil(BlockGlobalIpUtilBase): BLOCK_KEY_TMPL = "_LOGIN_BLOCK_{}" -def validate_email(addr): - addr = addr or '' +def validate_emails(emails): pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' - if re.match(pattern, addr): - return addr - else: - return '' + for e in emails: + e = e or '' + if re.match(pattern, e): + return e def construct_user_email(username, email, email_suffix=''): - email = validate_email(email) - if not email: - email = validate_email(username) - - if not email: - email_suffix = email_suffix or settings.EMAIL_SUFFIX - email = f'{username}@{email_suffix}' - return email + default = f'{username}@{email_suffix or settings.EMAIL_SUFFIX}' + emails = [email, username] + email = validate_emails(emails) + return email or default def get_current_org_members():