Project

General

Profile

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

root / extra / modules / announces.py @ e3bbea04

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

    
19
    user_id = None
20
    email = None
21
    sms = None
22
    enabled = True
23
    enabled_sms = False
24

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

    
43

    
44
class Announce(StorableObject):
45
    _names = 'announces'
46

    
47
    title = None
48
    text = None
49

    
50
    hidden = False
51

    
52
    publication_time = None
53
    modification_time = None
54
    sent_by_email_time = None
55
    sent_by_sms_time = None
56

    
57
    position = None
58

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

    
71
    def get_atom_entry(self):
72
        from pyatom import pyatom
73
        entry = pyatom.Entry()
74
        entry.id = self.get_url()
75
        entry.title = self.title
76

    
77
        entry.content.attrs['type'] = 'html'
78
        entry.content.text = str('<p>' + htmlescape(
79
                    unicode(self.text, 'iso-8859-1').encode('utf-8')) + '</p>')
80

    
81
        link = pyatom.Link(self.get_url())
82
        entry.links.append(link)
83

    
84
        if self.publication_time:
85
            entry.published = misc.format_time(self.publication_time,
86
                    '%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
87
                    gmtime = True)
88

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

    
94
        return entry
95

    
96
    def get_url(self):
97
        from quixote import get_request
98
        return '%s://%s%sannounces/%s/' % (
99
                get_request().get_scheme(), get_request().get_server(),
100
                get_publisher().get_root_url(), self.id)
101

    
102
    def store(self):
103
        self.modification_time = time.gmtime()
104
        StorableObject.store(self)
105

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

    
110
        data = {
111
            'title': self.title,
112
            'text': self.text
113
        }
114

    
115
        subscribers = AnnounceSubscription.select(lambda x: x.enabled)
116

    
117
        rcpts = []
118
        for l in subscribers:
119
            if l.user_id and l.user.email:
120
                rcpts.append(l.user.email)
121
            elif l.email:
122
                rcpts.append(l.email)
123

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

    
126
    def sms(self):
127
        sms = SMS()
128
        self.sent_by_sms_time = time.gmtime()
129
        StorableObject.store(self)
130

    
131
        subscribers = AnnounceSubscription.select(lambda x: x.enabled_sms)
132

    
133
        rcpts = []
134
        for sub in subscribers:
135
            if sub.sms:
136
                rcpts.append(sub.sms)
137

    
138
        sms_cfg = get_cfg('sms', {})
139
        sender = sms_cfg.get('sender', '')[:11]
140
        message = "%s: %s" % (self.title, self.text)
141
        try:
142
            sms.send(rcpts, message[:160], sender)
143
        except errors.SMSError, e:
144
            get_logger().error(e)
145

    
146
    def get_published_announces(cls):
147
        announces = cls.select(lambda x: not x.hidden)
148
        announces.sort(lambda x,y: cmp(x.publication_time or x.modification_time,
149
                    y.publication_time or y.modification_time))
150
        announces = [x for x in announces if x.publication_time < time.gmtime()]
151
        announces.reverse()
152
        return announces
153
    get_published_announces = classmethod(get_published_announces)
154

    
155

    
156
EmailsDirectory.register('aq-announce',
157
        N_('Publication of announce to subscriber'),
158
        N_('Available variables: title, text'),
159
        default_subject = N_('Announce: [title]'),
160
        default_body = N_("""\
161
[text]
162

    
163
-- 
164
This is an announce sent to you by your city, you can opt to not receive
165
those messages anymore on the city website.
166
"""))
167

    
(4-4/15)