author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 26 Aug 2009 11:14:47 +0200 | |
branch | stable |
changeset 3013 | 6a40a9a72957 |
parent 3004 | 09ab5e93a02c |
child 3023 | 7864fee8b4ec |
child 3045 | 82e0b12054a8 |
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') |
|
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
|
79 |
# 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
|
80 |
# #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
|
81 |
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
|
82 |
comment = normalize_text(comment, 80) |
0 | 83 |
RenderAndSendNotificationView(session, view=view, viewargs={ |
84 |
'comment': comment, 'previous_state': entity.previous_state.name, |
|
85 |
'current_state': entity.new_state.name}) |
|
86 |
||
87 |
||
88 |
class RelationChangeHook(Hook): |
|
89 |
events = ('before_add_relation', 'after_add_relation', |
|
90 |
'before_delete_relation', 'after_delete_relation') |
|
91 |
accepts = ('Any',) |
|
92 |
def call(self, session, fromeid, rtype, toeid): |
|
93 |
"""if a notification view is defined for the event, send notification |
|
94 |
email defined by the view |
|
95 |
""" |
|
96 |
rset = session.eid_rset(fromeid) |
|
97 |
vid = 'notif_%s_%s' % (self.event, rtype) |
|
98 |
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
|
99 |
view = session.vreg['views'].select(vid, session, rset=rset, row=0) |
0 | 100 |
except RegistryException: |
101 |
return |
|
102 |
RenderAndSendNotificationView(session, view=view) |
|
103 |
||
104 |
||
105 |
class EntityChangeHook(Hook): |
|
106 |
events = ('after_add_entity', |
|
107 |
'after_update_entity') |
|
108 |
accepts = ('Any',) |
|
109 |
def call(self, session, entity): |
|
110 |
"""if a notification view is defined for the event, send notification |
|
111 |
email defined by the view |
|
112 |
""" |
|
113 |
rset = entity.as_rset() |
|
114 |
vid = 'notif_%s' % self.event |
|
115 |
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
|
116 |
view = session.vreg['views'].select(vid, session, rset=rset, row=0) |
0 | 117 |
except RegistryException: |
118 |
return |
|
119 |
RenderAndSendNotificationView(session, view=view) |
|
120 |
||
121 |
||
122 |
# abstract or deactivated notification views and mixin ######################## |
|
123 |
||
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
|
124 |
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
|
125 |
"""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
|
126 |
default |
0 | 127 |
""" |
128 |
||
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
|
129 |
def send_on_commit(self, recipients, msg): |
0 | 130 |
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
|
131 |
send = send_on_commit |
0 | 132 |
|
133 |
class StatusChangeMixIn(object): |
|
134 |
id = 'notif_status_change' |
|
135 |
msgid_timestamp = True |
|
136 |
message = _('status changed') |
|
137 |
content = _(""" |
|
138 |
%(user)s changed status from <%(previous_state)s> to <%(current_state)s> for entity |
|
139 |
'%(title)s' |
|
140 |
||
141 |
%(comment)s |
|
142 |
||
143 |
url: %(url)s |
|
144 |
""") |
|
145 |
||
146 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
147 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
148 |
# Actual notification views. # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
149 |
# # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
150 |
# 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
|
151 |
############################################################################### |
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 |
# 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
|
154 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
155 |
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
|
156 |
"""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
|
157 |
|
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 |
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
|
159 |
* 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
|
160 |
* 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
|
161 |
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
|
162 |
""" |
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
163 |
__abstract__ = True |
1477 | 164 |
id = 'notif_after_add_entity' |
0 | 165 |
msgid_timestamp = False |
166 |
message = _('new') |
|
167 |
content = """ |
|
168 |
%(title)s |
|
169 |
||
170 |
%(content)s |
|
171 |
||
172 |
url: %(url)s |
|
173 |
""" |
|
174 |
||
175 |
def context(self, **kwargs): |
|
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
176 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 177 |
content = entity.printable_value(self.content_attr, format='text/plain') |
178 |
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
|
179 |
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
|
180 |
'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
|
181 |
# 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
|
182 |
# #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
|
183 |
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
|
184 |
content = normalize_text(content, 80) |
761 | 185 |
return super(ContentAddedView, self).context(content=content, **kwargs) |
1477 | 186 |
|
0 | 187 |
def subject(self): |
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
188 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 189 |
return u'%s #%s (%s)' % (self.req.__('New %s' % entity.e_schema), |
190 |
entity.eid, self.user_login()) |
|
191 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
192 |
NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView) |