Project

General

Profile

Download (2.9 KB) Statistics
| Branch: | Tag: | Revision:

root / corbo / utils.py @ f46f80e4

1
# corbo - Announces Manager
2
# Copyright (C) 2017 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

    
17
import os
18
import logging
19
import urlparse
20
import hashlib
21
from html2text import HTML2Text
22
from emails.django import Message
23
from lxml import etree
24

    
25
from django.conf import settings
26
from django.template import loader, Context
27
from django.utils.translation import activate
28
from django.core.files.storage import DefaultStorage
29
from django.core.urlresolvers import reverse
30
from django.core import signing
31

    
32

    
33
UNSUBSCRIBE_LINK_PLACEHOLDER = '%%UNSUBSCRIBE_LINK_PLACEHOLDER%%'
34

    
35

    
36
def transform_image_src(src, **kwargs):
37
    return urlparse.urljoin(settings.SITE_BASE_URL, src)
38

    
39
def send_email(title, content, destinations, category_id):
40
    logger = logging.getLogger(__name__)
41
    total_sent = 0
42
    handler = HTML2Text()
43
    activate(settings.LANGUAGE_CODE)
44
    template = loader.get_template('corbo/announce.html')
45
    message = Message(subject=title, mail_from=settings.DEFAULT_FROM_EMAIL,
46
            html=template.render(
47
            Context({'content': content,
48
            'unsubscribe_link_placeholder': UNSUBSCRIBE_LINK_PLACEHOLDER})))
49

    
50
    # perform transformations in message html, like inline css parsing
51
    message.transformer.apply_to_images(func=transform_image_src)
52
    message.transformer.load_and_transform(images_inline=True)
53
    message.transformer.save()
54
    orig_html = message.html
55
    handler.body_width = 0
56
    orig_text = handler.handle(message.html)
57

    
58
    for dest in destinations:
59
        unsubscribe_token = signing.dumps({'category': category_id,
60
                                           'identifier': dest})
61
        unsubscribe_link = urlparse.urljoin(settings.SITE_BASE_URL, reverse(
62
            'unsubscribe', kwargs={'unsubscription_token': unsubscribe_token}))
63
        message.set_headers({'List-Unsubscribe': '<%s>' % unsubscribe_link})
64
        message.html = orig_html.replace(UNSUBSCRIBE_LINK_PLACEHOLDER, unsubscribe_link)
65
        message.text = orig_text.replace(UNSUBSCRIBE_LINK_PLACEHOLDER, unsubscribe_link)
66

    
67
        sent = message.send(to=dest)
68
        if sent:
69
            total_sent += 1
70
            logger.info('Announce "%s" sent to %s', title, dest)
71
        else:
72
            logger.warning('Error occured while sending announce "%s" to %s.',
73
                           title, dest)
74
    return total_sent
(10-10/13)