From a37996395b148da69889450b1b33149a4b21ebb8 Mon Sep 17 00:00:00 2001 From: "zia.fazal@arbisoft.com" Date: Tue, 27 Sep 2022 18:31:25 +0500 Subject: [PATCH] fix: added keyword substitution support in edx_ace based bulk course email fix: pylint quality violation fix: added positive assert and docs link fix: mark string for i18n according to best practices --- lms/djangoapps/bulk_email/messages.py | 9 +++++++ lms/djangoapps/bulk_email/tests/test_email.py | 27 +++++++++++++++++++ lms/envs/common.py | 1 + .../edx_ace/bulkemail/email/body.html | 2 +- .../edx_ace/bulkemail/email/body.txt | 2 +- .../instructor_dashboard_2/send_email.html | 13 +++++++-- 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lms/djangoapps/bulk_email/messages.py b/lms/djangoapps/bulk_email/messages.py index 4f83c5f83f..8052ca0158 100644 --- a/lms/djangoapps/bulk_email/messages.py +++ b/lms/djangoapps/bulk_email/messages.py @@ -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'], diff --git a/lms/djangoapps/bulk_email/tests/test_email.py b/lms/djangoapps/bulk_email/tests/test_email.py index cdcf2eb0e8..b67864381b 100644 --- a/lms/djangoapps/bulk_email/tests/test_email.py +++ b/lms/djangoapps/bulk_email/tests/test_email.py @@ -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 diff --git a/lms/envs/common.py b/lms/envs/common.py index 0b1fe87fcf..bc532fa105 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -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. diff --git a/lms/templates/bulk_email/edx_ace/bulkemail/email/body.html b/lms/templates/bulk_email/edx_ace/bulkemail/email/body.html index 43768b8222..8cb009e40a 100644 --- a/lms/templates/bulk_email/edx_ace/bulkemail/email/body.html +++ b/lms/templates/bulk_email/edx_ace/bulkemail/email/body.html @@ -8,7 +8,7 @@

{% autoescape off %} - {{ course_email.html_message }} + {{ formatted_html_message }} {% endautoescape %}

diff --git a/lms/templates/bulk_email/edx_ace/bulkemail/email/body.txt b/lms/templates/bulk_email/edx_ace/bulkemail/email/body.txt index b3d80b9e2a..c14e6d8882 100644 --- a/lms/templates/bulk_email/edx_ace/bulkemail/email/body.txt +++ b/lms/templates/bulk_email/edx_ace/bulkemail/email/body.txt @@ -1,6 +1,6 @@ {% load i18n %} {% autoescape off %} -{{ course_email.text_message }} +{{ formatted_text_message }} {% endautoescape %} diff --git a/lms/templates/instructor/instructor_dashboard_2/send_email.html b/lms/templates/instructor/instructor_dashboard_2/send_email.html index 06358c1a8e..e6d729480a 100644 --- a/lms/templates/instructor/instructor_dashboard_2/send_email.html +++ b/lms/templates/instructor/instructor_dashboard_2/send_email.html @@ -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
-

${_("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.")}

+

+ ${_("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('').format(settings.COURSE_BULK_EMAIL_HELP_URL), + end_link=HTML('') + ) + } +

${_("CAUTION!")}