changeset 4561:3397e3457f9c

email templates: send text/plain part as well This change adds text parts to the email templates, as HTML and text templates may be way to different to be handled automatically. Also, use proper dash-dash-space signature separator, so the email clients recognise it for sure.
author Andrew Shadura <andrew@shadura.me>
date Sun, 26 Oct 2014 09:53:22 +0100
parents b1679034b6c4
children c384703b3ae3
files kallithea/controllers/admin/settings.py kallithea/model/notification.py kallithea/model/user.py kallithea/templates/email_templates/changeset_comment.txt kallithea/templates/email_templates/default.txt kallithea/templates/email_templates/main.html kallithea/templates/email_templates/main.txt kallithea/templates/email_templates/password_reset.txt kallithea/templates/email_templates/pull_request.txt kallithea/templates/email_templates/pull_request_comment.txt kallithea/templates/email_templates/registration.txt
diffstat 11 files changed, 108 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/admin/settings.py	Fri Oct 03 00:20:36 2014 +0200
+++ b/kallithea/controllers/admin/settings.py	Sun Oct 26 09:53:22 2014 +0100
@@ -350,20 +350,23 @@
         if request.POST:
             test_email = request.POST.get('test_email')
             test_email_subj = 'Kallithea test email'
-            test_email_body = ('Kallithea Email test, '
+            test_body = ('Kallithea Email test, '
                                'Kallithea version: %s' % c.kallithea_version)
             if not test_email:
                 h.flash(_('Please enter email address'), category='error')
                 return redirect(url('admin_settings_email'))
 
+            test_email_txt_body = EmailNotificationModel()\
+                .get_email_tmpl(EmailNotificationModel.TYPE_DEFAULT,
+                                'txt', body=test_body)
             test_email_html_body = EmailNotificationModel()\
                 .get_email_tmpl(EmailNotificationModel.TYPE_DEFAULT,
-                                body=test_email_body)
+                                'html', body=test_body)
 
             recipients = [test_email] if test_email else None
 
             run_task(tasks.send_email, recipients, test_email_subj,
-                     test_email_body, test_email_html_body)
+                     test_email_txt_body, test_email_html_body)
 
             h.flash(_('Send email task created'), category='success')
             return redirect(url('admin_settings_email'))
--- a/kallithea/model/notification.py	Fri Oct 03 00:20:36 2014 +0200
+++ b/kallithea/model/notification.py	Sun Oct 26 09:53:22 2014 +0100
@@ -118,7 +118,6 @@
 
         # send email with notification to all other participants
         for rec in rec_objs:
-            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),
@@ -129,11 +128,13 @@
             kwargs.update(email_kwargs)
             email_subject = EmailNotificationModel()\
                                 .get_email_description(type_, **kwargs)
-            email_body_html = EmailNotificationModel()\
-                                .get_email_tmpl(type_, **kwargs)
+            email_txt_body = EmailNotificationModel()\
+                                .get_email_tmpl(type_, 'txt', **kwargs)
+            email_html_body = EmailNotificationModel()\
+                                .get_email_tmpl(type_, 'html', **kwargs)
 
-            run_task(tasks.send_email, [rec.email], email_subject, email_body,
-                     email_body_html, headers)
+            run_task(tasks.send_email, [rec.email], email_subject, email_txt_body,
+                     email_html_body, headers)
 
         return notif
 
