def test_first_parameter_must_be_string(self):
     with self.assertRaises(AttributeError):
         replace_context_variable([], {})
     with self.assertRaises(AttributeError):
         replace_context_variable(1, {})
     with self.assertRaises(AttributeError):
         replace_context_variable({}, {})
     replace_context_variable('', {})
 def test_second_parameter_must_be_dictionary(self):
     with self.assertRaises(TypeError):
         replace_context_variable('', '')
     with self.assertRaises(TypeError):
         replace_context_variable('', [])
     with self.assertRaises(TypeError):
         replace_context_variable('', 1)
     replace_context_variable('', {})
Exemplo n.º 3
0
    def send(self, context=None):
        """
        When sending an email a set of attributes will be required.

        The required attributes are mainly dictated by django.core.mail
        used to send mail:
        * Message or body.
        * Subject.
        * Recipients list or to.
        * From email

        :param context: A dictionary with context variables to be used with
                        the subject and the message.
        :return: A tuple (result, message) where result is a boolean indicating
                 if mail could be sent or not. An a message in case the mail
                 could not be sent the message will be the reason. This could
                 have future uses if logging is implemented.
        """
        subject = self.subject
        body = self.body
        if context is None:
            # Needed whe no context is received so no replacement is tried.
            pass
        elif not isinstance(context, dict):
            raise ValueError(
                _('The argument for send method must be a '
                  'mapping.'))
        else:
            subject = replace_context_variable(text=self.subject,
                                               context_variable=context)
            body = replace_context_variable(text=self.body,
                                            context_variable=context)
        msg = EmailMultiAlternatives(subject=subject,
                                     from_email=self.from_email,
                                     to=clean_address_list(self.to),
                                     cc=clean_address_list(self.cc),
                                     bcc=clean_address_list(self.bcc),
                                     reply_to=clean_address_list(
                                         self.reply_to))
        msg.body = body
        msg.attach_alternative(body, 'text/html')
        return msg.send()
 def test_return_valid_value_without_context_variable_in_text(self):
     text = 'Dummy text {context_variable} {fake%2d0} {more-fake}.'
     expected = 'Dummy text example {fake%2d0} {more-fake}.'
     data = {'context_variable': 'example', 'replaced_text': 'of replace'}
     assert expected == replace_context_variable(text, data)
 def test_return_valid_value_without_all_variable_mapping(self):
     text = 'Dummy text {context_variable}.'
     expected = 'Dummy text example.'
     data = {'context_variable': 'example', 'replaced_text': 'of replace'}
     assert expected == replace_context_variable(text, data)
 def test_return_main_text_with_multiple_variables_replaced(self):
     text = 'Dummy text {context_variable} {replaced_text}.'
     expected = 'Dummy text example of replace.'
     data = {'context_variable': 'example', 'replaced_text': 'of replace'}
     assert expected == replace_context_variable(text, data)
 def test_return_main_text_with_variables_replaced(self):
     text = 'Dummy text {context_variable}.'
     expected = 'Dummy text example.'
     data = {'context_variable': 'example'}
     assert expected == replace_context_variable(text, data)