Translatable bulk_email from Address based on platform`s default language
This commit is contained in:
@@ -35,6 +35,7 @@ from django.contrib.auth.models import User
|
||||
from django.core.mail import EmailMultiAlternatives, get_connection
|
||||
from django.core.mail.message import forbid_multi_line_headers
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import override as override_language, ugettext as _
|
||||
|
||||
from bulk_email.models import CourseEmail, Optout
|
||||
from courseware.courses import get_course
|
||||
@@ -373,7 +374,12 @@ def _get_source_address(course_id, course_title, truncate=True):
|
||||
# character appears.
|
||||
course_name = re.sub(r"[^\w.-]", '_', course_id.course)
|
||||
|
||||
from_addr_format = u'"{course_title}" Course Staff <{course_name}-{from_email}>'
|
||||
with override_language(settings.LANGUAGE_CODE):
|
||||
from_addr_format = u'{name} {email}'.format(
|
||||
# Translators: Bulk email from address e.g. ("Physics 101" Course Staff)
|
||||
name=_('"{course_title}" Course Staff'),
|
||||
email=u'<{course_name}-{from_email}>',
|
||||
)
|
||||
|
||||
def format_address(course_title_no_quotes):
|
||||
"""
|
||||
|
||||
@@ -16,6 +16,7 @@ from django.core.urlresolvers import reverse
|
||||
from django.core.management import call_command
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from django.utils import translation
|
||||
from bulk_email.models import Optout, BulkEmailFlag
|
||||
from bulk_email.tasks import _get_source_address, _get_course_email_context
|
||||
from openedx.core.djangoapps.course_groups.models import CourseCohort
|
||||
@@ -132,6 +133,90 @@ class EmailSendFromDashboardTestCase(SharedModuleStoreTestCase):
|
||||
BulkEmailFlag.objects.all().delete()
|
||||
|
||||
|
||||
@attr(shard=1)
|
||||
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
|
||||
class TestLocalizedFromAddress(EmailSendFromDashboardTestCase):
|
||||
|
||||
original_ugettext = None
|
||||
mocked_lang = 'ar'
|
||||
|
||||
def setUp(self):
|
||||
super(TestLocalizedFromAddress, self).setUp()
|
||||
|
||||
translations = translation.trans_real._translations # pylint: disable=protected-access
|
||||
|
||||
with translation.override(self.mocked_lang):
|
||||
# In order to undo it later
|
||||
self.original_ugettext = translations[self.mocked_lang].ugettext
|
||||
|
||||
mocked_ugettext = self.get_mocked_ugettext(self.mocked_lang)
|
||||
translations[self.mocked_lang].ugettext = mocked_ugettext
|
||||
|
||||
def get_mocked_ugettext(self, lang_code):
|
||||
"""
|
||||
Mocks ugettext to return the lang code with the original string.
|
||||
|
||||
e.g.
|
||||
|
||||
>>> ugettext = self.mock_ugettext('ar')
|
||||
>>> ugettext('Hello') == '@AR Hello@'
|
||||
"""
|
||||
def mocked_ugettext(msg):
|
||||
"""
|
||||
A mock of ugettext to isolate it from the real `.mo` files.
|
||||
"""
|
||||
return u'@{} {}@'.format(lang_code.upper(), msg)
|
||||
|
||||
return mocked_ugettext
|
||||
|
||||
def send_email(self):
|
||||
"""
|
||||
Sends a dummy email to check the `from_addr` translation.
|
||||
"""
|
||||
test_email = {
|
||||
'action': 'send',
|
||||
'send_to': '["myself"]',
|
||||
'subject': 'test subject for myself',
|
||||
'message': 'test message for myself'
|
||||
}
|
||||
|
||||
self.client.post(self.send_mail_url, test_email)
|
||||
|
||||
return mail.outbox[0]
|
||||
|
||||
@override_settings(LANGUAGE_CODE='en')
|
||||
def test_english_platform(self):
|
||||
"""
|
||||
Test if the email `from` is localized to the platform's preference.
|
||||
"""
|
||||
|
||||
message = self.send_email()
|
||||
|
||||
self.assertNotRegexpMatches(
|
||||
message.from_email,
|
||||
'@.* Course Staff@'
|
||||
)
|
||||
|
||||
@override_settings(LANGUAGE_CODE='ar')
|
||||
def test_arabic_platform(self):
|
||||
"""
|
||||
Test if the email `from` is localized to the platform's preference.
|
||||
"""
|
||||
|
||||
message = self.send_email()
|
||||
|
||||
self.assertRegexpMatches(
|
||||
message.from_email,
|
||||
'@AR .* Course Staff@'
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
super(TestLocalizedFromAddress, self).tearDown()
|
||||
|
||||
translations = translation.trans_real._translations # pylint: disable=protected-access
|
||||
translations[self.mocked_lang].ugettext = self.original_ugettext
|
||||
|
||||
|
||||
@attr(shard=1)
|
||||
@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True))
|
||||
class TestEmailSendFromDashboardMockedHtmlToText(EmailSendFromDashboardTestCase):
|
||||
|
||||
Reference in New Issue
Block a user