@@ -270,12 +271,12 @@
         self._template_root = kallithea.CONFIG['pylons.paths']['templates'][0]
         self._tmpl_lookup = kallithea.CONFIG['pylons.app_globals'].mako_lookup
         self.email_types = {
-            self.TYPE_CHANGESET_COMMENT: 'email_templates/changeset_comment.html',
-            self.TYPE_PASSWORD_RESET: 'email_templates/password_reset.html',
-            self.TYPE_REGISTRATION: 'email_templates/registration.html',
-            self.TYPE_DEFAULT: 'email_templates/default.html',
-            self.TYPE_PULL_REQUEST: 'email_templates/pull_request.html',
-            self.TYPE_PULL_REQUEST_COMMENT: 'email_templates/pull_request_comment.html',
+            self.TYPE_CHANGESET_COMMENT: 'changeset_comment',
+            self.TYPE_PASSWORD_RESET: 'password_reset',
+            self.TYPE_REGISTRATION: 'registration',
+            self.TYPE_DEFAULT: 'default',
+            self.TYPE_PULL_REQUEST: 'pull_request',
+            self.TYPE_PULL_REQUEST_COMMENT: 'pull_request_comment',
         }
         self._subj_map = {
             self.TYPE_CHANGESET_COMMENT: _('Comment on %(repo_name)s changeset %(short_id)s on %(branch)s by %(comment_username)s'),
@@ -302,12 +303,12 @@
             subj += ' (%s)' % (', '.join(l))
         return subj
 
-    def get_email_tmpl(self, type_, **kwargs):
+    def get_email_tmpl(self, type_, content_type, **kwargs):
         """
         return generated template for email based on given type
         """
 
-        base = self.email_types.get(type_, self.email_types[self.TYPE_DEFAULT])
+        base = 'email_templates/' + self.email_types.get(type_, self.email_types[self.TYPE_DEFAULT]) + '.' + content_type
         email_template = self._tmpl_lookup.get_template(base)
         # translator and helpers inject
         _kwargs = {'_': _,
--- a/kallithea/model/user.py	Fri Oct 03 00:20:36 2014 +0200
+++ b/kallithea/model/user.py	Sun Oct 26 09:53:22 2014 +0100
@@ -297,11 +297,16 @@
                 link = h.canonical_url('reset_password_confirmation', key=user.api_key)
                 reg_type = EmailNotificationModel.TYPE_PASSWORD_RESET
                 body = EmailNotificationModel().get_email_tmpl(reg_type,
+                                                               'txt',
+                                                               user=user.short_contact,
+                                                               reset_url=link)
+                html_body = EmailNotificationModel().get_email_tmpl(reg_type,
+                                                               'html',
                                                                user=user.short_contact,
                                                                reset_url=link)
                 log.debug('sending email')
                 run_task(tasks.send_email, [user_email],
-                         _("Password reset link"), body, body)
+                         _("Password reset link"), body, html_body)
                 log.info('send new password mail to %s' % user_email)
             else:
                 log.debug("password reset email %s not found" % user_email)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/templates/email_templates/changeset_comment.txt	Sun Oct 26 09:53:22 2014 +0100
@@ -0,0 +1,19 @@
+## -*- coding: utf-8 -*-
+<%inherit file="main.txt"/>
+
+%if is_mention:
+${_('Comment from %s on %s changeset %s mentioned you') % (cs_comment_user, cs_target_repo, h.short_id(raw_id))}:
+%else:
+${_('Comment from %s on %s changeset %s') % (cs_comment_user, cs_target_repo, h.short_id(raw_id))}:
+%endif
+${body}
+
+%if status_change:
+${_('The changeset status was changed to')}: ${status_change}
+%endif
+
+${_('URL')}: ${cs_comment_url}
+
+${_('Changeset')}: ${h.short_id(raw_id)}
+${_('Description')}:
+${h.shorter(message, 256)}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/templates/email_templates/default.txt	Sun Oct 26 09:53:22 2014 +0100
@@ -0,0 +1,4 @@
+## -*- coding: utf-8 -*-
+<%inherit file="main.txt"/>
+
+${body}
--- a/kallithea/templates/email_templates/main.html	Fri Oct 03 00:20:36 2014 +0200
+++ b/kallithea/templates/email_templates/main.html	Sun Oct 26 09:53:22 2014 +0100
@@ -1,4 +1,6 @@
 ${self.body()}
+
+<br/>
 <br/>
 -- <br/>
-${_("This is an automatic notification - don't reply to this mail.")}
+${_("This is an automatic notification. Don't reply to this mail.")}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/templates/email_templates/main.txt	Sun Oct 26 09:53:22 2014 +0100
@@ -0,0 +1,4 @@
+${self.body()}
+
+-- 
+${_("This is an automatic notification. Don't reply to this mail.")}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/templates/email_templates/password_reset.txt	Sun Oct 26 09:53:22 2014 +0100
@@ -0,0 +1,11 @@
+## -*- coding: utf-8 -*-
+<%inherit file="main.txt"/>
+
+${_('Hello %s') % user}
+
+${_('We received a request to create a new password for your account.')}
+${_('You can generate it by clicking following URL')}:
+
+${reset_url}
+
+${_("Please ignore this email if you did not request a new password .")}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/templates/email_templates/pull_request.txt	Sun Oct 26 09:53:22 2014 +0100
@@ -0,0 +1,20 @@
+## -*- coding: utf-8 -*-
+<%inherit file="main.txt"/>
+
+%if is_mention:
+${_('%s mentioned you on %s pull request "%s"') % (pr_user_created, repo_name, pr_title)}
+%else:
+${_('%s requested your review of %s pull request "%s"') % (pr_user_created, repo_name, pr_title)}
+%endif
+
+${_('URL')}: ${pr_url}
+
+${_('Description')}:
+${body}
+
+${_('Changesets')}:
+%for r,r_msg in pr_revisions:
+${h.short_id(r)}:
+${h.shorter(r_msg, 256)}
+
+%endfor
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/templates/email_templates/pull_request_comment.txt	Sun Oct 26 09:53:22 2014 +0100
@@ -0,0 +1,15 @@
+## -*- coding: utf-8 -*-
+<%inherit file="main.txt"/>
+
+${_('Comment from %s on %s pull request "%s"') % (pr_comment_user, repo_name, pr_title)}:
+${body}
+
+%if status_change:
+    %if closing_pr:
+${_('The comment closed the pull request with status')}: ${status_change}
+    %else:
+${_('The comment was made with status')}: ${status_change}
+    %endif
+%endif
+
+${_('URL')}: ${pr_comment_url}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/templates/email_templates/registration.txt	Sun Oct 26 09:53:22 2014 +0100
@@ -0,0 +1,6 @@
+## -*- coding: utf-8 -*-
+<%inherit file="main.txt"/>
+
+${body}
+
+${_('View this user here')}: ${registered_user_url}