Projet

Général

Profil

0001-announces-send-command-10805.patch

Serghei Mihai, 27 juin 2016 15:30

Télécharger (5,99 ko)

Voir les différences:

Subject: [PATCH] announces send command (#10805)

 corbo/models.py        | 38 ++++++++++++++++++++++++++++++++
 corbo/settings.py      |  3 +++
 requirements.txt       |  3 +++
 setup.py               |  5 ++++-
 tests/test_emailing.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 tests/test_emailing.py
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
            sent = m.send(to=s.identifier)
93
            if sent:
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.',
98
                               self.announce.title, s.identifier)
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

  
107 110
# django-mellon settings
108 111
MELLON_ATTRIBUTE_MAPPING = {
109 112
    '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={
tests/test_emailing.py
1
import pytest
2
import json
3
from uuid import uuid4
4

  
5

  
6
from django.core.urlresolvers import reverse
7
from django.utils.http import urlencode
8
from django.core import mail
9
from django.utils import timezone
10

  
11
from corbo.models import Category, Announce, Subscription, Broadcast
12
from corbo.models import channel_choices
13

  
14
pytestmark = pytest.mark.django_db
15

  
16
CATEGORIES = ('Alerts', 'News')
17

  
18

  
19
@pytest.fixture
20
def categories():
21
    categories = []
22
    for category in CATEGORIES:
23
        c, created = Category.objects.get_or_create(name=category)
24
        categories.append(c)
25
    return categories
26

  
27
@pytest.fixture
28
def announces():
29
    announces = []
30
    for category in Category.objects.all():
31
        a = Announce.objects.create(category=category, title='Announce 1',
32
                                    publication_time=timezone.now(),
33
                                    text='<h2>Announce 1</h2>')
34
        Broadcast.objects.create(announce=a)
35
        announces.append(a)
36
        a = Announce.objects.create(category=category, title='Announce 2',
37
                                    publication_time=timezone.now(),
38
                                    text='Announce 2')
39
        Broadcast.objects.create(announce=a)
40
        announces.append(a)
41
    return announces
42

  
43
def test_emailing_with_no_subscriptions(app, categories, announces):
44
    for announce in announces:
45
        broadcast = Broadcast.objects.get(announce=announce)
46
        broadcast.send()
47
        assert not broadcast.result
48
    assert not mail.outbox
49

  
50
def test_send_email(app, categories, announces):
51
    for category in categories:
52
        uuid = uuid4()
53
        s = Subscription.objects.create(category=category,
54
                    identifier='%s@example.net' % uuid, uuid=uuid)
55
    for announce in announces:
56
        broadcast= Broadcast.objects.get(announce=announce)
57
        broadcast.send()
58
        assert broadcast.result
59
        assert mail.outbox
0
-