Changeset - 2b2e37660e95
[Not reviewed]
default
0 5 0
Thomas De Schampheleire - 8 years ago 2018-02-13 11:09:14
thomas.de_schampheleire@nokia.com
HTML email templates: introduce link_style variable

Avoid repetition inside the templates and simplify future refactoring by
using 'style' variables provided by the model.
5 files changed with 11 insertions and 10 deletions:
0 comments (0 inline, 0 general)
kallithea/model/notification.py
Show inline comments
 
@@ -246,100 +246,101 @@ class NotificationModel(object):
 
                }[notification.type_] % dict(
 
                    user=notification.created_by_user.username,
 
                    age=h.age(notification.created_on),
 
                )
 
        else:
 
            return {
 
                    _n.TYPE_CHANGESET_COMMENT: _('%(user)s commented on changeset at %(when)s'),
 
                    _n.TYPE_MESSAGE: _('%(user)s sent message at %(when)s'),
 
                    _n.TYPE_MENTION: _('%(user)s mentioned you at %(when)s'),
 
                    _n.TYPE_REGISTRATION: _('%(user)s registered in Kallithea at %(when)s'),
 
                    _n.TYPE_PULL_REQUEST: _('%(user)s opened new pull request at %(when)s'),
 
                    _n.TYPE_PULL_REQUEST_COMMENT: _('%(user)s commented on pull request at %(when)s'),
 
                }[notification.type_] % dict(
 
                    user=notification.created_by_user.username,
 
                    when=h.fmt_date(notification.created_on),
 
                )
 

	
 

	
 
class EmailNotificationModel(object):
 

	
 
    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
 
    TYPE_PULL_REQUEST_COMMENT = Notification.TYPE_PULL_REQUEST_COMMENT
 
    TYPE_DEFAULT = 'default'
 

	
 
    def __init__(self):
 
        super(EmailNotificationModel, self).__init__()
 
        self._template_root = kallithea.CONFIG['paths']['templates'][0]
 
        self._tmpl_lookup = app_globals.mako_lookup
 
        self.email_types = {
 
            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] %(repo_name)s changeset %(short_id)s "%(message_short)s" on %(branch)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] %(repo_name)s PR %(pr_nice_id)s "%(pr_title_short)s" from %(pr_source_branch)s by %(pr_owner_username)s'),
 
            self.TYPE_PULL_REQUEST_COMMENT: _('[Comment] %(repo_name)s PR %(pr_nice_id)s "%(pr_title_short)s" from %(pr_source_branch)s by %(pr_owner_username)s'),
 
        }
 

	
 
    def get_email_description(self, type_, **kwargs):
 
        """
 
        return subject for email based on given type
 
        """
 
        tmpl = self._subj_map[type_]
 
        try:
 
            subj = tmpl % kwargs
 
        except KeyError as e:
 
            log.error('error generating email subject for %r from %s: %s', type_, ','.join(self._subj_map.keys()), e)
 
            raise
 
        l = [safe_unicode(x) for x in [kwargs.get('status_change'), kwargs.get('closing_pr') and _('Closing')] if x]
 
        if l:
 
            if subj.startswith('['):
 
                subj = '[' + ', '.join(l) + ': ' + subj[1:]
 
            else:
 
                subj = '[' + ', '.join(l) + '] ' + subj
 
        return subj
 

	
 
    def get_email_tmpl(self, type_, content_type, **kwargs):
 
        """
 
        return generated template for email based on given type
 
        """
 

	
 
        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 = {'_': _,
 
                   'h': h,
 
                   'c': c}
 
        _kwargs.update(kwargs)
 
        if content_type == 'html':
 
            _kwargs.update({
 
                "color_text": "#202020",
 
                "color_emph": "#395fa0",
 
                "color_link": "#395fa0",
 
                "color_border": "#ddd",
 
                "color_background_grey": "#f9f9f9",
 
                "color_button": "#395fa0",
 
                "monospace_style": "font-family:Lucida Console,Consolas,Monaco,Inconsolata,Liberation Mono,monospace",
 
                "sans_style": "font-family:Helvetica,Arial,sans-serif",
 
                })
 
            _kwargs.update({
 
                "default_style": "%(sans_style)s;font-weight:200;font-size:14px;line-height:17px;color:%(color_text)s" % _kwargs,
 
                "comment_style": "%(monospace_style)s;white-space:pre-wrap" % _kwargs,
 
                "emph_style": "font-weight:600;color:%(color_emph)s" % _kwargs,
 
                "link_style": "color:%(color_link)s;text-decoration:none" % _kwargs,
 
                })
 

	
 
        log.debug('rendering tmpl %s with kwargs %s', base, _kwargs)
 
        return email_template.render_unicode(**_kwargs)
