|
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 misc
|
|
9
|
|
|
10
|
from qommon import emails
|
|
11
|
from qommon.admin.emails import EmailsDirectory
|
|
12
|
|
|
13
|
class AnnounceSubscription(StorableObject):
|
|
14
|
_names = 'announce-subscriptions'
|
|
15
|
|
|
16
|
user_id = None
|
|
17
|
email = None
|
|
18
|
sms = None
|
|
19
|
enabled = True
|
|
20
|
|
|
21
|
def get_user(self):
|
|
22
|
if self.user_id:
|
|
23
|
return get_publisher().user_class.get(self.user_id)
|
|
24
|
return None
|
|
25
|
user = property(get_user)
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
class Announce(StorableObject):
|
|
30
|
_names = 'announces'
|
|
31
|
|
|
32
|
title = None
|
|
33
|
text = None
|
|
34
|
|
|
35
|
hidden = False
|
|
36
|
|
|
37
|
publication_time = None
|
|
38
|
modification_time = None
|
|
39
|
sent_by_email_time = None
|
|
40
|
|
|
41
|
position = None
|
|
42
|
|
|
43
|
def sort_by_position(cls, links):
|
|
44
|
def cmp_position(x, y):
|
|
45
|
if x.position == y.position:
|
|
46
|
return 0
|
|
47
|
if x.position is None:
|
|
48
|
return 1
|
|
49
|
if y.position is None:
|
|
50
|
return -1
|
|
51
|
return cmp(x.position, y.position)
|
|
52
|
links.sort(cmp_position)
|
|
53
|
sort_by_position = classmethod(sort_by_position)
|
|
54
|
|
|
55
|
def get_atom_entry(self):
|
|
56
|
from pyatom import pyatom
|
|
57
|
entry = pyatom.Entry()
|
|
58
|
entry.id = self.get_url()
|
|
59
|
entry.title = self.title
|
|
60
|
|
|
61
|
entry.content.attrs['type'] = 'html'
|
|
62
|
entry.content.text = str('<p>' + htmlescape(
|
|
63
|
unicode(self.text, 'iso-8859-1').encode('utf-8')) + '</p>')
|
|
64
|
|
|
65
|
link = pyatom.Link(self.get_url())
|
|
66
|
entry.links.append(link)
|
|
67
|
|
|
68
|
if self.publication_time:
|
|
69
|
entry.published = misc.format_time(self.publication_time,
|
|
70
|
'%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
|
|
71
|
gmtime = True)
|
|
72
|
|
|
73
|
if self.modification_time:
|
|
74
|
entry.updated = misc.format_time(self.modification_time,
|
|
75
|
'%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
|
|
76
|
gmtime = True)
|
|
77
|
|
|
78
|
return entry
|
|
79
|
|
|
80
|
def get_url(self):
|
|
81
|
from quixote import get_request
|
|
82
|
return '%s://%s/announces/%s/' % (
|
|
83
|
get_request().get_scheme(), get_request().get_server(), self.id)
|
|
84
|
|
|
85
|
def store(self):
|
|
86
|
self.modification_time = time.gmtime()
|
|
87
|
StorableObject.store(self)
|
|
88
|
|
|
89
|
def email(self):
|
|
90
|
self.sent_by_email_time = time.gmtime()
|
|
91
|
StorableObject.store(self)
|
|
92
|
|
|
93
|
data = {
|
|
94
|
'title': self.title,
|
|
95
|
'text': self.text
|
|
96
|
}
|
|
97
|
|
|
98
|
subscribers = AnnounceSubscription.select(lambda x: x.enabled)
|
|
99
|
|
|
100
|
rcpts = []
|
|
101
|
for l in subscribers:
|
|
102
|
if l.user_id and l.user.email:
|
|
103
|
rcpts.append(l.user.email)
|
|
104
|
elif l.email:
|
|
105
|
rcpts.append(l.email)
|
|
106
|
|
|
107
|
emails.custom_ezt_email('aq-announce', data, email_rcpt = rcpts, hide_recipients = True)
|
|
108
|
|
|
109
|
def get_published_announces(cls):
|
|
110
|
announces = cls.select(lambda x: not x.hidden)
|
|
111
|
announces.sort(lambda x,y: cmp(x.publication_time or x.modification_time,
|
|
112
|
y.publication_time or y.modification_time))
|
|
113
|
announces = [x for x in announces if x.publication_time < time.gmtime()]
|
|
114
|
announces.reverse()
|
|
115
|
return announces
|
|
116
|
get_published_announces = classmethod(get_published_announces)
|
|
117
|
|
|
118
|
|
|
119
|
EmailsDirectory.register('aq-announce',
|
|
120
|
N_('Publication of announce to subscriber'),
|
|
121
|
N_('Available variables: title, text'),
|
|
122
|
default_subject = N_('Announce: [title]'),
|
|
123
|
default_body = N_("""\
|
|
124
|
[text]
|
|
125
|
|
|
126
|
--
|
|
127
|
This is an announce sent to you by your city, you can opt to not receive
|
|
128
|
those messages anymore on the city website.
|
|
129
|
"""))
|
|
130
|
|