changeset 4381:55f2f2b4c462

notifications: improve Email subjects
author Mads Kiilerich <madski@unity3d.com>
date Fri, 18 Jul 2014 19:22:01 +0200
parents 5ebd887522ea
children 8eeb3fa7bc05
files kallithea/model/comment.py kallithea/model/notification.py kallithea/model/pull_request.py kallithea/model/user.py
diffstat 4 files changed, 57 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/model/comment.py	Sun Jan 12 17:30:00 2014 +0100
+++ b/kallithea/model/comment.py	Fri Jul 18 19:22:01 2014 +0200
@@ -93,8 +93,6 @@
                           {'desc': desc, 'line': line},
                           _url)
             )
-            email_subject = '%s commented on changeset %s' % \
-                (user.username, h.short_id(revision))
             # get the current participants of this changeset
             recipients = ChangesetComment.get_users(revision=revision)
             # add changeset author if it's in kallithea system
@@ -110,12 +108,17 @@
                                         qualified=True),
                 'cs_comment_url': _url,
                 'raw_id': revision,
-                'message': cs.message
+                'message': cs.message,
+                'repo_name': repo.repo_name,
+                'short_id': h.short_id(revision),
+                'branch': cs.branch,
+                'comment_username': user.username,
             }
         #pull request
         elif pull_request:
             notification_type = Notification.TYPE_PULL_REQUEST_COMMENT
             desc = comment.pull_request.title
+            _org_ref_type, org_ref_name, _org_rev = comment.pull_request.org_ref.split(':')
             _url = h.url('pullrequest_show',
                 repo_name=pull_request.other_repo.repo_name,
                 pull_request_id=pull_request.pull_request_id,
@@ -129,8 +132,6 @@
                            'line': line},
                           _url)
             )
-            email_subject = '%s commented on pull request #%s' % \
-                    (user.username, comment.pull_request.pull_request_id)
             # get the current participants of this pull request
             recipients = ChangesetComment.get_users(pull_request_id=
                                                 pull_request.pull_request_id)
@@ -150,10 +151,13 @@
                 'pr_comment_user': h.person(user),
                 'pr_target_repo': h.url('summary_home',
                                    repo_name=pull_request.other_repo.repo_name,
-                                   qualified=True)
+                                   qualified=True),
+                'repo_name': pull_request.other_repo.repo_name,
+                'ref': org_ref_name,
+                'comment_username': user.username,
             }
 
-        return subj, body, recipients, notification_type, email_kwargs, email_subject
+        return subj, body, recipients, notification_type, email_kwargs
 
     def create(self, text, repo, user, revision=None, pull_request=None,
                f_path=None, line_no=None, status_change=None, closing_pr=False,
@@ -200,7 +204,7 @@
 
         if send_email:
             (subj, body, recipients, notification_type,
-             email_kwargs, email_subject) = self._get_notification_data(
+             email_kwargs) = self._get_notification_data(
                                 repo, comment, user,
                                 comment_text=text,
                                 line_no=line_no,
@@ -212,7 +216,7 @@
             NotificationModel().create(
                 created_by=user, subject=subj, body=body,
                 recipients=recipients, type_=notification_type,
-                email_kwargs=email_kwargs, email_subject=email_subject
+                email_kwargs=email_kwargs,
             )
 
             mention_recipients = set(self._extract_mentions(body))\
--- a/kallithea/model/notification.py	Sun Jan 12 17:30:00 2014 +0100
+++ b/kallithea/model/notification.py	Fri Jul 18 19:22:01 2014 +0200
@@ -59,7 +59,7 @@
 
     def create(self, created_by, subject, body, recipients=None,
                type_=Notification.TYPE_MESSAGE, with_email=True,
-               email_kwargs={}, email_subject=None):
+               email_kwargs={}):
         """
 
         Creates notification of given type
@@ -73,7 +73,6 @@
         :param type_: type of notification
         :param with_email: send email with this notification
         :param email_kwargs: additional dict to pass as args to email template
-        :param email_subject: use given subject as email subject
         """
         from kallithea.lib.celerylib import tasks, run_task
 
@@ -117,14 +116,17 @@
 
         # send email with notification to all other participants
         for rec in rec_objs:
-            if not email_subject:
-                email_subject = NotificationModel()\
-                                    .make_description(notif, show_age=False)
-            type_ = type_
             email_body = None  # we set body to none, we just send HTML emails
             ## this is passed into template
-            kwargs = {'subject': subject, 'body': h.rst_w_mentions(body)}
+            kwargs = {'subject': subject,
+                      'body': h.rst_w_mentions(body),
+                      'when': h.fmt_date(notif.created_on),
+                      'user': notif.created_by_user.username,
+                      }
+
             kwargs.update(email_kwargs)
+            email_subject = EmailNotificationModel()\
+                                .get_email_description(type_, **kwargs)
             email_body_html = EmailNotificationModel()\
                                 .get_email_tmpl(type_, **kwargs)
 
@@ -253,6 +255,8 @@
 class EmailNotificationModel(BaseModel):
 
     TYPE_CHANGESET_COMMENT = Notification.TYPE_CHANGESET_COMMENT
+    TYPE_MESSAGE = Notification.TYPE_MESSAGE # only used for testing
+    # Notification.TYPE_MENTION is not used
     TYPE_PASSWORD_RESET = 'password_link'
     TYPE_REGISTRATION = Notification.TYPE_REGISTRATION
     TYPE_PULL_REQUEST = Notification.TYPE_PULL_REQUEST
@@ -271,13 +275,30 @@
             self.TYPE_PULL_REQUEST: 'email_templates/pull_request.html',
             self.TYPE_PULL_REQUEST_COMMENT: 'email_templates/pull_request_comment.html',
         }
