# HG changeset patch # User Thomas De Schampheleire # Date 1537297052 -7200 # Node ID 6bd262eaa058e189760fda42bf282f01b9ef0a11 # Parent 3d39e68ff5bce9eeaaa9650f449613235cc17fb6 notification: don't repeat common actions for each email recipient The emails sent upon PR creation or comments are identical for all recipients, except for the 'To:' header added by the send_email method. Calculation of subject, body, etc. can thus be moved outside of the loop. Move the manipulation of the recipient list down to where it is used, for clarity. diff -r 3d39e68ff5bc -r 6bd262eaa058 kallithea/model/notification.py --- a/kallithea/model/notification.py Mon Sep 17 22:34:09 2018 +0200 +++ b/kallithea/model/notification.py Tue Sep 18 20:57:32 2018 +0200 @@ -98,40 +98,40 @@ if not with_email: return notif - # don't send email to person who created this comment - rec_objs = set(recipients_objs).difference(set([created_by_obj])) - headers = {} headers['X-Kallithea-Notification-Type'] = type_ if 'threading' in email_kwargs: headers['References'] = ' '.join('<%s>' % x for x in email_kwargs['threading']) + # this is passed into template + html_kwargs = { + 'subject': subject, + 'body': h.render_w_mentions(body, repo_name), + 'when': h.fmt_date(notif.created_on), + 'user': notif.created_by_user.username, + } + + txt_kwargs = { + 'subject': subject, + 'body': body, + 'when': h.fmt_date(notif.created_on), + 'user': notif.created_by_user.username, + } + + html_kwargs.update(email_kwargs) + txt_kwargs.update(email_kwargs) + email_subject = EmailNotificationModel() \ + .get_email_description(type_, **txt_kwargs) + email_txt_body = EmailNotificationModel() \ + .get_email_tmpl(type_, 'txt', **txt_kwargs) + email_html_body = EmailNotificationModel() \ + .get_email_tmpl(type_, 'html', **html_kwargs) + + # don't send email to person who created this comment + rec_objs = set(recipients_objs).difference(set([created_by_obj])) + # send email with notification to all other participants for rec in rec_objs: - # this is passed into template - html_kwargs = { - 'subject': subject, - 'body': h.render_w_mentions(body, repo_name), - 'when': h.fmt_date(notif.created_on), - 'user': notif.created_by_user.username, - } - - txt_kwargs = { - 'subject': subject, - 'body': body, - 'when': h.fmt_date(notif.created_on), - 'user': notif.created_by_user.username, - } - - html_kwargs.update(email_kwargs) - txt_kwargs.update(email_kwargs) - email_subject = EmailNotificationModel() \ - .get_email_description(type_, **txt_kwargs) - email_txt_body = EmailNotificationModel() \ - .get_email_tmpl(type_, 'txt', **txt_kwargs) - email_html_body = EmailNotificationModel() \ - .get_email_tmpl(type_, 'html', **html_kwargs) - tasks.send_email([rec.email], email_subject, email_txt_body, email_html_body, headers, author=created_by_obj)