def _render(format_string, message_body, context): """ Create a text message using a template, message body and context. Convert message body (`message_body`) into an email message using the provided template. The template is a format string, which is rendered using format() with the provided `context` dict. Any keywords encoded in the form %%KEYWORD%% found in the message body are substituted with user data before the body is inserted into the template. Output is returned as a unicode string. It is not encoded as utf-8. Such encoding is left to the email code, which will use the value of settings.DEFAULT_CHARSET to encode the message. """ # Substitute all %%-encoded keywords in the message body if 'user_id' in context and 'course_id' in context: message_body = substitute_keywords_with_data(message_body, context) result = format_string.format(**context) # Note that the body tag in the template will now have been # "formatted", so we need to do the same to the tag being # searched for. message_body_tag = COURSE_EMAIL_MESSAGE_BODY_TAG.format() result = result.replace(message_body_tag, message_body, 1) # finally, return the result, after wrapping long lines and without converting to an encoded byte array. return wrap_message(result)
def test_subbing_no_userid_or_courseid(self): """ Tests that no subbing occurs if no user_id or no course_id is given. """ test_string = 'This string should not be subbed here %%USER_ID%%' no_course_context = { key: value for key, value in self.context.items() if key != 'course_title' } result = Ks.substitute_keywords_with_data(test_string, no_course_context) assert test_string == result no_user_id_context = { key: value for key, value in self.context.items() if key != 'user_id' } result = Ks.substitute_keywords_with_data(test_string, no_user_id_context) assert test_string == result
def test_subbing_no_userid_or_courseid(self): """ Tests that no subbing occurs if no user_id or no course_id is given. """ test_string = 'This string should not be subbed here %%USER_ID%%' no_course_context = dict( (key, value) for key, value in six.iteritems(self.context) if key != 'course_title' ) result = Ks.substitute_keywords_with_data(test_string, no_course_context) self.assertEqual(test_string, result) no_user_id_context = dict( (key, value) for key, value in six.iteritems(self.context) if key != 'user_id' ) result = Ks.substitute_keywords_with_data(test_string, no_user_id_context) self.assertEqual(test_string, result)
def test_sub_multiple_tags(self, test_string, expected): """ Test that subbing works with multiple subtags """ anon_id = '123456789' with patch('common.djangoapps.util.keyword_substitution.anonymous_id_from_user_id', lambda user_id: anon_id): result = Ks.substitute_keywords_with_data( test_string, self.context, ) assert result == expected
def test_course_name_sub(self, test_string, expected): """ Tests subbing course name in various scenarios """ course_name = self.course.display_name result = Ks.substitute_keywords_with_data( test_string, self.context, ) assert course_name in result assert result == expected
def test_should_not_sub(self): """ Test that sub-ing doesn't work without subtags """ test_string = "this string has no subtags" result = Ks.substitute_keywords_with_data( test_string, self.context, ) assert test_string == result
def test_illegal_subtag(self): """ Test that sub-ing doesn't ocurr with illegal tags """ test_string = "%%user_id%%" result = Ks.substitute_keywords_with_data( test_string, self.context, ) assert test_string == result
def test_anonymous_id_sub(self): """ Test that anonymous_id is subbed """ test_string = "Turn %%USER_ID%% into anonymous id" anonymous_id = Ks.anonymous_id_from_user_id(self.user.id) result = Ks.substitute_keywords_with_data( test_string, self.context, ) assert '%%USER_ID%%' not in result assert anonymous_id in result
def test_name_sub(self): """ Test that the user's full name is correctly subbed """ test_string = "This is the test string. subthis: %%USER_FULLNAME%% into user name" user_name = self.user.profile.name result = Ks.substitute_keywords_with_data( test_string, self.context, ) assert '%%USER_FULLNAME%%' not in result assert user_name in result