|
1 |
from datetime import datetime
|
|
2 |
import logging
|
|
3 |
from html2text import HTML2Text
|
|
4 |
|
1 |
5 |
from django.utils import timezone
|
2 |
6 |
from django.conf import settings
|
3 |
7 |
from django.db import models
|
|
8 |
from django.core.mail import send_mail
|
4 |
9 |
from django.utils.translation import ugettext_lazy as _
|
5 |
10 |
|
6 |
11 |
from ckeditor.fields import RichTextField
|
... | ... | |
10 |
15 |
('homepage', _('Homepage'))
|
11 |
16 |
)
|
12 |
17 |
|
|
18 |
logger = logging.getLogger(__name__)
|
|
19 |
|
13 |
20 |
class Category(models.Model):
|
14 |
21 |
name = models.CharField(max_length=64, blank=False, null=False)
|
15 |
22 |
ctime = models.DateTimeField(auto_now_add=True)
|
... | ... | |
62 |
69 |
id=self.announce.id, time=self.deliver_time)
|
63 |
70 |
return u'announce {id} to deliver'.format(id=self.announce.id)
|
64 |
71 |
|
|
72 |
def send(self):
|
|
73 |
subscriptions = self.announce.category.subscription_set.all()
|
|
74 |
total_sent = 0
|
|
75 |
destinations = [s.identifier for s in subscriptions]
|
|
76 |
handler = HTML2Text()
|
|
77 |
subject = self.announce.title
|
|
78 |
text_message = handler.handle(self.announce.text)
|
|
79 |
for destination in destinations:
|
|
80 |
if send_mail(subject, text_message,
|
|
81 |
settings.CORBO_DEFAULT_FROM_EMAIL,
|
|
82 |
[destination], html_message=self.announce.text):
|
|
83 |
total_sent += 1
|
|
84 |
logger.debug('announce "%s" delivered to %s destinations', subject, total_sent)
|
|
85 |
self.result = total_sent
|
|
86 |
self.deliver_time = timezone.now()
|
|
87 |
self.save()
|
|
88 |
|
65 |
89 |
class Meta:
|
66 |
90 |
verbose_name = _('sent')
|
67 |
91 |
ordering = ('-deliver_time',)
|