Commit f7a57c82 authored by Lukas Fleischer's avatar Lukas Fleischer
Browse files

Localize notification emails



Add support for translating notification emails and send localized
notifications, based on the user's language preferences. Also, update
the translations Makefile to add strings from the notification script
to the message catalog.

Implements FS#31850.
Signed-off-by: Lukas Fleischer's avatarLukas Fleischer <lfleischer@archlinux.org>
parent f3b4c5c6
import gettext
class Translator:
def __init__(self):
self._translator = {}
def translate(self, s, lang):
if lang == 'en':
return s
if lang not in self._translator:
self._translator[lang] = gettext.translation("aur",
"../../web/locale",
languages=[lang])
self._translator[lang].install()
return _(s)
...@@ -7,6 +7,7 @@ import textwrap ...@@ -7,6 +7,7 @@ import textwrap
import aurweb.config import aurweb.config
import aurweb.db import aurweb.db
import aurweb.l10n
aur_location = aurweb.config.get('options', 'aur_location') aur_location = aurweb.config.get('options', 'aur_location')
...@@ -40,36 +41,37 @@ def pkgbase_from_pkgreq(conn, reqid): ...@@ -40,36 +41,37 @@ def pkgbase_from_pkgreq(conn, reqid):
return cur.fetchone()[0] return cur.fetchone()[0]
def get_user_email(conn, uid):
cur = conn.execute('SELECT Email FROM Users WHERE ID = ?', [uid])
return cur.fetchone()[0]
class Notification: class Notification:
def __init__(self):
self._l10n = aurweb.l10n.Translator()
def get_refs(self): def get_refs(self):
return () return ()
def get_headers(self): def get_headers(self):
return {} return {}
def send(self): def get_body_fmt(self, lang):
body = '' body = ''
for line in self.get_body().splitlines(): for line in self.get_body(lang).splitlines():
body += textwrap.fill(line, break_long_words=False) + '\n' body += textwrap.fill(line, break_long_words=False) + '\n'
for i, ref in enumerate(self.get_refs()): for i, ref in enumerate(self.get_refs()):
body += '\n' + '[%d] %s' % (i + 1, ref) body += '\n' + '[%d] %s' % (i + 1, ref)
body = body.rstrip() return body.rstrip()
def send(self):
sendmail = aurweb.config.get('notifications', 'sendmail') sendmail = aurweb.config.get('notifications', 'sendmail')
sender = aurweb.config.get('notifications', 'sender') sender = aurweb.config.get('notifications', 'sender')
reply_to = aurweb.config.get('notifications', 'reply-to') reply_to = aurweb.config.get('notifications', 'reply-to')
for recipient in self.get_recipients(): for recipient in self.get_recipients():
msg = email.mime.text.MIMEText(body, 'plain', 'utf-8') to, lang = recipient
msg['Subject'] = self.get_subject() msg = email.mime.text.MIMEText(self.get_body_fmt(lang),
'plain', 'utf-8')
msg['Subject'] = self.get_subject(lang)
msg['From'] = sender msg['From'] = sender
msg['Reply-to'] = reply_to msg['Reply-to'] = reply_to
msg['To'] = recipient msg['To'] = to
for key, value in self.get_headers().items(): for key, value in self.get_headers().items():
msg[key] = value msg[key] = value
...@@ -81,66 +83,76 @@ class Notification: ...@@ -81,66 +83,76 @@ class Notification:
class ResetKeyNotification(Notification): class ResetKeyNotification(Notification):
def __init__(self, conn, uid): def __init__(self, conn, uid):
cur = conn.execute('SELECT UserName, Email, ResetKey FROM Users ' + cur = conn.execute('SELECT UserName, Email, LangPreference, ' +
'WHERE ID = ?', [uid]) 'ResetKey FROM Users WHERE ID = ?', [uid])
self._username, self._to, self._resetkey = cur.fetchone() self._username, self._to, self._lang, self._resetkey = cur.fetchone()
super().__init__()
def get_recipients(self): def get_recipients(self):
return [self._to] return [(self._to, self._lang)]
def get_subject(self): def get_subject(self, lang):
return 'AUR Password Reset' return self._l10n.translate('AUR Password Reset', lang)
def get_body(self): def get_body(self, lang):
return 'A password reset request was submitted for the account %s ' \ return self._l10n.translate(
'associated with your email address. If you wish to reset ' \ 'A password reset request was submitted for the account %s '
'your password follow the link [1] below, otherwise ignore ' \ 'associated with your email address. If you wish to reset '
'this message and nothing will happen.' % (self._username) 'your password follow the link [1] below, otherwise ignore '
'this message and nothing will happen.', lang) % \
(self._username)
def get_refs(self): def get_refs(self):
return (aur_location + '/passreset/?resetkey=' + self._resetkey,) return (aur_location + '/passreset/?resetkey=' + self._resetkey,)
class WelcomeNotification(ResetKeyNotification): class WelcomeNotification(ResetKeyNotification):
def get_subject(self): def get_subject(self, lang):
return 'Welcome to the Arch User Repository' return self._l10n.translate('Welcome to the Arch User Repository',
lang)
def get_body(self): def get_body(self, lang):
return 'Welcome to the Arch User Repository! In order to set an ' \ return self._l10n.translate(
'initial password for your new account, please click the ' \ 'Welcome to the Arch User Repository! In order to set an '
'link [1] below. If the link does not work, try copying and ' \ 'initial password for your new account, please click the '
'pasting it into your browser.' 'link [1] below. If the link does not work, try copying and '
'pasting it into your browser.', lang)
class CommentNotification(Notification): class CommentNotification(Notification):
def __init__(self, conn, uid, pkgbase_id, comment_id): def __init__(self, conn, uid, pkgbase_id, comment_id):
self._user = username_from_id(conn, uid) self._user = username_from_id(conn, uid)
self._pkgbase = pkgbase_from_id(conn, pkgbase_id) self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' + cur = conn.execute('SELECT DISTINCT Users.Email, Users.LangPreference '
'INNER JOIN PackageNotifications ' + 'FROM Users INNER JOIN PackageNotifications ' +
'ON PackageNotifications.UserID = Users.ID WHERE ' + 'ON PackageNotifications.UserID = Users.ID WHERE ' +
'Users.CommentNotify = 1 AND ' + 'Users.CommentNotify = 1 AND ' +
'PackageNotifications.UserID != ? AND ' + 'PackageNotifications.UserID != ? AND ' +
'PackageNotifications.PackageBaseID = ?', 'PackageNotifications.PackageBaseID = ?',
[uid, pkgbase_id]) [uid, pkgbase_id])
self._to = [row[0] for row in cur.fetchall()] self._recipients = cur.fetchall()
cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?', cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
[comment_id]) [comment_id])
self._text = cur.fetchone()[0] self._text = cur.fetchone()[0]
super().__init__()
def get_recipients(self): def get_recipients(self):
return self._to return self._recipients
def get_subject(self): def get_subject(self, lang):
return 'AUR Comment for %s' % (self._pkgbase) return self._l10n.translate('AUR Comment for %s', lang) % \
(self._pkgbase)
def get_body(self): def get_body(self, lang):
body = '%s [1] added the following comment to %s [2]:' % \ body = self._l10n.translate(
(self._user, self._pkgbase) '%s [1] added the following comment to %s [2]:', lang) % \
(self._user, self._pkgbase)
body += '\n\n' + self._text + '\n\n' body += '\n\n' + self._text + '\n\n'
body += 'If you no longer wish to receive notifications about this ' \ dnlabel = self._l10n.translate('Disable notifications', lang)
'package, please go to the package page [2] and select ' \ body += self._l10n.translate(
'"%s".' % ('Disable notifications') 'If you no longer wish to receive notifications about this '
'package, please go to the package page [2] and select '
'"%s".', lang) % dnlabel
return body return body
def get_refs(self): def get_refs(self):
...@@ -157,28 +169,33 @@ class UpdateNotification(Notification): ...@@ -157,28 +169,33 @@ class UpdateNotification(Notification):
def __init__(self, conn, uid, pkgbase_id): def __init__(self, conn, uid, pkgbase_id):
self._user = username_from_id(conn, uid) self._user = username_from_id(conn, uid)
self._pkgbase = pkgbase_from_id(conn, pkgbase_id) self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' + cur = conn.execute('SELECT DISTINCT Users.Email, ' +
'Users.LangPreference FROM Users ' +
'INNER JOIN PackageNotifications ' + 'INNER JOIN PackageNotifications ' +
'ON PackageNotifications.UserID = Users.ID WHERE ' + 'ON PackageNotifications.UserID = Users.ID WHERE ' +
'Users.UpdateNotify = 1 AND ' + 'Users.UpdateNotify = 1 AND ' +
'PackageNotifications.UserID != ? AND ' + 'PackageNotifications.UserID != ? AND ' +
'PackageNotifications.PackageBaseID = ?', 'PackageNotifications.PackageBaseID = ?',
[uid, pkgbase_id]) [uid, pkgbase_id])
self._to = [row[0] for row in cur.fetchall()] self._recipients = cur.fetchall()
super().__init__()
def get_recipients(self): def get_recipients(self):
return self._to return self._recipients
def get_subject(self): def get_subject(self, lang):
return 'AUR Package Update: %s' % (self._pkgbase) return self._l10n.translate('AUR Package Update: %s', lang) % \
(self._pkgbase)
def get_body(self): def get_body(self, lang):
body = '%s [1] pushed a new commit to %s [2].' % \ body = self._l10n.translate('%s [1] pushed a new commit to %s [2].',
(self._user, self._pkgbase) lang) % (self._user, self._pkgbase)
body += '\n\n' body += '\n\n'
body += 'If you no longer wish to receive notifications about this ' \ dnlabel = self._l10n.translate('Disable notifications', lang)
'package, please go to the package page [2] and select ' \ body += self._l10n.translate(
'"%s".' % ('Disable notifications') 'If you no longer wish to receive notifications about this '
'package, please go to the package page [2] and select '
'"%s".', lang) % dnlabel
return body return body
def get_refs(self): def get_refs(self):
...@@ -195,27 +212,31 @@ class FlagNotification(Notification): ...@@ -195,27 +212,31 @@ class FlagNotification(Notification):
def __init__(self, conn, uid, pkgbase_id): def __init__(self, conn, uid, pkgbase_id):
self._user = username_from_id(conn, uid) self._user = username_from_id(conn, uid)
self._pkgbase = pkgbase_from_id(conn, pkgbase_id) self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' + cur = conn.execute('SELECT DISTINCT Users.Email, ' +
'Users.LangPreference FROM Users ' +
'LEFT JOIN PackageComaintainers ' + 'LEFT JOIN PackageComaintainers ' +
'ON PackageComaintainers.UsersID = Users.ID ' + 'ON PackageComaintainers.UsersID = Users.ID ' +
'INNER JOIN PackageBases ' + 'INNER JOIN PackageBases ' +
'ON PackageBases.MaintainerUID = Users.ID OR ' + 'ON PackageBases.MaintainerUID = Users.ID OR ' +
'PackageBases.ID = PackageComaintainers.PackageBaseID ' + 'PackageBases.ID = PackageComaintainers.PackageBaseID ' +
'WHERE PackageBases.ID = ?', [pkgbase_id]) 'WHERE PackageBases.ID = ?', [pkgbase_id])
self._to = [row[0] for row in cur.fetchall()] self._recipients = cur.fetchall()
cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' + cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
'ID = ?', [pkgbase_id]) 'ID = ?', [pkgbase_id])
self._text = cur.fetchone()[0] self._text = cur.fetchone()[0]
super().__init__()
def get_recipients(self): def get_recipients(self):
return self._to return self._recipients
def get_subject(self): def get_subject(self, lang):
return 'AUR Out-of-date Notification for %s' % (self._pkgbase) return self._l10n.translate('AUR Out-of-date Notification for %s',
lang) % (self._pkgbase)
def get_body(self): def get_body(self, lang):
body = 'Your package %s [1] has been flagged out-of-date by ' \ body = self._l10n.translate(
'%s [2]:' % (self._pkgbase, self._user) 'Your package %s [1] has been flagged out-of-date by '
'%s [2]:', lang) % (self._pkgbase, self._user)
body += '\n\n' + self._text body += '\n\n' + self._text
return body return body
...@@ -228,23 +249,26 @@ class OwnershipEventNotification(Notification): ...@@ -228,23 +249,26 @@ class OwnershipEventNotification(Notification):
def __init__(self, conn, uid, pkgbase_id): def __init__(self, conn, uid, pkgbase_id):
self._user = username_from_id(conn, uid) self._user = username_from_id(conn, uid)
self._pkgbase = pkgbase_from_id(conn, pkgbase_id) self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' + cur = conn.execute('SELECT DISTINCT Users.Email, ' +
'Users.LangPreference FROM Users ' +
'INNER JOIN PackageNotifications ' + 'INNER JOIN PackageNotifications ' +
'ON PackageNotifications.UserID = Users.ID WHERE ' + 'ON PackageNotifications.UserID = Users.ID WHERE ' +
'Users.OwnershipNotify = 1 AND ' + 'Users.OwnershipNotify = 1 AND ' +
'PackageNotifications.UserID != ? AND ' + 'PackageNotifications.UserID != ? AND ' +
'PackageNotifications.PackageBaseID = ?', 'PackageNotifications.PackageBaseID = ?',
[uid, pkgbase_id]) [uid, pkgbase_id])
self._to = [row[0] for row in cur.fetchall()] self._recipients = cur.fetchall()
cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' + cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
'ID = ?', [pkgbase_id]) 'ID = ?', [pkgbase_id])
self._text = cur.fetchone()[0] self._text = cur.fetchone()[0]
super().__init__()
def get_recipients(self): def get_recipients(self):
return self._to return self._recipients
def get_subject(self): def get_subject(self, lang):
return 'AUR Ownership Notification for %s' % (self._pkgbase) return self._l10n.translate('AUR Ownership Notification for %s',
lang) % (self._pkgbase)
def get_refs(self): def get_refs(self):
return (aur_location + '/pkgbase/' + self._pkgbase + '/', return (aur_location + '/pkgbase/' + self._pkgbase + '/',
...@@ -252,42 +276,50 @@ class OwnershipEventNotification(Notification): ...@@ -252,42 +276,50 @@ class OwnershipEventNotification(Notification):
class AdoptNotification(OwnershipEventNotification): class AdoptNotification(OwnershipEventNotification):
def get_body(self): def get_body(self, lang):
return 'The package %s [1] was adopted by %s [2].' % \ return self._l10n.translate(
(self._pkgbase, self._user) 'The package %s [1] was adopted by %s [2].', lang) % \
(self._pkgbase, self._user)
class DisownNotification(OwnershipEventNotification): class DisownNotification(OwnershipEventNotification):
def get_body(self): def get_body(self):
return 'The package %s [1] was disowned by %s [2].' % \ return self._l10n.translate(
(self._pkgbase, self._user) 'The package %s [1] was disowned by %s [2].', lang) % \
(self._pkgbase, self._user)
class ComaintainershipEventNotification(Notification): class ComaintainershipEventNotification(Notification):
def __init__(self, conn, uid, pkgbase_id): def __init__(self, conn, uid, pkgbase_id):
self._pkgbase = pkgbase_from_id(conn, pkgbase_id) self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
self._to = get_user_email(conn, uid) cur = conn.execute('SELECT Email, LangPreference FROM Users ' +
'WHERE ID = ?', [uid])
self._to, self._lang = cur.fetchone()
super().__init__()
def get_recipients(self): def get_recipients(self):
return [self._to] return [(self._to, self._lang)]
def get_subject(self): def get_subject(self, lang):
return 'AUR Co-Maintainer Notification for %s' % (self._pkgbase) return self._l10n.translate('AUR Co-Maintainer Notification for %s',
lang) % (self._pkgbase)
def get_refs(self): def get_refs(self):
return (aur_location + '/pkgbase/' + self._pkgbase + '/',) return (aur_location + '/pkgbase/' + self._pkgbase + '/',)
class ComaintainerAddNotification(ComaintainershipEventNotification): class ComaintainerAddNotification(ComaintainershipEventNotification):
def get_body(self): def get_body(self, lang):
return 'You were added to the co-maintainer list of %s [1].' % \ return self._l10n.translate(
(self._pkgbase) 'You were added to the co-maintainer list of %s [1].',
lang) % (self._pkgbase)
class ComaintainerRemoveNotification(ComaintainershipEventNotification): class ComaintainerRemoveNotification(ComaintainershipEventNotification):
def get_body(self): def get_body(self, lang):
return 'You were removed from the co-maintainer list of %s [1].' % \ return self._l10n.translate(
(self._pkgbase) 'You were removed from the co-maintainer list of %s [1].',
lang) % (self._pkgbase)
class DeleteNotification(Notification): class DeleteNotification(Notification):
...@@ -298,31 +330,36 @@ class DeleteNotification(Notification): ...@@ -298,31 +330,36 @@ class DeleteNotification(Notification):
self._new_pkgbase = pkgbase_from_id(conn, new_pkgbase_id) self._new_pkgbase = pkgbase_from_id(conn, new_pkgbase_id)
else: else:
self._new_pkgbase = None self._new_pkgbase = None
cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' + cur = conn.execute('SELECT DISTINCT Users.Email, ' +
'Users.LangPreference FROM Users ' +
'INNER JOIN PackageNotifications ' + 'INNER JOIN PackageNotifications ' +
'ON PackageNotifications.UserID = Users.ID WHERE ' + 'ON PackageNotifications.UserID = Users.ID WHERE ' +
'PackageNotifications.UserID != ? AND ' + 'PackageNotifications.UserID != ? AND ' +
'PackageNotifications.PackageBaseID = ?', 'PackageNotifications.PackageBaseID = ?',
[uid, old_pkgbase_id]) [uid, old_pkgbase_id])
self._to = [row[0] for row in cur.fetchall()] self._recipients = cur.fetchall()
super().__init__()
def get_recipients(self): def get_recipients(self):
return self._to return self._recipients
def get_subject(self): def get_subject(self, lang):
return 'AUR Package deleted: %s' % (self._old_pkgbase) return self._l10n.translate('AUR Package deleted: %s', lang) % \
(self._old_pkgbase)
def get_body(self): def get_body(self, lang):
if self._new_pkgbase: if self._new_pkgbase:
return '%s [1] merged %s [2] into %s [3].\n\n' \ dnlabel = self._l10n.translate('Disable notifications', lang)
'If you no longer wish receive notifications about the ' \ return self._l10n.translate(
'new package, please go to [3] and click "%s".' % \ '%s [1] merged %s [2] into %s [3].\n\n'
(self._user, self._old_pkgbase, self._new_pkgbase, 'If you no longer wish receive notifications about the '
'Disable notifications') 'new package, please go to [3] and click "%s".', lang) % \
(self._user, self._old_pkgbase, self._new_pkgbase, dnlabel)
else: else:
return '%s [1] deleted %s [2].\n\n' \ return self._l10n.translate(
'You will no longer receive notifications about this ' \ '%s [1] deleted %s [2].\n\n'
'package.' % (self._user, self._old_pkgbase) 'You will no longer receive notifications about this '
'package.', lang) % (self._user, self._old_pkgbase)
def get_refs(self): def get_refs(self):
refs = (aur_location + '/account/' + self._user + '/', refs = (aur_location + '/account/' + self._user + '/',
...@@ -353,13 +390,13 @@ class RequestOpenNotification(Notification): ...@@ -353,13 +390,13 @@ class RequestOpenNotification(Notification):
self._merge_into = merge_into self._merge_into = merge_into
def get_recipients(self): def get_recipients(self):
return [self._to] return [(self._to, 'en')]
def get_subject(self): def get_subject(self, lang):
return '[PRQ#%d] %s Request for %s' % \ return '[PRQ#%d] %s Request for %s' % \
(self._reqid, self._reqtype.title(), self._pkgbase) (self._reqid, self._reqtype.title(), self._pkgbase)
def get_body(self): def get_body(self, lang):
if self._merge_into: if self._merge_into:
body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \ body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
(self._user, self._pkgbase, self._merge_into) (self._user, self._pkgbase, self._merge_into)
...@@ -405,12 +442,12 @@ class RequestCloseNotification(Notification): ...@@ -405,12 +442,12 @@ class RequestCloseNotification(Notification):
self._reason = reason self._reason = reason
def get_recipients(self): def get_recipients(self):
return [self._to] return [(self._to, 'en')]
def get_subject(self): def get_subject(self, lang):
return '[PRQ#%d] Request %s' % (self._reqid, self._reason.title()) return '[PRQ#%d] Request %s' % (self._reqid, self._reason.title())
def get_body(self): def get_body(self, lang):
if self._user: if self._user:
body = 'Request #%d has been %s by %s [1]' % \ body = 'Request #%d has been %s by %s [1]' % \
(self._reqid, self._reason, self._user) (self._reqid, self._reason, self._user)
...@@ -440,22 +477,25 @@ class RequestCloseNotification(Notification): ...@@ -440,22 +477,25 @@ class RequestCloseNotification(Notification):
class TUVoteReminderNotification(Notification): class TUVoteReminderNotification(Notification):
def __init__(self, conn, vote_id): def __init__(self, conn, vote_id):
self._vote_id = int(vote_id) self._vote_id = int(vote_id)
cur = conn.execute('SELECT Email FROM Users ' + cur = conn.execute('SELECT Email, LangPreference FROM Users ' +
'WHERE AccountTypeID IN (2, 4) AND ID NOT IN ' + 'WHERE AccountTypeID IN (2, 4) AND ID NOT IN ' +
'(SELECT UserID FROM TU_Votes ' + '(SELECT UserID FROM TU_Votes ' +
'WHERE TU_Votes.VoteID = ?)', [vote_id]) 'WHERE TU_Votes.VoteID = ?)', [vote_id])
self._to = [row[0] for row in cur.fetchall()] self._recipients = cur.fetchall()
super().__init__()
def get_recipients(self): def get_recipients(self):
return self._to return self._recipients
def get_subject(self): def get_subject(self, lang):
return 'TU Vote Reminder: Proposal %d' % (self._vote_id) return self._l10n.translate('TU Vote Reminder: Proposal %d', lang) % \
(self._vote_id)