Skip to content

Commit

Permalink
[bug 846386] l10n the kb email templates
Browse files Browse the repository at this point in the history
* overhaul code to support setting the right locale for each user
  for each email sent
* convert kb email templates from Django to Jinja2
  • Loading branch information
willkg committed Mar 4, 2013
1 parent 5aa3696 commit 3457aa1
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 164 deletions.
161 changes: 91 additions & 70 deletions apps/wiki/events.py
Expand Up @@ -12,6 +12,7 @@
from tower import ugettext as _
from wikimarkup.parser import ALLOWED_TAGS, ALLOWED_ATTRIBUTES

from sumo import email_utils
from sumo.urlresolvers import reverse
from users.models import Profile
from wiki.models import Document
Expand Down Expand Up @@ -87,18 +88,28 @@ def context_dict(revision, ready_for_l10n=False, revision_approved=False):
def notification_mails(revision, subject, template, url, users_and_watches):
"""Return EmailMessages in the KB's standard notification mail format."""
document = revision.document
subject = subject.format(title=document.title, creator=revision.creator,
locale=document.locale)
t = loader.get_template(template)
c = context_dict(revision)
mail = EmailMessage(subject, '', settings.TIDINGS_FROM_ADDRESS)

for u, w in users_and_watches:
c['watch'] = w[0] # TODO: Expose all watches.
c['url'] = url
mail.to = [u.email]
mail.body = t.render(Context(c))
yield mail
if hasattr(u, 'profile'):
locale = u.profile.locale
else:
locale = document.locale

with email_utils.uselocale(locale):
c = context_dict(revision)
# TODO: Expose all watches
c['watch'] = w[0]
c['url'] = url

subject = subject.format(title=document.title,
creator=revision.creator,
locale=document.locale)
msg = email_utils.render_email(template, c)

yield EmailMessage(subject,
msg,
settings.TIDINGS_FROM_ADDRESS,
[u.email])


class EditDocumentEvent(InstanceEvent):
Expand All @@ -114,6 +125,9 @@ def _mails(self, users_and_watches):
document = self.revision.document
log.debug('Sending edited notification email for document (id=%s)' %
document.id)

# Note: This is a lazy_gettext, so it gets localized in the
# locale that's in effect when .format() is called on it.
subject = _(u'{title} was edited by {creator}')
url = reverse('wiki.document_revisions', locale=document.locale,
args=[document.slug])
Expand Down Expand Up @@ -146,8 +160,8 @@ class ReviewableRevisionInLocaleEvent(_RevisionConstructor,
_LocaleFilter,
Event):
"""Event fired when any revision in a certain locale is ready for review"""
# Our event_type suffices to limit our scope, so we don't bother setting
# content_type.
# Our event_type suffices to limit our scope, so we don't bother
# setting content_type.
event_type = 'reviewable wiki in locale'

def _mails(self, users_and_watches):
Expand Down Expand Up @@ -178,29 +192,28 @@ def _mails(self, users_and_watches):
document = revision.document
log.debug('Sending ready notifications for revision (id=%s)' %
revision.id)
ready_subject = _(
u'{title} has a revision ready for localization').format(
title=document.title,
creator=revision.creator,
locale=document.locale)

ready_template = loader.get_template(
'wiki/email/ready_for_l10n.ltxt')

c = context_dict(revision, ready_for_l10n=True)
for user, watches in users_and_watches:
c['watch'] = watches[0] # TODO: Expose all watches.

