From 9841eeb472deae48c898b3462629bc208435844a Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Tue, 4 Oct 2016 17:03:28 +0200 Subject: [PATCH] compute email message body once (#13552) --- corbo/models.py | 30 ++++++++++++++++-------------- corbo/templates/corbo/announce.html | 2 +- tests/test_emailing.py | 3 +++ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/corbo/models.py b/corbo/models.py index 6b1f7cb..6ece581 100644 --- a/corbo/models.py +++ b/corbo/models.py @@ -23,6 +23,8 @@ channel_choices = ( ('homepage', _('Homepage')) ) +UNSUBSCRIBE_LINK_PLACEHOLDER = '##UNSUBSCRIBE_LINK_PLACEHOLDER##' + logger = logging.getLogger(__name__) @@ -96,12 +98,21 @@ class Broadcast(models.Model): total_sent = 0 handler = HTML2Text() template = loader.get_template('corbo/announce.html') - m = Message(subject=self.announce.title, mail_from=settings.CORBO_DEFAULT_FROM_EMAIL) + message = Message(subject=self.announce.title, mail_from=settings.CORBO_DEFAULT_FROM_EMAIL, + html=template.render(Context({'content': self.announce.text, + 'unsubscribe_link_placeholder': UNSUBSCRIBE_LINK_PLACEHOLDER}))) html_tree = HTMLTree(self.announce.text) storage = DefaultStorage() for img in html_tree.xpath('//img/@src'): img_path = img.lstrip(storage.base_url) - m.attach(filename=transform_image_src(img), data=storage.open(img_path)) + message.attach(filename=transform_image_src(img), data=storage.open(img_path)) + + message.transformer.apply_to_images(func=transform_image_src) + # perform transformations in message html, like inline css parsing + message.transformer.load_and_transform() + # mark all attached images as inline + message.transformer.make_all_images_inline() + message.transformer.save() for s in subscriptions: if not s.identifier: continue @@ -109,19 +120,10 @@ class Broadcast(models.Model): 'identifier': s.identifier}) unsubscribe_link = urlparse.urljoin(settings.SITE_BASE_URL, reverse('unsubscribe', kwargs={'unsubscription_token': unsubscribe_token})) - message = template.render(Context({'unsubscribe_link': unsubscribe_link, - 'content': self.announce.text})) - m.html = message - # transform all images sources in order to use image names only - m.transformer.apply_to_images(func=transform_image_src) - # perform transformations in message html, like inline css parsing - m.transformer.load_and_transform(load_images=False) - # mark all attached images as inline - m.transformer.make_all_images_inline() - m.transformer.save() + message.html = message.html.replace(UNSUBSCRIBE_LINK_PLACEHOLDER, unsubscribe_link) handler.body_width = 0 - m.text = handler.handle(m.html) - sent = m.send(to=s.identifier) + message.text = handler.handle(message.html) + sent = message.send(to=s.identifier) if sent: total_sent += 1 logger.info('Announce "%s" sent to %s', self.announce.title, s.identifier) diff --git a/corbo/templates/corbo/announce.html b/corbo/templates/corbo/announce.html index 39592e4..ae6a6e2 100644 --- a/corbo/templates/corbo/announce.html +++ b/corbo/templates/corbo/announce.html @@ -4,6 +4,6 @@ diff --git a/tests/test_emailing.py b/tests/test_emailing.py index 6070ee0..9f367ea 100644 --- a/tests/test_emailing.py +++ b/tests/test_emailing.py @@ -102,6 +102,7 @@ def test_check_inline_images(app, categories, announces): storage.delete(image_name) def test_unsubscription_link(app, categories, announces): + unsubscription_link_sentinel = '' for category in categories: uuid = uuid4() scheme = 'mailto:' @@ -124,7 +125,9 @@ def test_unsubscription_link(app, categories, announces): assert mail.outbox[0].subject == announce.title assert unsubscription_link in mail.outbox[0].html assert unsubscription_link in mail.outbox[0].text + assert unsubscription_link_sentinel != unsubscription_link mail.outbox = [] # make sure the uri schema is not in the page resp = app.get(unsubscription_link) assert scheme not in resp.content + unsubscription_link_sentinel = unsubscription_link -- 2.9.3