kallithea/templates/email_templates/changeset_comment.html
Show inline comments
 
<%inherit file="main.html"/>\
 
\
 
<%block name="header">\
 
<% title = _('Mention in Comment on Changeset "%s"') % h.shorter(message, 200, firstline=True) if is_mention else _('Comment on Changeset "%s"') % h.shorter(message, 200, firstline=True) %>\
 
<%include file="header.html" args="title=title,link=cs_comment_url"/>\
 
</%block>\
 
\
 
<table cellpadding="0" cellspacing="0" border="0" width="100%">
 
    <tr>
 
        <td>
 
<%include file="comment.html" args="text=body,author=cs_comment_user,status=status_change"/>\
 
        </td>
 
    </tr>
 
    <tr>
 
        <td height="30px" style="height:30px"></td>
 
    </tr>
 
    <tr>
 
        <td>
 
            <div>
 
                ${_('Changeset on')}
 
                <a style="color:${color_link};text-decoration:none"
 
                <a style="${link_style}"
 
                   href="${cs_target_repo}">${cs_target_repo}</a>
 
                ${_('branch')}
 
                <span style="color:${color_link}">${branch}</span>:
 
            </div>
 
            <div>
 
                "<a style="color:${color_link};text-decoration:none"
 
                "<a style="${link_style}"
 
                   href="${cs_url}">${h.shorter(message, 60, firstline=True)}</a>"
 
                ${_('by')}
 
                <span style="color:${color_link}">${cs_author.full_name_and_username}</span>.
 
            </div>
 
        </td>
 
    </tr>
 
    <tr>
 
        <td>
 
<%include file="button.html" args="url=cs_comment_url,title=_('View Comment'),padding_bottom=False"/>\
 
        </td>
 
    </tr>
 
</table>
kallithea/templates/email_templates/password_reset.html
Show inline comments
 
<%inherit file="main.html"/>\
 
\
 
<%block name="header">\
 
<%include file="header.html" args="title=_('Password Reset Request'),link=None"/>\
 
</%block>\
 
\
 
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="table-layout:fixed;word-wrap:break-word;">
 
    <tr>
 
        <td>${_('Hello %s') % user},</td>
 
    </tr>
 
    <tr>
 
        <td height="10px" style="height:10px"></td>
 
    </tr>
 
    <tr>
 
        <td>
 
            ${_('We have received a request to reset the password for your account.')}
 
        </td>
 
    </tr>
 
    <tr>
 
        <td height="10px" style="height:10px"></td>
 
    </tr>
 
    <tr>
 
        <td>
 
            %if reset_token is None:
 
                <div>${_('This account is however managed outside this system and the password cannot be changed here.')}</div>
 
            %else:
 
                <div>
 
                    ${_('To set a new password, click the following link')}:
 
                    <br/>
 
                    <a style="color:${color_link};text-decoration:none" href="${reset_url}"
 
                    <a style="${link_style}" href="${reset_url}"
 
                        target="_blank">${reset_url}</a>
 
                    <br/>
 
                    ${_("Should you not be able to use the link above, please type the following code into the password reset form")}:
 
                    <code>${reset_token}</code>
 
                </div>
 
            %endif
 
        </td>
 
    </tr>
 
    <tr>
 
        <td height="10px" style="height:10px"></td>
 
    </tr>
 
    <tr>
 
        <td>
 
            ${_("If it weren't you who requested the password reset, just disregard this message.")}
 
        </td>
 
    </tr>
 
</table>
kallithea/templates/email_templates/pull_request.html
Show inline comments
 
<%inherit file="main.html"/>\
 
\
 
<%block name="header">\
 
<% title = _('Mention on Pull Request %s "%s" by %s') % (pr_nice_id, pr_title, pr_user_created) if is_mention else _('Added as Reviewer of Pull Request %s "%s" by %s') % (pr_nice_id, pr_title, pr_user_created) %>\
 
<%include file="header.html" args="title=title,link=pr_url"/>\
 
</%block>\
 
\
 
