Révision 6a3e75ef
Ajouté par Serghei Mihai (congés, retour 15/05) il y a presque 8 ans
corbo/models.py | ||
---|---|---|
9 | 9 |
from django.db import models |
10 | 10 |
from django.core.files.storage import DefaultStorage |
11 | 11 |
from django.utils.translation import ugettext_lazy as _ |
12 |
from django.core import signing |
|
13 |
from django.template import loader, Context |
|
14 |
from django.core.urlresolvers import reverse |
|
12 | 15 |
|
13 | 16 |
from ckeditor.fields import RichTextField |
14 | 17 |
|
... | ... | |
75 | 78 |
subscriptions = self.announce.category.subscription_set.all() |
76 | 79 |
total_sent = 0 |
77 | 80 |
handler = HTML2Text() |
78 |
m = Message(html=self.announce.text, subject=self.announce.title, |
|
79 |
text=handler.handle(self.announce.text), |
|
80 |
mail_from=settings.CORBO_DEFAULT_FROM_EMAIL) |
|
81 |
template = loader.get_template('corbo/announce.html') |
|
82 |
m = Message(subject=self.announce.title, mail_from=settings.CORBO_DEFAULT_FROM_EMAIL) |
|
81 | 83 |
html_tree = HTMLTree(self.announce.text) |
82 | 84 |
storage = DefaultStorage() |
83 | 85 |
for img in html_tree.xpath('//img/@src'): |
84 | 86 |
img_path = img.lstrip(storage.base_url) |
85 | 87 |
m.attach(filename=img, data=storage.open(img_path)) |
86 | 88 |
m.attachments[img].is_inline = True |
87 |
m.transformer.synchronize_inline_images() |
|
88 |
m.transformer.load_and_transform() |
|
89 |
m.transformer.save() |
|
90 | 89 |
for s in subscriptions: |
91 | 90 |
if not s.identifier: |
92 | 91 |
continue |
92 |
unsubscribe_token = signing.dumps({'category': self.announce.category.pk, |
|
93 |
'identifier': s.identifier}) |
|
94 |
unsubscribe_link = reverse('unsubscribe', kwargs={'unsubscription_token': unsubscribe_token}) |
|
95 |
message = template.render(Context({'unsubscribe_link': unsubscribe_link, |
|
96 |
'content': self.announce.text})) |
|
97 |
m.html = message |
|
98 |
m.transformer.load_and_transform() |
|
99 |
m.transformer.synchronize_inline_images() |
|
100 |
m.transformer.save() |
|
101 |
handler.body_width = 0 |
|
102 |
m.text = handler.handle(message) |
|
93 | 103 |
sent = m.send(to=s.identifier) |
94 | 104 |
if sent: |
95 | 105 |
total_sent += 1 |
Formats disponibles : Unified diff
unsubscribe link (#10795)