Revision ecf2d157
Added by Serghei Mihai over 8 years ago
corbo/models.py | ||
---|---|---|
1 |
import os |
|
2 |
import hashlib |
|
1 | 3 |
from datetime import datetime |
2 | 4 |
import logging |
3 | 5 |
import urlparse |
... | ... | |
23 | 25 |
|
24 | 26 |
logger = logging.getLogger(__name__) |
25 | 27 |
|
28 |
|
|
29 |
def transform_image_src(src, **kwargs): |
|
30 |
basename = os.path.basename(src) |
|
31 |
if basename == src: |
|
32 |
return src |
|
33 |
name, ext = os.path.splitext(src) |
|
34 |
hash = hashlib.sha256(name) |
|
35 |
return '%s_%s%s' % (os.path.basename(name), hash.hexdigest()[:8], ext) |
|
36 |
|
|
37 |
|
|
26 | 38 |
class Category(models.Model): |
27 | 39 |
name = models.CharField(max_length=64, blank=False, null=False) |
28 | 40 |
ctime = models.DateTimeField(auto_now_add=True) |
... | ... | |
89 | 101 |
storage = DefaultStorage() |
90 | 102 |
for img in html_tree.xpath('//img/@src'): |
91 | 103 |
img_path = img.lstrip(storage.base_url) |
92 |
m.attach(filename=img, data=storage.open(img_path)) |
|
93 |
m.attachments[img].is_inline = True |
|
104 |
m.attach(filename=transform_image_src(img), data=storage.open(img_path)) |
|
94 | 105 |
for s in subscriptions: |
95 | 106 |
if not s.identifier: |
96 | 107 |
continue |
... | ... | |
101 | 112 |
message = template.render(Context({'unsubscribe_link': unsubscribe_link, |
102 | 113 |
'content': self.announce.text})) |
103 | 114 |
m.html = message |
104 |
m.transformer.load_and_transform() |
|
105 |
m.transformer.synchronize_inline_images() |
|
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() |
|
106 | 121 |
m.transformer.save() |
107 | 122 |
handler.body_width = 0 |
108 |
m.text = handler.handle(message)
|
|
123 |
m.text = handler.handle(m.html)
|
|
109 | 124 |
sent = m.send(to=s.identifier) |
110 | 125 |
if sent: |
111 | 126 |
total_sent += 1 |
tests/test_emailing.py | ||
---|---|---|
15 | 15 |
from django.conf import settings |
16 | 16 |
|
17 | 17 |
from corbo.models import Category, Announce, Subscription, Broadcast |
18 |
from corbo.models import channel_choices |
|
18 |
from corbo.models import channel_choices, transform_image_src
|
|
19 | 19 |
|
20 | 20 |
pytestmark = pytest.mark.django_db |
21 | 21 |
|
... | ... | |
82 | 82 |
storage = DefaultStorage() |
83 | 83 |
media_path = os.path.join(os.path.dirname(__file__), 'media') |
84 | 84 |
image_name = 'logo.png' |
85 |
storage.save(image_name, file(os.path.join(media_path, image_name))) |
|
85 |
image_name = storage.save(image_name, file(os.path.join(media_path, image_name)))
|
|
86 | 86 |
for announce in announces: |
87 |
announce.text = announce.text + '<img src="/media/%s" />' % image_name |
|
87 |
img_src = "/media/%s" % image_name |
|
88 |
announce.text = announce.text + '<img src="%s" />' % img_src |
|
88 | 89 |
announce.save() |
89 | 90 |
uuid = uuid4() |
90 | 91 |
s = Subscription.objects.create(category=announce.category, |
... | ... | |
93 | 94 |
broadcast.send() |
94 | 95 |
assert broadcast.result |
95 | 96 |
assert mail.outbox |
96 |
assert storage.url(image_name) in mail.outbox[0].attachments.keys() |
|
97 |
transformed_image_src = transform_image_src(img_src) |
|
98 |
assert transformed_image_src in mail.outbox[0].attachments.keys() |
|
99 |
assert 'cid:%s' % transformed_image_src in mail.outbox[0].html_body |
|
100 |
assert 'cid:%s' % transformed_image_src in mail.outbox[0].text_body |
|
97 | 101 |
mail.outbox = [] |
98 | 102 |
storage.delete(image_name) |
99 | 103 |
|
Also available in: Unified diff
include filename only for attached inline images (#12872)