Beispiel #1
0
 def test_render_html_fragments_with_images_test_true(self):
     header = EmailKindFragment.objects.create(
         name='header',
         content='<div>Hello {{ name }}</div><img src="cid:logo">',
         default_context={'name': 'Yo'},
         is_plain=False)
     ekind = EmailKind.objects.create(
         name='my-test-email',
         language='es',
         template=
         '{{ fragments.header }}<h1>{{ sentence }}</h1><img src="cid:other">',
         plain_template='Hola, {{ name }}',
         default_context={'name': 'Bran'})
     logo = create_upload_image()
     logo_img = header.images.create(placeholder_name='logo', image=logo)
     other = create_upload_image()
     other_img = ekind.images.create(placeholder_name='other', image=other)
     ekind.fragments.add(header)
     context = {'sentence': 'Hold the door!'}
     rendered = render_html(ekind, context, test=True)
     self.assertEqual(re.sub('<img.*?>', '', rendered),
                      '<div>Hello Bran</div><h1>Hold the door!</h1>')
     imgs = re.findall(r'<img\s+src="(?P<img>.*?)"', rendered)
     self.assertEqual(len(imgs), 2)
     logo_src, other_src = imgs
     self.assertEqual(logo_img.image.url, logo_src)
     self.assertEqual(other_img.image.url, other_src)
Beispiel #2
0
    def test_render_html_with_embedded_images_testing_false(self):
        ekind = EmailKind.objects.create(
            name='my-test-email',
            language='es',
            template=('<h1>Hola, {{ first_name }}</h1>'
                      '<img src="cid:logo-solocal">'
                      '<img src="cid:logo-qdq">'),
            plain_template='Hola, {{ first_name }}',
            default_context={'first_name': 'Luisa'})
        upload_image1 = create_upload_image()
        upload_image2 = create_upload_image()
        EmbeddedImage.objects.create(kind=ekind,
                                     placeholder_name='logo-solocal',
                                     image=upload_image1)
        EmbeddedImage.objects.create(kind=ekind,
                                     placeholder_name='logo-qdq',
                                     image=upload_image2)
        image1 = EmbeddedImage.objects.get(kind=ekind,
                                           placeholder_name='logo-solocal')
        self.assertEqual('', image1.content_id)

        rendered = render_html(ekind, {})
        image1 = EmbeddedImage.objects.get(kind=ekind,
                                           placeholder_name='logo-solocal')
        self.assertIn('src="cid:', rendered)
        self.assertNotIn('src="cid:<', rendered)
        self.assertIn('logo-solocal@', rendered)
        self.assertIn('logo-qdq@', rendered)
        self.assertTrue(len(image1.content_id) > 0)
Beispiel #3
0
 def test_render_html_with_fragments(self):
     header = EmailKindFragment.objects.create(
         name='header',
         content='<title>Hello {{ name }}. {{ sentence }}</title>',
         default_context={'name': 'Yo'},
         is_plain=False)
     footer = EmailKindFragment.objects.create(
         name='footer',
         content='<div>{{ smiley }}</div>',
         default_context={'smiley': '^_^'},
         is_plain=False)
     ekind = EmailKind.objects.create(
         name='my-test-email',
         language='es',
         template=
         '{{ fragments.header }}<h1>Hola, {{ name }}</h1>{{ fragments.footer }}',
         plain_template='Hola, {{ name }}',
         default_context={'name': 'Manola'})
     ekind.fragments.add(header)
     ekind.fragments.add(footer)
     context = {'sentence': 'How are you?'}
     rendered = render_html(ekind, context)
     self.assertEqual(
         '<title>Hello Manola. How are you?</title><h1>Hola, Manola</h1><div>^_^</div>',
         rendered)
     rendered_plain = render_plain(ekind, context)
     self.assertEqual(rendered_plain, 'Hola, Manola')
Beispiel #4
0
 def test_render_html_not_enough_context(self):
     ekind = EmailKind.objects.create(
         name='my-test-email',
         language='es',
         template='<h1>Hola, {{ first_name }}</h1>',
         plain_template='Hola, {{ first_name }}',
     )
     rendered = render_html(ekind, {})
     self.assertEqual('<h1>Hola, </h1>', rendered)
Beispiel #5
0
 def test_render_html_default_context_override(self):
     ekind = EmailKind.objects.create(
         name='my-test-email',
         language='es',
         template='<h1>Hola, {{ first_name }}</h1>',
         plain_template='Hola, {{ first_name }}',
         default_context={'first_name': 'Luisa'})
     rendered = render_html(ekind, {'first_name': 'Matias'})
     self.assertEqual('<h1>Hola, Matias</h1>', rendered)