try:
profile = user.profile
except Profile.DoesNotExist:
locale = settings.WIKI_DEFAULT_LANGUAGE
if hasattr(user, 'profile'):
locale = user.profile.locale
else:
locale = profile.locale
c['url'] = django_reverse('wiki.select_locale',
args=[document.slug])
yield EmailMessage(ready_subject,
ready_template.render(Context(c)),
locale = document.locale

with email_utils.uselocale(locale):
subject = _(u'{title} has a revision ready for '
'localization').format(title=document.title)
template = 'wiki/email/ready_for_l10n.ltxt'

c = context_dict(revision, ready_for_l10n=True)
# TODO: Expose all watches
c['watch'] = watches[0]
c['url'] = django_reverse('wiki.select_locale',
args=[document.slug])

msg = email_utils.render_email(template, c)

yield EmailMessage(subject,
msg,
settings.TIDINGS_FROM_ADDRESS,
[user.email])

Expand Down Expand Up @@ -239,44 +252,52 @@ def _mails(self, users_and_watches):
is_ready = revision.is_ready_for_localization
log.debug('Sending approved/ready notifications for revision (id=%s)' %
revision.id)
ready_subject, approved_subject = [s.format(
title=document.title,
reviewer=revision.reviewer.username,
locale=document.locale) for s in
[_(u'{title} has a revision ready for localization'),
_(u'{title} ({locale}) has a new approved revision '
'({reviewer})')]]
ready_template = loader.get_template('wiki/email/ready_for_l10n.ltxt')
approved_template = loader.get_template('wiki/email/approved.ltxt')
approved_url = reverse('wiki.document',
locale=document.locale,
args=[document.slug])

for user, watches in users_and_watches:
if (is_ready and
ReadyRevisionEvent.event_type in
# Figure out the locale to use for l10n.
if hasattr(user, 'profile'):
locale = user.profile.locale
else:
locale = document.locale

# Localize the subject and message with the appropriate
# context.
with email_utils.uselocale(locale):
if (is_ready and
ReadyRevisionEvent.event_type in
(w.event_type for w in watches)):
c = context_dict(revision, ready_for_l10n=True)
c['watch'] = watches[0] # TODO: Expose all watches.
# We should send a "ready" mail.
try:
profile = user.profile
except Profile.DoesNotExist:
locale = settings.WIKI_DEFAULT_LANGUAGE
c = context_dict(revision, ready_for_l10n=True)
# TODO: Expose all watches
c['watch'] = watches[0]
c['url'] = django_reverse('wiki.select_locale',
args=[document.slug])

subject = _(u'{title} has a revision ready for '
'localization')
template = 'wiki/email/ready_for_l10n.ltxt'

else:
locale = profile.locale
c['url'] = django_reverse('wiki.select_locale',
args=[document.slug])
yield EmailMessage(ready_subject,
ready_template.render(Context(c)),
settings.TIDINGS_FROM_ADDRESS,
[user.email])
else:
c = context_dict(revision, revision_approved=True)
c['url'] = approved_url
c['watch'] = watches[0] # TODO: Expose all watches.
c['reviewer'] = revision.reviewer.username
# Send an "approved" mail:
yield EmailMessage(approved_subject,
approved_template.render(Context(c)),
settings.TIDINGS_FROM_ADDRESS,
[user.email])
c = context_dict(revision, revision_approved=True)
approved_url = reverse('wiki.document',
locale=document.locale,
args=[document.slug])

c['url'] = approved_url
# TODO: Expose all watches.
c['watch'] = watches[0]
c['reviewer'] = revision.reviewer.username

subject = _(u'{title} ({locale}) has a new approved '
'revision ({reviewer})')
template = 'wiki/email/approved.ltxt'

subject = subject.format(
title=document.title,
reviewer=revision.reviewer.username,
locale=document.locale)
msg = email_utils.render_email(template, c)

yield EmailMessage(subject,
msg,
settings.TIDINGS_FROM_ADDRESS,
[user.email])
114 changes: 76 additions & 38 deletions apps/wiki/tasks.py
Expand Up @@ -5,9 +5,8 @@
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.core.mail import send_mail, mail_admins, send_mass_mail
from django.core.mail import EmailMessage, mail_admins
from django.db import transaction
from django.template import Context, loader

import celery.conf
from celery.task import task
Expand All @@ -16,6 +15,7 @@
from tower import ugettext as _
import waffle

from sumo import email_utils
from sumo.urlresolvers import reverse
from sumo.utils import chunked
from wiki.models import (Document, points_to_document_view, SlugCollision,
Expand All @@ -29,55 +29,93 @@
def send_reviewed_notification(revision, document, message):
"""Send notification of review to the revision creator."""
if revision.reviewer == revision.creator:
log.debug('Revision (id=%s) reviewed by creator, skipping email' % \
log.debug('Revision (id=%s) reviewed by creator, skipping email' %
revision.id)
return

log.debug('Sending reviewed email for revision (id=%s)' % revision.id)
if revision.is_approved:
subject = _(u'Your revision has been approved: {title}')
else:
subject = _(u'Your revision has been reviewed: {title}')
subject = subject.format(title=document.title)
t = loader.get_template('wiki/email/reviewed.ltxt')

url = reverse('wiki.document_revisions', locale=document.locale,
args=[document.slug])
content = t.render(Context({'document_title': document.title,
'approved': revision.is_approved,
'reviewer': revision.reviewer,
'message': message,
'url': url,
'host': Site.objects.get_current().domain}))
send_mass_mail(
((subject, content, settings.TIDINGS_FROM_ADDRESS, [who.email])
for who in [revision.creator, revision.reviewer]))

c = {'document_title': document.title,
'approved': revision.is_approved,
'reviewer': revision.reviewer,
'message': message,
'url': url,
'host': Site.objects.get_current().domain}

msgs = []

for user in [revision.creator, revision.reviewer]:
if hasattr(user, 'profile'):
locale = user.profile.locale
else:
locale = settings.WIKI_DEFAULT_LANGUAGE

with email_utils.uselocale(locale):
if revision.is_approved:
subject = _(u'Your revision has been approved: {title}')
else:
subject = _(u'Your revision has been reviewed: {title}')
subject = subject.format(title=document.title)

template = 'wiki/email/reviewed.ltxt'
msg = email_utils.render_email(template, c)

msgs.append(EmailMessage(subject,
msg,
settings.TIDINGS_FROM_ADDRESS,
[user.email]))

email_utils.send_messages(msgs)


@task
def send_contributor_notification(based_on, revision, document, message):
"""Send notification of review to the contributors of revisions."""
if revision.is_approved:
subject = _(u'A revision you contributed to has '
'been approved: {title}')
else:
subject = _(u'A revision you contributed to has '
'been reviewed: {title}')
subject = subject.format(title=document.title)
t = loader.get_template('wiki/email/reviewed_contributors.ltxt')

template = 'wiki/email/reviewed_contributors.ltxt'
url = reverse('wiki.document_revisions', locale=document.locale,
args=[document.slug])
content = t.render(Context({'document_title': document.title,
'approved': revision.is_approved,
'reviewer': revision.reviewer,
'message': message,
'url': url,
'host': Site.objects.get_current().domain}))

# Send email to all contributors except the reviewer and the creator
# of the approved revision.
send_mail(subject, content, settings.TIDINGS_FROM_ADDRESS,
[r.creator.email for r in based_on
if r.creator not in [revision.creator, revision.reviewer]])
c = {'document_title': document.title,
'approved': revision.is_approved,
'reviewer': revision.reviewer,
'message': message,
'url': url,
'host': Site.objects.get_current().domain}

msgs = []
for r in based_on:
# Send email to all contributors except the reviewer and the creator
# of the approved revision.
if r.creator in [revision.creator, revision.reviewer]:
continue

user = r.creator

if hasattr(user, 'profile'):
locale = user.profile.locale
else:
locale = settings.WIKI_DEFAULT_LANGUAGE

with email_utils.uselocale(locale):
if revision.is_approved:
subject = _(u'A revision you contributed to has '
'been approved: {title}')
else:
subject = _(u'A revision you contributed to has '
'been reviewed: {title}')
subject = subject.format(title=document.title)

msg = email_utils.render_email(template, c)

msgs.append(EmailMessage(subject,
msg,
settings.TIDINGS_FROM_ADDRESS,
[user.email]))

email_utils.send_messages(msgs)


def schedule_rebuild_kb():
Expand Down
26 changes: 20 additions & 6 deletions apps/wiki/templates/wiki/email/approved.ltxt
@@ -1,14 +1,28 @@
{% autoescape off %}{% load i18n %}{% load unsubscribe_instructions %}{# L10n: This is an email. Whitespace matters! #}{% blocktrans %}{{ reviewer }} has approved the revision to the document {{ document_title }}.
{# This is an email. Whitespace matters! #}
{% from "includes/unsubscribe_text.ltxt" import unsubscribe_text with context %}
{% autoescape false %}
{% trans reviewer=reviewer, document_title=document_title %}
{{ reviewer }} has approved the revision to the document
{{ document_title }}.
{% endtrans %}


{% trans %}
To view the updated document, click the following link, or paste it
into your browser's location bar:
{% endtrans %}


To view the updated document, click the following link, or paste it into your browser's location bar:
{% endblocktrans %}
https://{{ host }}{{ url }}

--
{% trans "Summary:" %}
{# L10n: This is in an email. #}
{{ _('Summary:') }}
{{ summary|safe }}

--
{% blocktrans %}Changes:{% endblocktrans %}
{# L10n: This is in an email. #}
{{ _('Changes:') }}
{{ diff|safe }}
{% unsubscribe_instructions watch %}{% endautoescape %}

{{ unsubscribe_text(watch) }}{% endautoescape %}

0 comments on commit 3457aa1

Please sign in to comment.