Project

General

Profile

Download (5.31 KB) Statistics
| Branch: | Tag: | Revision:

root / extra / modules / announces.py @ 2750dbae

1
import time
2

    
3
from quixote import get_publisher
4

    
5
from quixote.html import htmlescape
6

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

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

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

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

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

    
45

    
46
class Announce(StorableObject):
47
    _names = 'announces'
48

    
49
    title = None
50
    text = None
51

    
52
    hidden = False
53

    
54
    publication_time = None
55
    modification_time = None
56
    expiration_time = None
57
    sent_by_email_time = None
58
    sent_by_sms_time = None
59
    theme = None
60

    
61
    position = None
62

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

    
75
    def get_atom_entry(self):
76
        from pyatom import pyatom
77
        entry = pyatom.Entry()
78
        entry.id = self.get_url()
79
        entry.title = self.title
80

    
81
        entry.content.attrs['type'] = 'html'
82
        entry.content.text = str('<p>' + htmlescape(
83
                    unicode(self.text, get_publisher().site_charset).encode('utf-8')) + '</p>')
84

    
85
        link = pyatom.Link(self.get_url())
86
        entry.links.append(link)
87

    
88
        if self.publication_time:
89
            entry.published = misc.format_time(self.publication_time,
90
                    '%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
91
                    gmtime = True)
92

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

    
98
        return entry
99

    
100
    def get_url(self):
101
        from quixote import get_request
102
        return '%s://%s%sannounces/%s/' % (
103
                get_request().get_scheme(), get_request().get_server(),
104
                get_publisher().get_root_url(), self.id)
105

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

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

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

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

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

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

    
134
    def sms(self, job=None):
135
        sms = SMS()
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 l.enabled_themes is not None:
145
                    if self.theme not in l.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
        try:
154
            sms.send(rcpts, message[:160], sender)
155
        except errors.SMSError, e:
156
            get_logger().error(e)
157

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

    
168

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

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

    
(4-4/26)