From 37a53757eb631f878820bb099c5f63cd5a920d6d Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Thu, 27 Aug 2020 10:43:30 +0200 Subject: [PATCH] add send_email_at_most_times --- app/email_utils.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/email_utils.py b/app/email_utils.py index 64a44d56..ba2b7acc 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -273,6 +273,41 @@ def send_email_with_rate_control( return True +def send_email_at_most_times( + user: User, + alert_type: str, + to_email: str, + subject, + plaintext, + html=None, + max_times=1, +) -> bool: + """Same as send_email with rate control over alert_type. + Sent at most `max_times` + This is used to inform users about a warning. + + Return true if the email is sent, otherwise False + """ + to_email = to_email.lower().strip() + nb_alert = SentAlert.query.filter_by( + alert_type=alert_type, to_email=to_email + ).count() + + if nb_alert >= max_times: + LOG.warning( + "%s emails were sent to %s alert type %s", + nb_alert, + to_email, + alert_type, + ) + return False + + SentAlert.create(user_id=user.id, alert_type=alert_type, to_email=to_email) + db.session.commit() + send_email(to_email, subject, plaintext, html) + return True + + def get_email_local_part(address): """ Get the local part from email