From be3b63843f9238d2374c7127de7fb137f7b67935 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Fri, 6 May 2016 17:21:56 +0200 Subject: [PATCH] announces send command (#10805) --- corbo/models.py | 38 ++++++++++++++++++++++++++++++++++++++ corbo/settings.py | 6 ++++++ requirements.txt | 3 +++ setup.py | 5 ++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/corbo/models.py b/corbo/models.py index 3c22ec9..7edee72 100644 --- a/corbo/models.py +++ b/corbo/models.py @@ -1,6 +1,13 @@ +from datetime import datetime +import logging +from html2text import HTML2Text +from emails.django import Message +from lxml.etree import HTML as HTMLTree + 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 ckeditor.fields import RichTextField @@ -10,6 +17,8 @@ channel_choices = ( ('homepage', _('Homepage')) ) +logger = logging.getLogger(__name__) + class Category(models.Model): name = models.CharField(max_length=64, blank=False, null=False) ctime = models.DateTimeField(auto_now_add=True) @@ -62,6 +71,35 @@ class Broadcast(models.Model): id=self.announce.id, time=self.deliver_time) 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() + m = Message(html=self.announce.text, subject=self.announce.title, + text=handler.handle(self.announce.text), + mail_from=settings.DEFAULT_FROM_EMAIL) + html_tree = HTMLTree(self.announce.text) + storage = DefaultStorage() + for img in html_tree.xpath('//img/@src'): + img_path = img.lstrip(settings.MEDIA_URL) + m.attach(filename=img, data=storage.open(img_path)) + m.attachments[img].is_inline = True + m.transformer.synchronize_inline_images() + m.transformer.save() + for s in subscriptions: + if not s.identifier: + continue + result = m.send(to=s.identifier) + if result.status_code == 250: + 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: status code %s', + self.announce.title, s.identifier, result.status_code) + self.result = total_sent + self.deliver_time = timezone.now() + self.save() + class Meta: verbose_name = _('sent') ordering = ('-deliver_time',) diff --git a/corbo/settings.py b/corbo/settings.py index 654c98c..d6c6419 100644 --- a/corbo/settings.py +++ b/corbo/settings.py @@ -104,6 +104,12 @@ RSS_DESCRIPTION = '' RSS_LINK = '' RSS_LINK_TEMPLATE = '/#announce{0}' +# default mass emails expeditor +CORBO_DEFAULT_FROM_EMAIL = 'webmaster@localhost' + +# media storage directory +SITE_MEDIA_ROOT = BASE_DIR + # django-mellon settings MELLON_ATTRIBUTE_MAPPING = { 'username': '{attributes[username][0]}', diff --git a/requirements.txt b/requirements.txt index 2f6bdfc..2d0946c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ Django>=1.7, <1.8 django-ckeditor<4.5.3 djangorestframework +html2text +emails -e git+http://repos.entrouvert.org/gadjo.git/#egg=gadjo + diff --git a/setup.py b/setup.py index e59a798..2254dba 100644 --- a/setup.py +++ b/setup.py @@ -96,7 +96,10 @@ setup( install_requires=['django>=1.7, <1.8', 'django-ckeditor<4.5.3', 'djangorestframework', - 'gadjo' + 'html2text', + 'gadjo', + 'emails', + 'lxml', ], zip_safe=False, cmdclass={ -- 2.8.1