Projet

Général

Profil

« Précédent | Suivant » 

Révision 9841eeb4

Ajouté par Serghei Mihai (congés, retour 15/05) il y a plus de 7 ans

compute email message body once (#13552)

Voir les différences:

corbo/models.py
23 23
    ('homepage', _('Homepage'))
24 24
)
25 25

  
26
UNSUBSCRIBE_LINK_PLACEHOLDER = '##UNSUBSCRIBE_LINK_PLACEHOLDER##'
27

  
26 28
logger = logging.getLogger(__name__)
27 29

  
28 30

  
......
96 98
        total_sent = 0
97 99
        handler = HTML2Text()
98 100
        template = loader.get_template('corbo/announce.html')
99
        m = Message(subject=self.announce.title, mail_from=settings.CORBO_DEFAULT_FROM_EMAIL)
101
        message = Message(subject=self.announce.title, mail_from=settings.CORBO_DEFAULT_FROM_EMAIL,
102
                    html=template.render(Context({'content': self.announce.text,
103
                    'unsubscribe_link_placeholder': UNSUBSCRIBE_LINK_PLACEHOLDER})))
100 104
        html_tree = HTMLTree(self.announce.text)
101 105
        storage = DefaultStorage()
102 106
        for img in html_tree.xpath('//img/@src'):
103 107
            img_path = img.lstrip(storage.base_url)
104
            m.attach(filename=transform_image_src(img), data=storage.open(img_path))
108
            message.attach(filename=transform_image_src(img), data=storage.open(img_path))
109

  
110
        message.transformer.apply_to_images(func=transform_image_src)
111
        # perform transformations in message html, like inline css parsing
112
        message.transformer.load_and_transform()
113
        # mark all attached images as inline
114
        message.transformer.make_all_images_inline()
115
        message.transformer.save()
105 116
        for s in subscriptions:
106 117
            if not s.identifier:
107 118
                continue
......
109 120
                                               'identifier': s.identifier})
110 121
            unsubscribe_link = urlparse.urljoin(settings.SITE_BASE_URL, reverse('unsubscribe',
111 122
                                    kwargs={'unsubscription_token': unsubscribe_token}))
112
            message = template.render(Context({'unsubscribe_link': unsubscribe_link,
113
                                               'content': self.announce.text}))
114
            m.html = message
115
            # transform all images sources in order to use image names only
116
            m.transformer.apply_to_images(func=transform_image_src)
117
            # perform transformations in message html, like inline css parsing
118
            m.transformer.load_and_transform(load_images=False)
119
            # mark all attached images as inline
120
            m.transformer.make_all_images_inline()
121
            m.transformer.save()
123
            message.html = message.html.replace(UNSUBSCRIBE_LINK_PLACEHOLDER, unsubscribe_link)
122 124
            handler.body_width = 0
123
            m.text = handler.handle(m.html)
124
            sent = m.send(to=s.identifier)
125
            message.text = handler.handle(message.html)
126
            sent = message.send(to=s.identifier)
125 127
            if sent:
126 128
                total_sent += 1
127 129
                logger.info('Announce "%s" sent to %s', self.announce.title, s.identifier)
corbo/templates/corbo/announce.html
4 4
</div>
5 5
<div class="footer">
6 6
  {% blocktrans %}
7
  Click <a href='{{ unsubscribe_link}}'>here</a> to unsubscribe from this newsletter.
7
  Click <a href='{{ unsubscribe_link_placeholder }}'>here</a> to unsubscribe from this newsletter.
8 8
  {% endblocktrans %}
9 9
</div>
tests/test_emailing.py
102 102
    storage.delete(image_name)
103 103

  
104 104
def test_unsubscription_link(app, categories, announces):
105
    unsubscription_link_sentinel = ''
105 106
    for category in categories:
106 107
        uuid = uuid4()
107 108
        scheme = 'mailto:'
......
124 125
            assert mail.outbox[0].subject == announce.title
125 126
            assert unsubscription_link in mail.outbox[0].html
126 127
            assert unsubscription_link in mail.outbox[0].text
128
            assert unsubscription_link_sentinel != unsubscription_link
127 129
            mail.outbox = []
128 130
            # make sure the uri schema is not in the page
129 131
            resp = app.get(unsubscription_link)
130 132
            assert scheme not in resp.content
133
        unsubscription_link_sentinel = unsubscription_link

Formats disponibles : Unified diff