[PATCH] notification: don't repeat common actions for each email recipient

Thomas De Schampheleire patrickdepinguin at gmail.com
Tue Sep 18 20:13:35 UTC 2018


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
# Date 1537297052 -7200
#      Tue Sep 18 20:57:32 2018 +0200
# Node ID a1682c5421a34b1f4bbc74474be1f483052a5ec3
# Parent  d63018164a308f1005c250a284279fe6f15cf8a3
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 --git a/kallithea/model/notification.py b/kallithea/model/notification.py
--- a/kallithea/model/notification.py
+++ b/kallithea/model/notification.py
@@ -98,40 +98,40 @@ class NotificationModel(object):
         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)
 


More information about the kallithea-general mailing list