move email sending logic in utils
This commit is contained in:
parent
2625291a6e
commit
a1df96238e
|
@ -3,38 +3,23 @@ import io
|
|||
import hashlib
|
||||
from time import mktime
|
||||
from datetime import datetime
|
||||
import logging
|
||||
import urlparse
|
||||
from html2text import HTML2Text
|
||||
from emails.django import Message
|
||||
from lxml import etree
|
||||
import requests
|
||||
import feedparser
|
||||
|
||||
from django.utils import timezone
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.core.files.storage import DefaultStorage
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core import signing
|
||||
from django.template import loader, Context
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import activate
|
||||
|
||||
from ckeditor.fields import RichTextField
|
||||
|
||||
from . import utils
|
||||
|
||||
channel_choices = (
|
||||
('mailto', _('Email')),
|
||||
)
|
||||
|
||||
UNSUBSCRIBE_LINK_PLACEHOLDER = '##UNSUBSCRIBE_LINK_PLACEHOLDER##'
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def transform_image_src(src, **kwargs):
|
||||
return urlparse.urljoin(settings.SITE_BASE_URL, src)
|
||||
|
||||
|
||||
class Category(models.Model):
|
||||
name = models.CharField(_('Name'), max_length=64, blank=False, null=False)
|
||||
|
@ -170,38 +155,8 @@ class Broadcast(models.Model):
|
|||
return u'announce {id} to deliver'.format(id=self.announce.id)
|
||||
|
||||
def send(self):
|
||||
subscriptions = self.announce.category.subscription_set.all()
|
||||
total_sent = 0
|
||||
handler = HTML2Text()
|
||||
activate(settings.LANGUAGE_CODE)
|
||||
template = loader.get_template('corbo/announce.html')
|
||||
message = Message(subject=self.announce.title, mail_from=settings.DEFAULT_FROM_EMAIL,
|
||||
html=template.render(
|
||||
Context({'content': self.announce.text,
|
||||
'unsubscribe_link_placeholder': UNSUBSCRIBE_LINK_PLACEHOLDER})))
|
||||
|
||||
# perform transformations in message html, like inline css parsing
|
||||
message.transformer.apply_to_images(func=transform_image_src)
|
||||
message.transformer.load_and_transform(images_inline=True)
|
||||
message.transformer.save()
|
||||
for s in subscriptions:
|
||||
if not s.identifier:
|
||||
continue
|
||||
unsubscribe_token = signing.dumps({'category': self.announce.category.pk,
|
||||
'identifier': s.identifier})
|
||||
unsubscribe_link = urlparse.urljoin(settings.SITE_BASE_URL, reverse(
|
||||
'unsubscribe', kwargs={'unsubscription_token': unsubscribe_token}))
|
||||
message.html = message.html.replace(UNSUBSCRIBE_LINK_PLACEHOLDER, unsubscribe_link)
|
||||
handler.body_width = 0
|
||||
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)
|
||||
else:
|
||||
logger.warning('Error occured while sending announce "%s" to %s.',
|
||||
self.announce.title, s.identifier)
|
||||
self.delivery_count = total_sent
|
||||
destinations = [s.identifier for s in self.announce.category.subscription_set.all() if s.identifier]
|
||||
self.delivery_count = utils.send_email(self.announce.title, self.announce.text, destinations, category_id=self.announce.category.pk)
|
||||
self.deliver_time = timezone.now()
|
||||
self.save()
|
||||
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
# corbo - Announces Manager
|
||||
# Copyright (C) 2017 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import logging
|
||||
import urlparse
|
||||
import hashlib
|
||||
from html2text import HTML2Text
|
||||
from emails.django import Message
|
||||
from lxml import etree
|
||||
|
||||
from django.conf import settings
|
||||
from django.template import loader, Context
|
||||
from django.utils.translation import activate
|
||||
from django.core.files.storage import DefaultStorage
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.core import signing
|
||||
|
||||
|
||||
UNSUBSCRIBE_LINK_PLACEHOLDER = '##UNSUBSCRIBE_LINK_PLACEHOLDER##'
|
||||
|
||||
|
||||
def transform_image_src(src, **kwargs):
|
||||
return urlparse.urljoin(settings.SITE_BASE_URL, src)
|
||||
|
||||
def send_email(title, content, destinations, category_id=None):
|
||||
logger = logging.getLogger(__name__)
|
||||
total_sent = 0
|
||||
handler = HTML2Text()
|
||||
activate(settings.LANGUAGE_CODE)
|
||||
template = loader.get_template('corbo/announce.html')
|
||||
message = Message(subject=title, mail_from=settings.DEFAULT_FROM_EMAIL,
|
||||
html=template.render(
|
||||
Context({'content': content,
|
||||
'unsubscribe_link_placeholder': UNSUBSCRIBE_LINK_PLACEHOLDER})))
|
||||
|
||||
# perform transformations in message html, like inline css parsing
|
||||
message.transformer.apply_to_images(func=transform_image_src)
|
||||
message.transformer.load_and_transform(images_inline=True)
|
||||
message.transformer.save()
|
||||
for dest in destinations:
|
||||
if category_id:
|
||||
unsubscribe_token = signing.dumps({'category': category_id,
|
||||
'identifier': dest})
|
||||
unsubscribe_link = urlparse.urljoin(settings.SITE_BASE_URL, reverse(
|
||||
'unsubscribe', kwargs={'unsubscription_token': unsubscribe_token}))
|
||||
message.html = message.html.replace(UNSUBSCRIBE_LINK_PLACEHOLDER, unsubscribe_link)
|
||||
handler.body_width = 0
|
||||
message.text = handler.handle(message.html)
|
||||
sent = message.send(to=dest)
|
||||
if sent:
|
||||
total_sent += 1
|
||||
logger.info('Announce "%s" sent to %s', title, dest)
|
||||
else:
|
||||
logger.warning('Error occured while sending announce "%s" to %s.',
|
||||
title, dest)
|
||||
return total_sent
|
Reference in New Issue