From d062e1e5d2e3660625f829f1ccea8623efacc089 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 | 24 ++++++++++++++++++++++++ corbo/settings.py | 3 +++ requirements.txt | 1 + setup.py | 1 + 4 files changed, 29 insertions(+) diff --git a/corbo/models.py b/corbo/models.py index 3c22ec9..47691a7 100644 --- a/corbo/models.py +++ b/corbo/models.py @@ -1,6 +1,11 @@ +from datetime import datetime +import logging +from html2text import HTML2Text + from django.utils import timezone from django.conf import settings from django.db import models +from django.core.mail import send_mail from django.utils.translation import ugettext_lazy as _ from ckeditor.fields import RichTextField @@ -10,6 +15,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 +69,23 @@ 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 + destinations = [s.identifier for s in subscriptions] + handler = HTML2Text() + subject = self.announce.title + text_message = handler.handle(self.announce.text) + for destination in destinations: + if send_mail(subject, text_message, + settings.CORBO_DEFAULT_FROM_EMAIL, + [destination], html_message=self.announce.text): + total_sent += 1 + logger.debug('announce "%s" delivered to %s destinations', subject, total_sent) + 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..a70dacc 100644 --- a/corbo/settings.py +++ b/corbo/settings.py @@ -104,6 +104,9 @@ RSS_DESCRIPTION = '' RSS_LINK = '' RSS_LINK_TEMPLATE = '/#announce{0}' +# default mass emails expeditor +CORBO_DEFAULT_FROM_EMAIL = 'webmaster@localhost' + # django-mellon settings MELLON_ATTRIBUTE_MAPPING = { 'username': '{attributes[username][0]}', diff --git a/requirements.txt b/requirements.txt index 2f6bdfc..0c9b472 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Django>=1.7, <1.8 django-ckeditor<4.5.3 djangorestframework +html2text -e git+http://repos.entrouvert.org/gadjo.git/#egg=gadjo diff --git a/setup.py b/setup.py index e59a798..b7e9c15 100644 --- a/setup.py +++ b/setup.py @@ -96,6 +96,7 @@ setup( install_requires=['django>=1.7, <1.8', 'django-ckeditor<4.5.3', 'djangorestframework', + 'html2text', 'gadjo' ], zip_safe=False, -- 2.8.1