Merge pull request #31044 from openedx/ziafazal/keyword-substitution-edx-ace
fix: added keyword substitution support in edx_ace based bulk course emails
This commit is contained in:
@@ -8,6 +8,7 @@ from django.core.mail import EmailMultiAlternatives
|
||||
from edx_ace import ace
|
||||
from edx_ace.recipient import Recipient
|
||||
|
||||
from common.djangoapps.util.keyword_substitution import substitute_keywords_with_data
|
||||
from lms.djangoapps.bulk_email.message_types import BulkEmail
|
||||
from openedx.core.lib.celery.task_utils import emulate_http_request
|
||||
|
||||
@@ -69,6 +70,14 @@ class ACEEmail(CourseEmailMessage):
|
||||
"""
|
||||
self.site = site
|
||||
self.user = User.objects.get(email=email_context['email'])
|
||||
text_message = email_context['course_email'].text_message
|
||||
html_message = email_context['course_email'].html_message
|
||||
formatted_text_message = substitute_keywords_with_data(text_message, email_context)
|
||||
formatted_html_message = substitute_keywords_with_data(html_message, email_context)
|
||||
email_context.update({
|
||||
'formatted_text_message': formatted_text_message,
|
||||
'formatted_html_message': formatted_html_message,
|
||||
})
|
||||
message = BulkEmail(context=email_context).personalize(
|
||||
recipient=Recipient(email_context['user_id'], email_context['email']),
|
||||
language=email_context['course_language'],
|
||||
|
||||
@@ -229,6 +229,33 @@ class AceEmailTestCase(SendEmailWithMockedUgettextMixin, EmailSendFromDashboardT
|
||||
response = self.client.post(self.send_mail_url, test_email)
|
||||
self.assertEqual(email_sent_with_ace, mock_ace_email_send.called)
|
||||
|
||||
def test_keyword_substitution_in_message_body(self):
|
||||
"""
|
||||
Make sure keywords like `%%USER_FULLNAME%%`, `%%COURSE_DISPLAY_NAME%%` are substituted in message body
|
||||
"""
|
||||
test_email = {
|
||||
'action': 'Send email',
|
||||
'send_to': '["staff"]',
|
||||
'subject': 'test subject for staff',
|
||||
'message': 'Hi %%USER_FULLNAME%%, Welcome to %%COURSE_DISPLAY_NAME%% course'
|
||||
}
|
||||
self.client.post(self.send_mail_url, test_email)
|
||||
text_message_body = mail.outbox[0].body
|
||||
html_message_body = ''
|
||||
for content, mimetype in mail.outbox[0].alternatives:
|
||||
if mimetype == 'text/html':
|
||||
html_message_body = content
|
||||
break
|
||||
|
||||
self.assertNotIn('%%USER_FULLNAME%%', text_message_body)
|
||||
self.assertNotIn('%%COURSE_DISPLAY_NAME%%', text_message_body)
|
||||
self.assertNotIn('%%USER_FULLNAME%%', html_message_body)
|
||||
self.assertNotIn('%%COURSE_DISPLAY_NAME%%', html_message_body)
|
||||
self.assertIn(f'Hi {self.instructor.get_full_name()}', text_message_body)
|
||||
self.assertIn(f'Welcome to {self.course.display_name}', text_message_body)
|
||||
self.assertIn(f'Hi {self.instructor.get_full_name()}', html_message_body)
|
||||
self.assertIn(f'Welcome to {self.course.display_name}', html_message_body)
|
||||
|
||||
|
||||
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
|
||||
@ddt.ddt
|
||||
|
||||
@@ -5144,6 +5144,7 @@ TEAMS_HELP_URL = "https://edx.readthedocs.io/projects/open-edx-building-and-runn
|
||||
TEXTBOOKS_HELP_URL = "https://edx.readthedocs.io/projects/open-edx-building-and-running-a-course/en/latest/course_assets/textbooks.html"
|
||||
WIKI_HELP_URL = "https://edx.readthedocs.io/projects/open-edx-building-and-running-a-course/en/latest/course_assets/course_wiki.html"
|
||||
CUSTOM_PAGES_HELP_URL = "https://edx.readthedocs.io/projects/open-edx-building-and-running-a-course/en/latest/course_assets/pages.html#adding-custom-pages"
|
||||
COURSE_BULK_EMAIL_HELP_URL = "https://edx.readthedocs.io/projects/open-edx-building-and-running-a-course/en/latest/manage_live_course/bulk_email.html"
|
||||
|
||||
################# Bulk Course Email Settings #################
|
||||
# If set, recipients of bulk course email messages will be filtered based on the last_login date of their User account.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<td>
|
||||
<p style="color: rgba(0,0,0,.75);">
|
||||
{% autoescape off %}
|
||||
{{ course_email.html_message }}
|
||||
{{ formatted_html_message }}
|
||||
{% endautoescape %}
|
||||
<br/>
|
||||
</p>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% load i18n %}
|
||||
{% autoescape off %}
|
||||
{{ course_email.text_message }}
|
||||
{{ formatted_text_message }}
|
||||
{% endautoescape %}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<%page args="section_data" expression_filter="h"/>
|
||||
<%!
|
||||
from django.utils.translation import ugettext as _
|
||||
from openedx.core.djangolib.markup import HTML
|
||||
from openedx.core.djangolib.markup import HTML, Text
|
||||
%>
|
||||
|
||||
|
||||
@@ -91,7 +91,16 @@ from openedx.core.djangolib.markup import HTML
|
||||
</li>
|
||||
</ul>
|
||||
<div class="submit-email-action">
|
||||
<p class="copy">${_("We recommend sending learners no more than one email message per week. Before you send your email, review the text carefully and send it to yourself first, so that you can preview the formatting and make sure embedded images and links work correctly.")}</p>
|
||||
<p class="copy">
|
||||
${_("We recommend sending learners no more than one email message per week. Before you send your email, review the text carefully and send it to yourself first, so that you can preview the formatting and make sure embedded images and links work correctly.")}
|
||||
${Text(
|
||||
_("You can include keywords in your messages. For list of available keywords see the {start_link}documentation{end_link}."
|
||||
)).format(
|
||||
start_link=HTML('<a href="{}">').format(settings.COURSE_BULK_EMAIL_HELP_URL),
|
||||
end_link=HTML('</a>')
|
||||
)
|
||||
}
|
||||
</p>
|
||||
</div>
|
||||
<div class="submit-email-warning">
|
||||
<p class="copy"><span style="color: red;"><b>${_("CAUTION!")}</b></span>
|
||||
|
||||
Reference in New Issue
Block a user