author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Mon, 21 Sep 2009 17:04:19 +0200 | |
branch | stable |
changeset 3343 | 383b42263bb1 |
parent 3110 | 757d36162235 |
child 3163 | edfe43ceaa35 |
child 3525 | 2dc3908f667f |
permissions | -rw-r--r-- |
0 | 1 |
"""some hooks and views to handle notification on entity's changes |
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 itertools import repeat |
|
12 |
||
13 |
from logilab.common.textutils import normalize_text |
|
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
14 |
from logilab.common.deprecation import class_renamed, deprecated |
0 | 15 |
|
16 |
from cubicweb import RegistryException |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
761
diff
changeset
|
17 |
from cubicweb.selectors import implements, yes |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
18 |
from cubicweb.view import Component |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
19 |
from cubicweb.common.mail import NotificationView, parse_message_id |
0 | 20 |
from cubicweb.server.pool import PreCommitOperation |
21 |
from cubicweb.server.hookhelper import SendMailOp |
|
22 |
from cubicweb.server.hooksmanager import Hook |
|
23 |
||
3051 | 24 |
parse_message_id = deprecated('parse_message_id is now defined in cubicweb.common.mail')(parse_message_id) |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
25 |
|
0 | 26 |
|
27 |
class RecipientsFinder(Component): |
|
28 |
"""this component is responsible to find recipients of a notification |
|
29 |
||
30 |
by default user's with their email set are notified if any, else the default |
|
31 |
email addresses specified in the configuration are used |
|
32 |
""" |
|
33 |
id = 'recipients_finder' |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
761
diff
changeset
|
34 |
__select__ = yes() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
35 |
user_rql = ('Any X,E,A WHERE X is CWUser, X in_state S, S name "activated",' |
0 | 36 |
'X primary_email E, E address A') |
1477 | 37 |
|
0 | 38 |
def recipients(self): |
39 |
mode = self.config['default-recipients-mode'] |
|
40 |
if mode == 'users': |
|
41 |
# use unsafe execute else we may don't have the right to see users |
|
42 |
# to notify... |
|
43 |
execute = self.req.unsafe_execute |
|
44 |
dests = [(u.get_email(), u.property_value('ui.language')) |
|
45 |
for u in execute(self.user_rql, build_descr=True, propagate=True).entities()] |
|
46 |
elif mode == 'default-dest-addrs': |
|
47 |
lang = self.vreg.property_value('ui.language') |
|
48 |
dests = zip(self.config['default-dest-addrs'], repeat(lang)) |
|
49 |
else: # mode == 'none' |
|
50 |
dests = [] |
|
51 |
return dests |
|
52 |
||
1477 | 53 |
|
0 | 54 |
# hooks ####################################################################### |
55 |
||
56 |
class RenderAndSendNotificationView(PreCommitOperation): |
|
57 |
"""delay rendering of notification view until precommit""" |
|
58 |
def precommit_event(self): |
|
3045
82e0b12054a8
fix potential session cache effect: entity's rset may have been emptied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3004
diff
changeset
|
59 |
view = self.view |
82e0b12054a8
fix potential session cache effect: entity's rset may have been emptied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3004
diff
changeset
|
60 |
if view.rset is not None and not view.rset: |
82e0b12054a8
fix potential session cache effect: entity's rset may have been emptied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3004
diff
changeset
|
61 |
return # entity added and deleted in the same transaction (cache effect) |
82e0b12054a8
fix potential session cache effect: entity's rset may have been emptied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3004
diff
changeset
|
62 |
if view.rset and view.rset[0][0] in self.session.transaction_data.get('pendingeids', ()): |
0 | 63 |
return # entity added and deleted in the same transaction |
64 |
self.view.render_and_send(**getattr(self, 'viewargs', {})) |
|
1477 | 65 |
|
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
66 |
|
0 | 67 |
class StatusChangeHook(Hook): |
68 |
"""notify when a workflowable entity has its state modified""" |
|
69 |
events = ('after_add_entity',) |
|
70 |
accepts = ('TrInfo',) |
|
1477 | 71 |
|
0 | 72 |
def call(self, session, entity): |
73 |
if not entity.from_state: # not a transition |
|
74 |
return |
|
75 |
rset = entity.related('wf_info_for') |
|
76 |
try: |
|
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
|
77 |
view = session.vreg['views'].select('notif_status_change', session, |
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
|
78 |
rset=rset, row=0) |
0 | 79 |
except RegistryException: |
80 |
return |
|
81 |
comment = entity.printable_value('comment', format='text/plain') |
|
3004
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
82 |
# XXX don't try to wrap rest until we've a proper transformation (see |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
83 |
# #103822) |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
84 |
if comment and entity.comment_format != 'text/rest': |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
85 |
comment = normalize_text(comment, 80) |
0 | 86 |
RenderAndSendNotificationView(session, view=view, viewargs={ |
87 |
'comment': comment, 'previous_state': entity.previous_state.name, |
|
88 |
'current_state': entity.new_state.name}) |
|
89 |
||
90 |
||
91 |
class RelationChangeHook(Hook): |
|
92 |
events = ('before_add_relation', 'after_add_relation', |
|
93 |
'before_delete_relation', 'after_delete_relation') |
|
94 |
accepts = ('Any',) |
|
95 |
def call(self, session, fromeid, rtype, toeid): |
|
96 |
"""if a notification view is defined for the event, send notification |
|
97 |
email defined by the view |
|
98 |
""" |
|
99 |
rset = session.eid_rset(fromeid) |
|
100 |
vid = 'notif_%s_%s' % (self.event, rtype) |
|
101 |
try: |
|
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
|
102 |
view = session.vreg['views'].select(vid, session, rset=rset, row=0) |
0 | 103 |
except RegistryException: |
104 |
return |
|
105 |
RenderAndSendNotificationView(session, view=view) |
|
106 |
||
107 |
||
108 |
class EntityChangeHook(Hook): |
|
109 |
events = ('after_add_entity', |
|
110 |
'after_update_entity') |
|
111 |
accepts = ('Any',) |
|
112 |
def call(self, session, entity): |
|
113 |
"""if a notification view is defined for the event, send notification |
|
114 |
email defined by the view |
|
115 |
""" |
|
116 |
rset = entity.as_rset() |
|
117 |
vid = 'notif_%s' % self.event |
|
118 |
try: |
|
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
|
119 |
view = session.vreg['views'].select(vid, session, rset=rset, row=0) |
0 | 120 |
except RegistryException: |
121 |
return |
|
122 |
RenderAndSendNotificationView(session, view=view) |
|
123 |
||
124 |
||
125 |
# abstract or deactivated notification views and mixin ######################## |
|
126 |
||
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
127 |
class NotificationView(NotificationView): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
128 |
"""overriden to delay actual sending of mails to a commit operation by |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
129 |
default |
0 | 130 |
""" |
131 |
||
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
132 |
def send_on_commit(self, recipients, msg): |
0 | 133 |
SendMailOp(self.req, recipients=recipients, msg=msg) |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
134 |
send = send_on_commit |
0 | 135 |
|
136 |
class StatusChangeMixIn(object): |
|
137 |
id = 'notif_status_change' |
|
138 |
msgid_timestamp = True |
|
139 |
message = _('status changed') |
|
140 |
content = _(""" |
|
141 |
%(user)s changed status from <%(previous_state)s> to <%(current_state)s> for entity |
|
142 |
'%(title)s' |
|
143 |
||
144 |
%(comment)s |
|
145 |
||
146 |
url: %(url)s |
|
147 |
""") |
|
148 |
||
149 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
150 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
151 |
# Actual notification views. # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
152 |
# # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
153 |
# 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
|
154 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
155 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
156 |
# 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
|
157 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
158 |
class ContentAddedView(NotificationView): |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
159 |
"""abstract class for notification on entity/relation |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
160 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
161 |
all you have to do by default is : |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
162 |
* set id and __select__ attributes to match desired events and entity types |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
163 |
* set a content attribute to define the content of the email (unless you |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
164 |
override call) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
165 |
""" |
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
166 |
__abstract__ = True |
1477 | 167 |
id = 'notif_after_add_entity' |
0 | 168 |
msgid_timestamp = False |
169 |
message = _('new') |
|
170 |
content = """ |
|
171 |
%(title)s |
|
172 |
||
173 |
%(content)s |
|
174 |
||
175 |
url: %(url)s |
|
176 |
""" |
|
177 |
||
178 |
def context(self, **kwargs): |
|
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
179 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 180 |
content = entity.printable_value(self.content_attr, format='text/plain') |
181 |
if content: |
|
3004
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
182 |
contentformat = getattr(entity, self.content_attr + '_format', |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
183 |
'text/rest') |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
184 |
# XXX don't try to wrap rest until we've a proper transformation (see |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
185 |
# #103822) |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
186 |
if contentformat != 'text/rest': |
09ab5e93a02c
[notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
187 |
content = normalize_text(content, 80) |
761 | 188 |
return super(ContentAddedView, self).context(content=content, **kwargs) |
1477 | 189 |
|
0 | 190 |
def subject(self): |
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
191 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 192 |
return u'%s #%s (%s)' % (self.req.__('New %s' % entity.e_schema), |
3110
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3051
diff
changeset
|
193 |
entity.eid, self.user_data['login']) |
0 | 194 |
|
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
195 |
NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView) |