Projet

Général

Profil

0001-announces-send-command-10805.patch

Serghei Mihai, 23 juin 2016 10:38

Télécharger (3,89 ko)

Voir les différences:

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(-)
corbo/models.py
1
from datetime import datetime
2
import logging
3
from html2text import HTML2Text
4
from emails.django import Message
5
from lxml.etree import HTML as HTMLTree
6

  
1 7
from django.utils import timezone
2 8
from django.conf import settings
3 9
from django.db import models
10
from django.core.files.storage import DefaultStorage
4 11
from django.utils.translation import ugettext_lazy as _
5 12

  
6 13
from ckeditor.fields import RichTextField
......
10 17
    ('homepage', _('Homepage'))
11 18
)
12 19

  
20
logger = logging.getLogger(__name__)
21

  
13 22
class Category(models.Model):
14 23
    name = models.CharField(max_length=64, blank=False, null=False)
15 24
    ctime = models.DateTimeField(auto_now_add=True)
......
62 71
                id=self.announce.id, time=self.deliver_time)
63 72
        return u'announce {id} to deliver'.format(id=self.announce.id)
64 73

  
74
    def send(self):
75
        subscriptions = self.announce.category.subscription_set.all()
76
        total_sent = 0
77
        handler = HTML2Text()
78
        m = Message(html=self.announce.text, subject=self.announce.title,
79
                    text=handler.handle(self.announce.text),
80
                    mail_from=settings.DEFAULT_FROM_EMAIL)
81
        html_tree = HTMLTree(self.announce.text)
82
        storage = DefaultStorage()
83
        for img in html_tree.xpath('//img/@src'):
84
            img_path = img.lstrip(settings.MEDIA_URL)
85
            m.attach(filename=img, data=storage.open(img_path))
86
            m.attachments[img].is_inline = True
87
        m.transformer.synchronize_inline_images()
88
        m.transformer.save()
89
        for s in subscriptions:
90
            if not s.identifier:
91
                continue
92
            result = m.send(to=s.identifier)
93
            if result.status_code == 250:
94
                total_sent += 1
95
                logger.info('Announce "%s" sent to %s', self.announce.title, s.identifier)
96
            else:
97
                logger.warning('Error occured while sending announce "%s" to %s: status code %s',
98
                               self.announce.title, s.identifier, result.status_code)
99
        self.result = total_sent
100
        self.deliver_time = timezone.now()
101
        self.save()
102

  
65 103
    class Meta:
66 104
        verbose_name = _('sent')
67 105
        ordering = ('-deliver_time',)
corbo/settings.py
104 104
RSS_LINK = ''
105 105
RSS_LINK_TEMPLATE = '/#announce{0}'
106 106

  
107
# default mass emails expeditor
108
CORBO_DEFAULT_FROM_EMAIL = 'webmaster@localhost'
109

  
110
# media storage directory
111
SITE_MEDIA_ROOT = BASE_DIR
112

  
107 113
# django-mellon settings
108 114
MELLON_ATTRIBUTE_MAPPING = {
109 115
    'username': '{attributes[username][0]}',
requirements.txt
1 1
Django>=1.7, <1.8
2 2
django-ckeditor<4.5.3
3 3
djangorestframework
4
html2text
5
emails
4 6
-e git+http://repos.entrouvert.org/gadjo.git/#egg=gadjo
7

  
setup.py
96 96
    install_requires=['django>=1.7, <1.8',
97 97
        'django-ckeditor<4.5.3',
98 98
        'djangorestframework',
99
        'gadjo'
99
        'html2text',
100
        'gadjo',
101
        'emails',
102
        'lxml',
100 103
        ],
101 104
    zip_safe=False,
102 105
    cmdclass={
103
-