<table cellpadding="0" cellspacing="0" border="0" width="100%">
 
    <tr>
 
        <td>
 
            <div>
 
                ${_('Pull request from')}
 
                <a style="color:${color_link};text-decoration:none"
 
                <a style="${link_style}"
 
                   href="${pr_source_repo}">${pr_source_repo}</a>
 
                ${_('at')}
 
                <span style="color:${color_link}">${pr_source_branch}</span>
 
                ${_('to')}
 
                <a style="color:${color_link};text-decoration:none"
 
                <a style="${link_style}"
 
                   href="${pr_target_repo}">${pr_target_repo}</a>
 
                ${_('at')}
 
                <span style="color:${color_link}">${pr_target_branch}</span>:
 
            </div>
 
            <div>
 
                <a style="color:${color_link};text-decoration:none"
 
                <a style="${link_style}"
 
                   href="${pr_url}">${pr_nice_id}</a>
 
                "<span style="color:${color_link}">${pr_title}</span>"
 
                ${_('by')}
 
                <span style="color:${color_link}">${pr_owner.full_name_and_username}</span>.
 
            </div>
 
        </td>
 
    </tr>
 
    <tr><td height="10px" style="height:10px"></td></tr>
 
    <tr>
 
        <td>
 
            <div>
 
                ${_('Description')}:
 
            </div>
 
        </td>
 
    </tr>
 
    <tr><td height="10px" style="height:10px"></td></tr>
 
    <tr>
 
        <td>
 
            <div style="${comment_style};color:${color_link}">${body}</div>
 
        </td>
 
    </tr>
 
    <tr><td height="15px" style="height:15px"></td></tr>
 
    <tr>
 
        <td>
 
            <div>${_('Changesets')}:</div>
 
        </td>
 
    </tr>
 
    <tr><td height="10px" style="height:10px"></td></tr>
 

	
 
    <tr>
 
        <td style="${sans_style}">
 
            <ul style="color:${color_link};padding-left:15px;margin:0">
 
                %for revision, desc in pr_revisions:
 
                    <li style="mso-special-format:bullet">
 
                        <a style="color:${color_link};text-decoration:none"
 
                        <a style="${link_style}"
 
                           href="${h.canonical_url('changeset_home', repo_name=org_repo_name, revision=revision)}">
 
                            ${h.shorter(desc, 80, firstline=True)}
 
                        </a>
 
                    </li>
 
                %endfor
 
            </ul>
 
        </td>
 
    </tr>
 
    <tr>
 
        <td>
 
<%include file="button.html" args="url=pr_url,title=_('View Pull Request'),padding_bottom=False"/>\
 
        </td>
 
    </tr>
 
</table>
kallithea/templates/email_templates/pull_request_comment.html
Show inline comments
 
<%inherit file="main.html"/>\
 
\
 
<%block name="header">\
 
<% title = _('Mention in Comment on Pull Request %s "%s"') % (pr_nice_id, pr_title) if is_mention else _('Pull Request %s "%s" Closed') % (pr_nice_id, pr_title) if closing_pr else _('Comment on Pull Request %s "%s"') % (pr_nice_id, pr_title) %>\
 
<%include file="header.html" args="title=title,link=pr_comment_url"/>\
 
</%block>\
 
\
 
<table cellpadding="0" cellspacing="0" border="0" width="100%">
 
    <tr>
 
        <td>
 
<%include file="comment.html" args="text=body,author=pr_comment_user,status=status_change,close=closing_pr"/>\
 
        </td>
 
    </tr>
 
    <tr>
 
        <td height="30px" style="height:30px"></td>
 
    </tr>
 
    <tr>
 
        <td>
 
            <div>
 
                ${_('Pull request from')}
 
                <a style="color:${color_link};text-decoration:none"
 
                <a style="${link_style}"
 
                   href="${pr_source_repo}">${pr_source_repo}</a>
 
                ${_('branch')}
 
                <span style="color:${color_link}">${pr_source_branch}</span>
 
                ${_('to')}
 
                <a style="color:${color_link};text-decoration:none"
 
                <a style="${link_style}"
 
                   href="${pr_target_repo}">${pr_target_repo}</a>
 
                ${_('branch')}
 
                <span style="color:${color_link}">${pr_target_branch}</span>:
 
            </div>
 
            <div>
 
                <a style="color:${color_link};text-decoration:none"
 
                <a style="${link_style}"
 
                   href="${pr_url}">${pr_nice_id}</a>
 
                "<span style="color:${color_link}">${pr_title}</span>"
 
                ${_('by')}
 
                <span style="color:${color_link}">${pr_owner.full_name_and_username}</span>.
 
            </div>
 
        </td>
 
    </tr>
 
    <tr>
 
        <td>
 
<%include file="button.html" args="url=pr_comment_url,title=_('View Comment'),padding_bottom=False"/>\
 
        </td>
 
    </tr>
 
</table>
0 comments (0 inline, 0 general)