Commit 846897ad authored by Michal Čihař's avatar Michal Čihař

Factor out email sending from profile

parent 35b699bf
......@@ -46,6 +46,53 @@ import logging
logger = logging.getLogger('weblate')
def send_notification_email(language, email, notification, translation_obj, context = {}, headers = {}):
'''
Renders and sends notification email.
'''
cur_language = translation.get_language()
try:
logger.info('sending notification %s on %s to %s', notification, translation_obj.__unicode__(), email)
# Load user language
translation.activate(language)
# Template names
subject_template = 'mail/%s_subject.txt' % notification
body_template = 'mail/%s.txt' % notification
# Adjust context
domain = Site.objects.get_current().domain
context['translation'] = translation_obj
context['current_site'] = domain
context['translation_url'] = 'http://%s%s' % (domain, translation_obj.get_absolute_url())
# Render subject
subject = render_to_string(subject_template, context)
# Render body
body = render_to_string(body_template, context)
# Define headers
headers['Auto-Submitted'] = 'auto-generated'
headers['X-AutoGenerated'] = 'yes'
headers['Precedence'] = 'bulk'
headers['X-Mailer'] = 'Weblate %s' % weblate.VERSION
# Create message
email = EmailMessage(
settings.EMAIL_SUBJECT_PREFIX + subject.strip(),
body,
to = [email],
headers = headers
)
# Send it out
email.send(fail_silently = False)
finally:
translation.activate(cur_language)
class ProfileManager(models.Manager):
'''
Manager providing shortcuts for subscription queries.
......@@ -128,47 +175,7 @@ class Profile(models.Model):
'''
Wrapper for sending notifications to user.
'''
cur_language = translation.get_language()
try:
logger.info('sending notification %s on %s to %s', notification, translation_obj.__unicode__(), self.user.email)
# Load user language
translation.activate(self.language)
# Template names
subject_template = 'mail/%s_subject.txt' % notification
body_template = 'mail/%s.txt' % notification
# Adjust context
domain = Site.objects.get_current().domain
context['translation'] = translation_obj
context['current_site'] = domain
context['translation_url'] = 'http://%s%s' % (domain, translation_obj.get_absolute_url())
# Render subject
subject = render_to_string(subject_template, context)
# Render body
body = render_to_string(body_template, context)
# Define headers
headers['Auto-Submitted'] = 'auto-generated'
headers['X-AutoGenerated'] = 'yes'
headers['Precedence'] = 'bulk'
headers['X-Mailer'] = 'Weblate %s' % weblate.VERSION
# Create message
email = EmailMessage(
settings.EMAIL_SUBJECT_PREFIX + subject.strip(),
body,
to = [self.user.email],
headers = headers
)
# Send it out
email.send(fail_silently = False)
finally:
translation.activate(cur_language)
send_notification_email(self.language, self.user.email, notification, translation_obj, context, headers)
def notify_any_translation(self, unit, oldunit):
'''
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment