author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 20 Aug 2009 17:52:22 +0200 | |
branch | 3.5 |
changeset 2924 | b5aadbd3fc5b |
parent 2879 | ae26a80c0635 |
child 2880 | bfc8e1831290 |
child 3004 | 09ab5e93a02c |
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 |
||
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
|
24 |
parse_message_id = deprecated('parse_message_id is now defined in cubicweb.common.mail') |
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): |
|
2565
df34d3720ff5
make notification views working with no rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2304
diff
changeset
|
59 |
if self.view.rset and self.view.rset[0][0] in self.session.transaction_data.get('pendingeids', ()): |
0 | 60 |
return # entity added and deleted in the same transaction |
61 |
self.view.render_and_send(**getattr(self, 'viewargs', {})) |
|
1477 | 62 |
|
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
|
63 |
|
0 | 64 |
class StatusChangeHook(Hook): |
65 |
"""notify when a workflowable entity has its state modified""" |
|
66 |
events = ('after_add_entity',) |
|
67 |
accepts = ('TrInfo',) |
|
1477 | 68 |
|
0 | 69 |
def call(self, session, entity): |
70 |
if not entity.from_state: # not a transition |
|
71 |
return |
|
72 |
rset = entity.related('wf_info_for') |
|
73 |
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
|
74 |
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
|
75 |
rset=rset, row=0) |
0 | 76 |
except RegistryException: |
77 |
return |
|
78 |
comment = entity.printable_value('comment', format='text/plain') |
|
79 |
if comment: |
|
80 |
comment = normalize_text(comment, 80, |
|
81 |
rest=entity.comment_format=='text/rest') |
|
82 |
RenderAndSendNotificationView(session, view=view, viewargs={ |
|
83 |
'comment': comment, 'previous_state': entity.previous_state.name, |
|
84 |
'current_state': entity.new_state.name}) |
|
85 |
||
86 |
||
87 |
class RelationChangeHook(Hook): |
|
88 |
events = ('before_add_relation', 'after_add_relation', |
|
89 |
'before_delete_relation', 'after_delete_relation') |
|
90 |
accepts = ('Any',) |
|
91 |
def call(self, session, fromeid, rtype, toeid): |
|
92 |
"""if a notification view is defined for the event, send notification |
|
93 |
email defined by the view |
|
94 |
""" |
|
95 |
rset = session.eid_rset(fromeid) |
|
96 |
vid = 'notif_%s_%s' % (self.event, rtype) |
|
97 |
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
|
98 |
view = session.vreg['views'].select(vid, session, rset=rset, row=0) |
0 | 99 |
except RegistryException: |
100 |
return |
|
101 |
RenderAndSendNotificationView(session, view=view) |
|
102 |
||
103 |
||
104 |
class EntityChangeHook(Hook): |
|
105 |
events = ('after_add_entity', |
|
106 |
'after_update_entity') |
|
107 |
accepts = ('Any',) |
|
108 |
def call(self, session, entity): |
|
109 |
"""if a notification view is defined for the event, send notification |
|
110 |
email defined by the view |
|
111 |
""" |
|
112 |
rset = entity.as_rset() |
|
113 |
vid = 'notif_%s' % self.event |
|
114 |
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
|
115 |
view = session.vreg['views'].select(vid, session, rset=rset, row=0) |
0 | 116 |
except RegistryException: |
117 |
return |
|
118 |
RenderAndSendNotificationView(session, view=view) |
|
119 |
||
120 |
||
121 |
# abstract or deactivated notification views and mixin ######################## |
|
122 |
||
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
|
123 |
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
|
124 |
"""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
|
125 |
default |
0 | 126 |
""" |
127 |
||
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
|
128 |
def send_on_commit(self, recipients, msg): |
0 | 129 |
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
|
130 |
send = send_on_commit |
0 | 131 |
|
132 |
class StatusChangeMixIn(object): |
|
133 |
id = 'notif_status_change' |
|
134 |
msgid_timestamp = True |
|
135 |
message = _('status changed') |
|
136 |
content = _(""" |
|
137 |
%(user)s changed status from <%(previous_state)s> to <%(current_state)s> for entity |
|
138 |
'%(title)s' |
|
139 |
||
140 |
%(comment)s |
|
141 |
||
142 |
url: %(url)s |
|
143 |
""") |
|
144 |
||
145 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
146 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
147 |
# Actual notification views. # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
148 |
# # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
149 |
# 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
|
150 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
151 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
152 |
# 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
|
153 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
154 |
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
|
155 |
"""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
|
156 |
|
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
|
157 |
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
|
158 |
* 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
|
159 |
* 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
|
160 |
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
|
161 |
""" |
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
162 |
__abstract__ = True |
1477 | 163 |
id = 'notif_after_add_entity' |
0 | 164 |
msgid_timestamp = False |
165 |
message = _('new') |
|
166 |
content = """ |
|
167 |
%(title)s |
|
168 |
||
169 |
%(content)s |
|
170 |
||
171 |
url: %(url)s |
|
172 |
""" |
|
173 |
||
174 |
def context(self, **kwargs): |
|
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
175 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 176 |
content = entity.printable_value(self.content_attr, format='text/plain') |
177 |
if content: |
|
178 |
contentformat = getattr(entity, self.content_attr + '_format', 'text/rest') |
|
179 |
content = normalize_text(content, 80, rest=contentformat=='text/rest') |
|
761 | 180 |
return super(ContentAddedView, self).context(content=content, **kwargs) |
1477 | 181 |
|
0 | 182 |
def subject(self): |
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
183 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 184 |
return u'%s #%s (%s)' % (self.req.__('New %s' % entity.e_schema), |
185 |
entity.eid, self.user_login()) |
|
186 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
187 |
NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView) |