author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 14 Aug 2009 11:14:10 +0200 | |
changeset 2841 | 107ba1c45227 |
parent 2818 | 326375561412 |
child 2880 | bfc8e1831290 |
permissions | -rw-r--r-- |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
1 |
"""some views to handle notification on data changes |
0 | 2 |
|
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1723
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1723
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
_ = unicode |
0 | 10 |
|
11 |
from base64 import b64encode, b64decode |
|
12 |
from itertools import repeat |
|
13 |
from time import time |
|
14 |
try: |
|
15 |
from socket import gethostname |
|
16 |
except ImportError: |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
17 |
def gethostname(): # gae |
0 | 18 |
return 'XXX' |
19 |
||
20 |
from logilab.common.textutils import normalize_text |
|
21 |
||
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
22 |
from cubicweb.selectors import yes |
803 | 23 |
from cubicweb.view import EntityView, Component |
0 | 24 |
from cubicweb.common.mail import format_mail |
25 |
||
26 |
from cubicweb.server.hookhelper import SendMailOp |
|
27 |
||
28 |
||
29 |
class RecipientsFinder(Component): |
|
30 |
"""this component is responsible to find recipients of a notification |
|
31 |
||
32 |
by default user's with their email set are notified if any, else the default |
|
33 |
email addresses specified in the configuration are used |
|
34 |
""" |
|
35 |
id = 'recipients_finder' |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
761
diff
changeset
|
36 |
__select__ = yes() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
37 |
user_rql = ('Any X,E,A WHERE X is CWUser, X in_state S, S name "activated",' |
0 | 38 |
'X primary_email E, E address A') |
1477 | 39 |
|
0 | 40 |
def recipients(self): |
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
41 |
mode = self.req.vreg.config['default-recipients-mode'] |
0 | 42 |
if mode == 'users': |
43 |
# use unsafe execute else we may don't have the right to see users |
|
44 |
# to notify... |
|
45 |
execute = self.req.unsafe_execute |
|
46 |
dests = [(u.get_email(), u.property_value('ui.language')) |
|
47 |
for u in execute(self.user_rql, build_descr=True, propagate=True).entities()] |
|
48 |
elif mode == 'default-dest-addrs': |
|
49 |
lang = self.vreg.property_value('ui.language') |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
50 |
dests = zip(self.req.vreg.config['default-dest-addrs'], repeat(lang)) |
0 | 51 |
else: # mode == 'none' |
52 |
dests = [] |
|
53 |
return dests |
|
54 |
||
1477 | 55 |
|
0 | 56 |
# abstract or deactivated notification views and mixin ######################## |
57 |
||
58 |
class NotificationView(EntityView): |
|
59 |
"""abstract view implementing the email API |
|
60 |
||
61 |
all you have to do by default is : |
|
62 |
* set id and accepts attributes to match desired events and entity types |
|
63 |
* set a content attribute to define the content of the email (unless you |
|
64 |
override call) |
|
65 |
""" |
|
2577 | 66 |
# XXX refactor this class to work with len(rset) > 1 |
67 |
||
0 | 68 |
msgid_timestamp = True |
2577 | 69 |
|
0 | 70 |
def recipients(self): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
71 |
finder = self.vreg['components'].select('recipients_finder', self.req, |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
72 |
rset=self.rset) |
0 | 73 |
return finder.recipients() |
1477 | 74 |
|
0 | 75 |
def subject(self): |
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
76 |
entity = self.rset.get_entity(self.row or 0, self.col or 0) |
0 | 77 |
subject = self.req._(self.message) |
78 |
etype = entity.dc_type() |
|
79 |
eid = entity.eid |
|
80 |
login = self.user_login() |
|
81 |
return self.req._('%(subject)s %(etype)s #%(eid)s (%(login)s)') % locals() |
|
82 |
||
83 |
def user_login(self): |
|
84 |
# req is actually a session (we are on the server side), and we have to |
|
85 |
# prevent nested internal session |
|
86 |
return self.req.actual_session().user.login |
|
1477 | 87 |
|
0 | 88 |
def context(self, **kwargs): |
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
89 |
entity = self.rset.get_entity(self.row or 0, self.col or 0) |
0 | 90 |
for key, val in kwargs.iteritems(): |
1126 | 91 |
if val and isinstance(val, unicode) and val.strip(): |
92 |
kwargs[key] = self.req._(val) |
|
0 | 93 |
kwargs.update({'user': self.user_login(), |
94 |
'eid': entity.eid, |
|
95 |
'etype': entity.dc_type(), |
|
96 |
'url': entity.absolute_url(), |
|
97 |
'title': entity.dc_long_title(),}) |
|
98 |
return kwargs |
|
1477 | 99 |
|
0 | 100 |
def cell_call(self, row, col=0, **kwargs): |
101 |
self.w(self.req._(self.content) % self.context(**kwargs)) |
|
102 |
||
103 |
def construct_message_id(self, eid): |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
104 |
return construct_message_id(self.req.vreg.config.appid, eid, self.msgid_timestamp) |
0 | 105 |
|
106 |
def render_and_send(self, **kwargs): |
|
107 |
"""generate and send an email message for this view""" |
|
108 |
self._kwargs = kwargs |
|
109 |
recipients = self.recipients() |
|
110 |
if not recipients: |
|
877
decb67772c92
should not use named args for req and rset when selecting
sylvain.thenault@logilab.fr
parents:
803
diff
changeset
|
111 |
self.info('skipping %s notification, no recipients', self.id) |
0 | 112 |
return |
113 |
if not isinstance(recipients[0], tuple): |
|
114 |
from warnings import warn |
|
115 |
warn('recipients should now return a list of 2-uple (email, language)', |
|
116 |
DeprecationWarning, stacklevel=1) |
|
117 |
lang = self.vreg.property_value('ui.language') |
|
118 |
recipients = zip(recipients, repeat(lang)) |
|
2565
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
119 |
if self.rset is not None: |
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
120 |
entity = self.rset.get_entity(self.row or 0, self.col or 0) |
2565
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
121 |
# if the view is using timestamp in message ids, no way to reference |
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
122 |
# previous email |
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
123 |
if not self.msgid_timestamp: |
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
124 |
refs = [self.construct_message_id(eid) |
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
125 |
for eid in entity.notification_references(self)] |
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
126 |
else: |
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
127 |
refs = () |
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
128 |
msgid = self.construct_message_id(entity.eid) |
0 | 129 |
else: |
130 |
refs = () |
|
2565
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
131 |
msgid = None |
0 | 132 |
userdata = self.req.user_data() |
133 |
origlang = self.req.lang |
|
134 |
for emailaddr, lang in recipients: |
|
135 |
self.req.set_language(lang) |
|
136 |
# since the same view (eg self) may be called multiple time and we |
|
137 |
# need a fresh stream at each iteration, reset it explicitly |
|
138 |
self.w = None |
|
1723 | 139 |
# XXX call render before subject to set .row/.col attributes on the |
140 |
# view |
|
141 |
content = self.render(row=0, col=0, **kwargs) |
|
0 | 142 |
subject = self.subject() |
143 |
msg = format_mail(userdata, [emailaddr], content, subject, |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
144 |
config=self.req.vreg.config, msgid=msgid, references=refs) |
0 | 145 |
self.send([emailaddr], msg) |
146 |
# restore language |
|
147 |
self.req.set_language(origlang) |
|
148 |
||
149 |
def send(self, recipients, msg): |
|
150 |
SendMailOp(self.req, recipients=recipients, msg=msg) |
|
151 |
||
152 |
||
153 |
def construct_message_id(appid, eid, withtimestamp=True): |
|
154 |
if withtimestamp: |
|
155 |
addrpart = 'eid=%s×tamp=%.10f' % (eid, time()) |
|
156 |
else: |
|
157 |
addrpart = 'eid=%s' % eid |
|
158 |
# we don't want any equal sign nor trailing newlines |
|
159 |
leftpart = b64encode(addrpart, '.-').rstrip().rstrip('=') |
|
160 |
return '<%s@%s.%s>' % (leftpart, appid, gethostname()) |
|
161 |
||
162 |
||
163 |
def parse_message_id(msgid, appid): |
|
164 |
if msgid[0] == '<': |
|
165 |
msgid = msgid[1:] |
|
166 |
if msgid[-1] == '>': |
|
167 |
msgid = msgid[:-1] |
|
168 |
try: |
|
169 |
values, qualif = msgid.split('@') |
|
170 |
padding = len(values) % 4 |
|
171 |
values = b64decode(str(values + '='*padding), '.-') |
|
172 |
values = dict(v.split('=') for v in values.split('&')) |
|
173 |
fromappid, host = qualif.split('.', 1) |
|
174 |
except: |
|
175 |
return None |
|
176 |
if appid != fromappid or host != gethostname(): |
|
177 |
return None |
|
178 |
return values |
|
1477 | 179 |
|
0 | 180 |
|
181 |
class StatusChangeMixIn(object): |
|
182 |
id = 'notif_status_change' |
|
183 |
msgid_timestamp = True |
|
184 |
message = _('status changed') |
|
185 |
content = _(""" |
|
186 |
%(user)s changed status from <%(previous_state)s> to <%(current_state)s> for entity |
|
187 |
'%(title)s' |
|
188 |
||
189 |
%(comment)s |
|
190 |
||
191 |
url: %(url)s |
|
192 |
""") |
|
193 |
||
194 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
195 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
196 |
# Actual notification views. # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
197 |
# # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
198 |
# disable them at the recipients_finder level if you don't want them # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
199 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
200 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
201 |
# XXX should be based on dc_title/dc_description, no? |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
202 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
203 |
class ContentAddedView(NotificationView): |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
204 |
__abstract__ = True |
1477 | 205 |
id = 'notif_after_add_entity' |
0 | 206 |
msgid_timestamp = False |
207 |
message = _('new') |
|
208 |
content = """ |
|
209 |
%(title)s |
|
210 |
||
211 |
%(content)s |
|
212 |
||
213 |
url: %(url)s |
|
214 |
""" |
|
215 |
||
216 |
def context(self, **kwargs): |
|
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
217 |
entity = self.rset.get_entity(self.row or 0, self.col or 0) |
0 | 218 |
content = entity.printable_value(self.content_attr, format='text/plain') |
219 |
if content: |
|
220 |
contentformat = getattr(entity, self.content_attr + '_format', 'text/rest') |
|
221 |
content = normalize_text(content, 80, rest=contentformat=='text/rest') |
|
761 | 222 |
return super(ContentAddedView, self).context(content=content, **kwargs) |
1477 | 223 |
|
0 | 224 |
def subject(self): |
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
225 |
entity = self.rset.get_entity(self.row or 0, self.col or 0) |
0 | 226 |
return u'%s #%s (%s)' % (self.req.__('New %s' % entity.e_schema), |
227 |
entity.eid, self.user_login()) |
|
228 |
||
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
229 |
from logilab.common.deprecation import class_renamed, class_moved |
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
230 |
NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView) |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
231 |
from cubicweb.hooks.notification import RenderAndSendNotificationView |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
232 |
RenderAndSendNotificationView = class_moved(RenderAndSendNotificationView) |