From d5c361855e9b3cd2d148215c22426a11f177fbfa Mon Sep 17 00:00:00 2001 From: Johannes Alneberg Date: Tue, 15 Feb 2022 11:38:07 +0100 Subject: [PATCH 1/3] Fix not sending invite emails --- dds_web/api/user.py | 130 ++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 64 deletions(-) diff --git a/dds_web/api/user.py b/dds_web/api/user.py index d9a0f0165..ac08d9f33 100644 --- a/dds_web/api/user.py +++ b/dds_web/api/user.py @@ -220,75 +220,77 @@ def add_user_to_project(existing_user, project, role): @staticmethod @logging_bind_request def compose_and_send_email_to_user(userobj, mail_type, link=None, project=None): - if hasattr(userobj, "emails") and userobj.emails: - # Compose and send email - unit_name = None - unit_email = None - project_id = None - deadline = None - if auth.current_user().role in ["Unit Admin", "Unit Personnel"]: - unit = auth.current_user().unit - unit_name = unit.external_display_name - unit_email = unit.contact_email - sender_name = auth.current_user().name - subject_subject = unit_name - - else: - sender_name = auth.current_user().name - subject_subject = sender_name - - # Fill in email subject with sentence subject - if mail_type == "invite": - subject = f"{subject_subject} invites you to the SciLifeLab Data Delivery System" - recepients = [userobj.email] - elif mail_type == "project_release": - subject = f"Project made available by {subject_subject} in the SciLifeLab Data Delivery System" - recepients = [x.email for x in userobj.emails] - project_id = project.public_id - deadline = project.current_deadline.astimezone(datetime.timezone.utc).strftime( - "%Y-%m-%d %H:%M:%S %Z" - ) - else: - raise ddserr.DDSArgumentError(message="Invalid mail type!") + flask.current_app.logger.info( + f"Sending email to user: {userobj} with {hasattr(userobj, 'emails')}" + ) + # Compose and send email + unit_name = None + unit_email = None + project_id = None + deadline = None + if auth.current_user().role in ["Unit Admin", "Unit Personnel"]: + unit = auth.current_user().unit + unit_name = unit.external_display_name + unit_email = unit.contact_email + sender_name = auth.current_user().name + subject_subject = unit_name - msg = flask_mail.Message( - subject, - recipients=recepients, + else: + sender_name = auth.current_user().name + subject_subject = sender_name + + # Fill in email subject with sentence subject + if mail_type == "invite": + subject = f"{subject_subject} invites you to the SciLifeLab Data Delivery System" + recepients = [userobj.email] + elif mail_type == "project_release": + subject = f"Project made available by {subject_subject} in the SciLifeLab Data Delivery System" + recepients = [x.email for x in userobj.emails] + project_id = project.public_id + deadline = project.current_deadline.astimezone(datetime.timezone.utc).strftime( + "%Y-%m-%d %H:%M:%S %Z" ) + else: + raise ddserr.DDSArgumentError(message="Invalid mail type!") - # Need to attach the image to be able to use it - msg.attach( - "scilifelab_logo.png", - "image/png", - open( - os.path.join(flask.current_app.static_folder, "img/scilifelab_logo.png"), "rb" - ).read(), - "inline", - headers=[ - ["Content-ID", ""], - ], - ) + msg = flask_mail.Message( + subject, + recipients=recepients, + ) - msg.body = flask.render_template( - f"mail/{mail_type}.txt", - link=link, - sender_name=sender_name, - unit_name=unit_name, - unit_email=unit_email, - project_id=project_id, - deadline=deadline, - ) - msg.html = flask.render_template( - f"mail/{mail_type}.html", - link=link, - sender_name=sender_name, - unit_name=unit_name, - unit_email=unit_email, - project_id=project_id, - deadline=deadline, - ) + # Need to attach the image to be able to use it + msg.attach( + "scilifelab_logo.png", + "image/png", + open( + os.path.join(flask.current_app.static_folder, "img/scilifelab_logo.png"), "rb" + ).read(), + "inline", + headers=[ + ["Content-ID", ""], + ], + ) + + msg.body = flask.render_template( + f"mail/{mail_type}.txt", + link=link, + sender_name=sender_name, + unit_name=unit_name, + unit_email=unit_email, + project_id=project_id, + deadline=deadline, + ) + msg.html = flask.render_template( + f"mail/{mail_type}.html", + link=link, + sender_name=sender_name, + unit_name=unit_name, + unit_email=unit_email, + project_id=project_id, + deadline=deadline, + ) - AddUser.send_email_with_retry(msg) + AddUser.send_email_with_retry(msg) class RetrieveUserInfo(flask_restful.Resource): From 0b60bcf71ddc2c4da3e57effc47ca76f583380e8 Mon Sep 17 00:00:00 2001 From: Johannes Alneberg Date: Tue, 15 Feb 2022 12:29:38 +0100 Subject: [PATCH 2/3] Removed debug statement and moved recpients statement --- dds_web/api/user.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/dds_web/api/user.py b/dds_web/api/user.py index ac08d9f33..5bea5d7a6 100644 --- a/dds_web/api/user.py +++ b/dds_web/api/user.py @@ -220,10 +220,13 @@ def add_user_to_project(existing_user, project, role): @staticmethod @logging_bind_request def compose_and_send_email_to_user(userobj, mail_type, link=None, project=None): - flask.current_app.logger.info( - f"Sending email to user: {userobj} with {hasattr(userobj, 'emails')}" - ) - # Compose and send email + """Compose and send email""" + if hasattr(userobj, "emails"): + recipients = [x.email for x in userobj.emails] + else: + # userobj likely an invite + recipients = [userobj.email] + unit_name = None unit_email = None project_id = None @@ -242,10 +245,8 @@ def compose_and_send_email_to_user(userobj, mail_type, link=None, project=None): # Fill in email subject with sentence subject if mail_type == "invite": subject = f"{subject_subject} invites you to the SciLifeLab Data Delivery System" - recepients = [userobj.email] elif mail_type == "project_release": subject = f"Project made available by {subject_subject} in the SciLifeLab Data Delivery System" - recepients = [x.email for x in userobj.emails] project_id = project.public_id deadline = project.current_deadline.astimezone(datetime.timezone.utc).strftime( "%Y-%m-%d %H:%M:%S %Z" From 1256a4d32746622031438d75b2ad96a5df3f4dc4 Mon Sep 17 00:00:00 2001 From: Johannes Alneberg Date: Tue, 15 Feb 2022 12:33:16 +0100 Subject: [PATCH 3/3] Fix spelling --- dds_web/api/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dds_web/api/user.py b/dds_web/api/user.py index 5bea5d7a6..a3f1fd6f1 100644 --- a/dds_web/api/user.py +++ b/dds_web/api/user.py @@ -256,7 +256,7 @@ def compose_and_send_email_to_user(userobj, mail_type, link=None, project=None): msg = flask_mail.Message( subject, - recipients=recepients, + recipients=recipients, ) # Need to attach the image to be able to use it