Beispiel #6
0
 def test_render_html_no_context_needed(self):
     ekind = EmailKind.objects.create(
         name='my-test-email',
         language='es',
         template='<h1>Hola, Luisa</h1>',
         plain_template='Hola, Luisa',
     )
     rendered = render_html(ekind, {})
     self.assertEqual('<h1>Hola, Luisa</h1>', rendered)
Beispiel #7
0
    def test_render_active_html_minification(self):
        ekind = EmailKind.objects.create(
            name='my-test-email',
            language='es',
            template='<h1>Hola, {{ first_name }}</h1>',
            plain_template='Hola, {{ first_name }}',
        )

        with mock.patch('emails.render._minify_html') as _minify_html:
            rendered = render_html(ekind, {'first_name': 'Luisa'})
            _minify_html.assert_any_call(mock.ANY)
            self.assertEqual(rendered, _minify_html.return_value)
            _minify_html.reset()

        with mock.patch('emails.render._minify_html') as _minify_html:
            rendered = render_html(ekind, {'first_name': 'Luisa'},
                                   ignore_minify=True)
            self.assertFalse(_minify_html.called)
            self.assertNotEqual(rendered, _minify_html.return_value)
Beispiel #8
0
 def test_render_html_fragment(self):
     fkind = EmailKindFragment.objects.create(
         name='throne-fragment',
         content='<h1>I am {{ name }} and I rule {{ kingdom }}</h1>',
         default_context={'name': 'Cersei Lannister'},
         is_plain=False)
     context = {'kingdom': 'Kingslanding'}
     rendered = render_html(fkind, context)
     self.assertEqual(
         '<h1>I am Cersei Lannister and I rule Kingslanding</h1>', rendered)
Beispiel #9
0
def send(emailentry):
    """
    Sends the email entry through the configured email backend.
    @type emailentry: EmailEntry
    """
    plain_body = render_plain(emailentry.kind, emailentry.context)
    rich_body = render_html(emailentry.kind, emailentry.context)

    email = EmailMultiAlternatives(subject=emailentry.subject,
                                   body=plain_body,
                                   to=emailentry.recipients.split(','),
                                   from_email=emailentry.sender,
                                   reply_to=emailentry.reply_to.split(','))
    email.encoding = 'utf-8'

    # Attach HTML alternative
    if rich_body:
        email.attach_alternative(rich_body, 'text/html')

    # Attach already embedded images. Only the actually used ones.
    for img in emailentry.kind.iter_all_images():
        content_id = img.content_id[1:-1]
        if content_id in rich_body:
            image = MIMEImage(img.image.read())
            image.add_header('Content-ID', content_id)
            email.attach(image)

    # Attach regular files
    for att in emailentry.attachments.all():
        email.attach(filename=att.name,
                     content=att.attached_file.read(),
                     mimetype=att.content_type)
        increment(settings.METRIC['SEND_ATTACHS'])

    # Attach fixed metadata and extras provided by the entry
    email.metadata = {
        'customer_id': emailentry.customer_id,
        'kind': str(emailentry.kind),
        'email_id': emailentry.id
    }
    if len(emailentry.metadata) > 0:
        email.metadata.update(emailentry.metadata)

    sent = send_with_backend(email, emailentry)
    if not sent:
        return None
    return email
Beispiel #10
0
    def test_render_plain_with_fragments(self):
        fragment = EmailKindFragment.objects.create(
            name='question',
            content='What is {{ who }} name?',
            default_context={'who': 'the girl'},
            is_plain=True)
        ekind = EmailKind.objects.create(
            name='test-email',
            language='es',
            template='<div>{{ answer }}</div>',
            plain_template='{{ fragments.question }} {{ answer }}',
            default_context={'answer': 'Arya Stark'})
        context = {'answer': 'The girl is no one'}
        ekind.fragments.add(fragment)
        rendered = render_plain(ekind, context)
        self.assertEqual('What is the girl name? The girl is no one', rendered)

        rendered_html = render_html(ekind, context)
        self.assertEqual('<div>The girl is no one</div>', rendered_html)
Beispiel #11
0
 def test_render_html_with_embedded_images_testing_true(self):
     ekind = EmailKind.objects.create(
         name='my-test-email',
         language='es',
         template=('<h1>Hola, {{ first_name }}</h1>'
                   '<img src="cid:logo-solocal">'
                   '<img src="cid:logo-qdq">'),
         plain_template='Hola, {{ first_name }}',
         default_context={'first_name': 'Luisa'})
     upload_image1 = create_upload_image()
     upload_image2 = create_upload_image()
     image1 = EmbeddedImage.objects.create(kind=ekind,
                                           placeholder_name='logo-solocal',
                                           image=upload_image1)
     image2 = EmbeddedImage.objects.create(kind=ekind,
                                           placeholder_name='logo-qdq',
                                           image=upload_image2)
     rendered = render_html(ekind, {}, test=True)
     self.assertIn(image1.image.url, rendered)
     self.assertIn(image2.image.url, rendered)