+        self._subj_map = {
+            self.TYPE_CHANGESET_COMMENT: _('Comment on %(repo_name)s changeset %(short_id)s on %(branch)s by %(comment_username)s'),
+            self.TYPE_MESSAGE: 'Test Message',
+            # self.TYPE_PASSWORD_RESET
+            self.TYPE_REGISTRATION: _('New user %(new_username)s registered'),
+            # self.TYPE_DEFAULT
+            self.TYPE_PULL_REQUEST: _('Review request on %(repo_name)s pull request #%(pr_id)s from %(ref)s by %(pr_username)s'),
+            self.TYPE_PULL_REQUEST_COMMENT: _('Comment on %(repo_name)s pull request #%(pr_id)s from %(ref)s by %(comment_username)s'),
+        }
 
+    def get_email_description(self, type_, **kwargs):
+        """
+        return subject for email based on given type
+        """
+        tmpl = self._subj_map[type_]
+        try:
+            return tmpl % kwargs
+        except KeyError, e:
+            log.error('error generating email subject for %r from %s: %s', type_, ','.join(self._subj_map.keys()), e)
+            raise
 
     def get_email_tmpl(self, type_, **kwargs):
         """
         return generated template for email based on given type
-
-        :param type_:
         """
 
         base = self.email_types.get(type_, self.email_types[self.TYPE_DEFAULT])
--- a/kallithea/model/pull_request.py	Sun Jan 12 17:30:00 2014 +0100
+++ b/kallithea/model/pull_request.py	Fri Jul 18 19:22:01 2014 +0200
@@ -132,17 +132,23 @@
                 pr_url)
             )
         body = pr.description
-        kwargs = {
+        _org_ref_type, org_ref_name, _org_rev = pr.org_ref.split(':')
+        email_kwargs = {
             'pr_title': pr.title,
             'pr_user_created': h.person(pr.author),
             'pr_repo_url': h.url('summary_home', repo_name=pr.other_repo.repo_name,
                                  qualified=True,),
             'pr_url': pr_url,
-            'pr_revisions': revision_data}
-
+            'pr_revisions': revision_data,
+            'repo_name': pr.other_repo.repo_name,
+            'pr_id': pr.pull_request_id,
+            'ref': org_ref_name,
+            'pr_username': pr.author.username,
+            }
         NotificationModel().create(created_by=pr.author, subject=subject, body=body,
                                    recipients=reviewers,
-                                   type_=Notification.TYPE_PULL_REQUEST, email_kwargs=kwargs)
+                                   type_=Notification.TYPE_PULL_REQUEST,
+                                   email_kwargs=email_kwargs)
 
     def update_reviewers(self, pull_request, reviewers_ids):
         reviewers_ids = set(reviewers_ids)
--- a/kallithea/model/user.py	Sun Jan 12 17:30:00 2014 +0100
+++ b/kallithea/model/user.py	Fri Jul 18 19:22:01 2014 +0200
@@ -203,12 +203,11 @@
                     '- Email: %s\n')
             body = body % (new_user.username, new_user.full_name, new_user.email)
             edit_url = url('edit_user', id=new_user.user_id, qualified=True)
-            kw = {'registered_user_url': edit_url}
+            email_kwargs = {'registered_user_url': edit_url, 'new_username': new_user.username}
             NotificationModel().create(created_by=new_user, subject=subject,
                                        body=body, recipients=None,
                                        type_=Notification.TYPE_REGISTRATION,
-                                       email_kwargs=kw)
-
+                                       email_kwargs=email_kwargs)
         except Exception:
             log.error(traceback.format_exc())
             raise
@@ -297,8 +296,8 @@
                            qualified=True)
                 reg_type = EmailNotificationModel.TYPE_PASSWORD_RESET
                 body = EmailNotificationModel().get_email_tmpl(reg_type,
-                                                    **{'user': user.short_contact,
-                                                       'reset_url': link})
+                                                               user=user.short_contact,
+                                                               reset_url=link)
                 log.debug('sending email')
                 run_task(tasks.send_email, [user_email],
                          _("Password reset link"), body, body)