management commands sending emails: translate texts for the email recipient
This commit is contained in:
parent
ae61712fbc
commit
58aae90b0a
@ -13,6 +13,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from nsupdate.main.models import Domain
|
||||
from nsupdate.main.dnstools import FQDN, query_ns, NameServerNotAvailable
|
||||
from nsupdate.utils.mail import translate_for_user, send_mail_to_user
|
||||
|
||||
|
||||
MSG = _("""\
|
||||
@ -96,11 +97,14 @@ class Command(BaseCommand):
|
||||
d.available = False # see comment in check_dns()
|
||||
d.public = False
|
||||
if notify_user:
|
||||
from_addr = None # will use DEFAULT_FROM_EMAIL
|
||||
to_addr = creator.email
|
||||
subject = _("issue with your domain %(domain)s") % dict(domain=domain)
|
||||
msg = MSG % dict(domain=domain, comment=comment)
|
||||
send_mail(subject, msg, from_addr, [to_addr], fail_silently=True)
|
||||
subject, msg = translate_for_user(
|
||||
creator,
|
||||
_("issue with your domain %(domain)s"),
|
||||
MSG
|
||||
)
|
||||
subject = subject % dict(domain=domain)
|
||||
msg = msg % dict(domain=domain, comment=comment)
|
||||
send_mail_to_user(creator, subject, msg)
|
||||
msg = "setting unavailable flag for domain %s (created by %s)\n" % (domain, creator, )
|
||||
self.stdout.write(msg)
|
||||
d.save()
|
||||
|
@ -10,6 +10,7 @@ from django.db import transaction
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from nsupdate.main.models import Host
|
||||
from nsupdate.utils.mail import translate_for_user, send_mail_to_user
|
||||
|
||||
|
||||
ABUSE_MSG = _("""\
|
||||
@ -140,11 +141,14 @@ class Command(BaseCommand):
|
||||
self.stdout.write("setting abuse flag for host %s (created by %s, client faults: %d)\n" % (
|
||||
fqdn, creator, faults_count))
|
||||
if notify_user:
|
||||
from_addr = None # will use DEFAULT_FROM_EMAIL
|
||||
to_addr = creator.email
|
||||
subject = _("issue with your host %(fqdn)s") % dict(fqdn=fqdn)
|
||||
msg = ABUSE_MSG % dict(fqdn=fqdn, comment=comment, faults_count=faults_count)
|
||||
send_mail(subject, msg, from_addr, [to_addr], fail_silently=True)
|
||||
subject, msg = translate_for_user(
|
||||
creator,
|
||||
_("issue with your host %(fqdn)s"),
|
||||
ABUSE_MSG
|
||||
)
|
||||
subject = subject % dict(fqdn=fqdn)
|
||||
msg = msg % dict(fqdn=fqdn, comment=comment, faults_count=faults_count)
|
||||
send_mail_to_user(creator, subject, msg)
|
||||
if reset_client:
|
||||
h.client_faults = 0
|
||||
if reset_server:
|
||||
|
@ -6,12 +6,12 @@ from datetime import datetime
|
||||
from optparse import make_option
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.core.mail import send_mail
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from nsupdate.main.models import Host
|
||||
from nsupdate.utils.mail import translate_for_user, send_mail_to_user
|
||||
|
||||
DAY = 24 * 3600 # [s]
|
||||
T_ip = 325 * DAY # age of last ip update so we starts considering host as stale
|
||||
@ -159,11 +159,14 @@ class Command(BaseCommand):
|
||||
creator = h.created_by
|
||||
staleness, email_msg, log_msg = check_staleness(h)
|
||||
if email_msg and notify_user:
|
||||
from_addr = None # will use DEFAULT_FROM_EMAIL
|
||||
to_addr = creator.email
|
||||
subject = _("issue with your host %(host)s") % dict(host=host)
|
||||
subject, msg = translate_for_user(
|
||||
creator,
|
||||
_("issue with your host %(host)s"),
|
||||
email_msg
|
||||
)
|
||||
subject = subject % dict(host=host)
|
||||
email_msg = email_msg % dict(host=host, staleness=staleness, comment=comment)
|
||||
send_mail(subject, email_msg, from_addr, [to_addr], fail_silently=True)
|
||||
send_mail_to_user(creator, subject, email_msg)
|
||||
if log_msg:
|
||||
log_msg = log_msg % dict(host=host, staleness=staleness, creator=creator)
|
||||
self.stdout.write(log_msg)
|
||||
|
18
nsupdate/utils/_tests/test_mail.py
Normal file
18
nsupdate/utils/_tests/test_mail.py
Normal file
@ -0,0 +1,18 @@
|
||||
"""
|
||||
Tests for mail module.
|
||||
"""
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from ..mail import translate_for_user
|
||||
|
||||
|
||||
class TestTransUser(object):
|
||||
def test(self):
|
||||
User = get_user_model()
|
||||
user = User.objects.get(username='test')
|
||||
user.profile.language = 'de'
|
||||
msgs = [_('German'), _('English')]
|
||||
msgs = translate_for_user(user, *msgs)
|
||||
assert msgs == ['Deutsch', 'Englisch']
|
40
nsupdate/utils/mail.py
Normal file
40
nsupdate/utils/mail.py
Normal file
@ -0,0 +1,40 @@
|
||||
"""
|
||||
sending emails
|
||||
"""
|
||||
|
||||
from django.utils import translation
|
||||
from django.core.mail import send_mail
|
||||
|
||||
|
||||
def translate_for_user(user, *msgs):
|
||||
"""
|
||||
translate msgs for user
|
||||
|
||||
this is typically used when emails are sent to a user
|
||||
(who is not the currently active user)
|
||||
|
||||
:param user: User instance
|
||||
:param msgs: list of lazy translatable strings
|
||||
:return: list of translated strings
|
||||
"""
|
||||
lang = user.profile.language
|
||||
saved_lang = translation.get_language()
|
||||
try:
|
||||
translation.activate(lang)
|
||||
# "using" the msg triggers lazy translation
|
||||
return [msg + u'' for msg in msgs]
|
||||
finally:
|
||||
translation.activate(saved_lang)
|
||||
|
||||
|
||||
def send_mail_to_user(user, subject, msg, from_addr=None):
|
||||
"""
|
||||
send an email to a specific user
|
||||
|
||||
:param user: User instance
|
||||
:param subject: email subject
|
||||
:param msg: email plain text
|
||||
:param from_addr: sender address (None means DEFAULT_FROM_EMAIL)
|
||||
:return:
|
||||
"""
|
||||
return send_mail(subject, msg, from_addr, [user.email], fail_silently=True)
|
Loading…
x
Reference in New Issue
Block a user