Projet

Général

Profil

Télécharger (5,34 ko) Statistiques
| Branche: | Tag: | Révision:

root / auquotidien / modules / announces.py @ 8b02623d

1
import time
2

    
3
from quixote import get_publisher
4

    
5
from quixote.html import htmlescape
6

    
7
from qommon import _
8
from qommon.storage import StorableObject
9
from qommon import get_cfg, get_logger
10
from qommon import errors
11
from qommon import misc
12

    
13
from qommon import emails
14
from qommon.sms import SMS
15
from qommon.admin.emails import EmailsDirectory
16

    
17
class AnnounceSubscription(StorableObject):
18
    _names = 'announce-subscriptions'
19
    _indexes = ['user_id']
20

    
21
    user_id = None
22
    email = None
23
    sms = None
24
    enabled = True
25
    enabled_sms = False
26
    enabled_themes = None
27

    
28
    def remove(self, type=None):
29
        """ type (string) : email or sms """
30
        if type == "email":
31
            self.email = None
32
        elif type == "sms":
33
            self.sms = None
34
            self.enabled_sms = False
35
        if not type or (not self.sms and not self.email):
36
            self.remove_self()
37
        else:
38
            self.store()
39
        
40
    def get_user(self):
41
        if self.user_id:
42
            try:
43
                return get_publisher().user_class.get(self.user_id)
44
            except KeyError:
45
                return None
46
        return None
47
    user = property(get_user)
48

    
49

    
50
class Announce(StorableObject):
51
    _names = 'announces'
52

    
53
    title = None
54
    text = None
55

    
56
    hidden = False
57

    
58
    publication_time = None
59
    modification_time = None
60
    expiration_time = None
61
    sent_by_email_time = None
62
    sent_by_sms_time = None
63
    theme = None
64

    
65
    position = None
66

    
67
    def sort_by_position(cls, links):
68
        def cmp_position(x, y):
69
            if x.position == y.position:
70
                return 0
71
            if x.position is None:
72
                return 1
73
            if y.position is None:
74
                return -1
75
            return cmp(x.position, y.position)
76
        links.sort(cmp_position)
77
    sort_by_position = classmethod(sort_by_position)
78

    
79
    def get_atom_entry(self):
80
        from pyatom import pyatom
81
        entry = pyatom.Entry()
82
        entry.id = self.get_url()
83
        entry.title = self.title
84

    
85
        entry.content.attrs['type'] = 'html'
86
        entry.content.text = str('<p>' + htmlescape(
87
                    unicode(self.text, get_publisher().site_charset).encode('utf-8')) + '</p>')
88

    
89
        link = pyatom.Link(self.get_url())
90
        entry.links.append(link)
91

    
92
        if self.publication_time:
93
            entry.published = misc.format_time(self.publication_time,
94
                    '%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
95
                    gmtime = True)
96

    
97
        if self.modification_time:
98
            entry.updated = misc.format_time(self.modification_time,
99
                    '%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
100
                    gmtime = True)
101

    
102
        return entry
103

    
104
    def get_url(self):
105
        return '%s/announces/%s/' % (get_publisher().get_frontoffice_url(), self.id)
106

    
107
    def store(self):
108
        self.modification_time = time.gmtime()
109
        StorableObject.store(self)
110

    
111
    def email(self, job=None):
112
        self.sent_by_email_time = time.gmtime()
113
        StorableObject.store(self)
114

    
115
        data = {
116
            'title': self.title,
117
            'text': self.text
118
        }
119

    
120
        subscribers = AnnounceSubscription.select(lambda x: x.enabled)
121

    
122
        rcpts = []
123
        for l in subscribers:
124
            if self.theme:
125
                if l.enabled_themes is not None:
126
                    if self.theme not in l.enabled_themes:
127
                        continue
128
            if l.user and l.user.email:
129
                rcpts.append(l.user.email)
130
            elif l.email:
131
                rcpts.append(l.email)
132

    
133
        emails.custom_ezt_email('aq-announce', data, email_rcpt = rcpts, hide_recipients = True)
134

    
135
    def sms(self, job=None):
136
        self.sent_by_sms_time = time.gmtime()
137
        StorableObject.store(self)
138

    
139
        subscribers = AnnounceSubscription.select(lambda x: x.enabled_sms)
140

    
141
        rcpts = []
142
        for sub in subscribers:
143
            if self.theme:
144
                if sub.enabled_themes is not None:
145
                    if self.theme not in sub.enabled_themes:
146
                        continue
147
            if sub.sms:
148
                rcpts.append(sub.sms)
149

    
150
        sms_cfg = get_cfg('sms', {})
151
        sender = sms_cfg.get('sender', 'AuQuotidien')[:11]
152
        message = "%s: %s" % (self.title, self.text)
153
        mode = sms_cfg.get('mode', 'none')
154
        sms = SMS.get_sms_class(mode)
155
        try:
156
            sms.send(sender, rcpts, message[:160])
157
        except errors.SMSError, e:
158
            get_logger().error(e)
159

    
160
    def get_published_announces(cls):
161
        announces = cls.select(lambda x: not x.hidden)
162
        announces.sort(lambda x,y: cmp(x.publication_time or x.modification_time,
163
                    y.publication_time or y.modification_time))
164
        announces = [x for x in announces if x.publication_time < time.gmtime()
165
                     and (x.expiration_time is None or x.expiration_time > time.gmtime())]
166
        announces.reverse()
167
        return announces
168
    get_published_announces = classmethod(get_published_announces)
169

    
170

    
171
EmailsDirectory.register('aq-announce',
172
        N_('Publication of announce to subscriber'),
173
        N_('Available variables: title, text'),
174
        default_subject = N_('Announce: [title]'),
175
        default_body = N_("""\
176
[text]
177

    
178
-- 
179
This is an announce sent to you by your city, you can opt to not receive
180
those messages anymore on the city website.
181
"""))
182

    
(9-9/27)