author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Mon, 26 Oct 2009 10:04:18 +0100 | |
branch | stable |
changeset 3786 | 11d98118f0eb |
parent 3689 | deb13e88e037 |
child 3720 | 5376aaadd16b |
child 3877 | 7ca53fc72a0a |
child 4212 | ab6573088b4a |
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 |
3580
e270358f68cc
[sobjects] missing import
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3525
diff
changeset
|
19 |
from cubicweb.common.mail import NotificationView, parse_message_id, SkipEmail |
3525
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
20 |
from cubicweb.server.pool import PreCommitOperation, SingleLastOperation |
0 | 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 |
||
3525
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
56 |
class EntityUpdatedNotificationOp(SingleLastOperation): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
57 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
58 |
def precommit_event(self): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
59 |
session = self.session |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
60 |
for eid in session.transaction_data['changes']: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
61 |
view = session.vreg['views'].select('notif_entity_updated', session, |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
62 |
rset=session.eid_rset(eid), |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
63 |
row=0) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
64 |
RenderAndSendNotificationView(session, view=view) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
65 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
66 |
def commit_event(self): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
67 |
pass |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
68 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
69 |
|
0 | 70 |
class RenderAndSendNotificationView(PreCommitOperation): |
71 |
"""delay rendering of notification view until precommit""" |
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
if view.rset and view.rset[0][0] in self.session.transaction_data.get('pendingeids', ()): |
0 | 77 |
return # entity added and deleted in the same transaction |
78 |
self.view.render_and_send(**getattr(self, 'viewargs', {})) |
|
1477 | 79 |
|
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
|
80 |
|
0 | 81 |
class StatusChangeHook(Hook): |
82 |
"""notify when a workflowable entity has its state modified""" |
|
83 |
events = ('after_add_entity',) |
|
84 |
accepts = ('TrInfo',) |
|
1477 | 85 |
|
0 | 86 |
def call(self, session, entity): |
87 |
if not entity.from_state: # not a transition |
|
88 |
return |
|
89 |
rset = entity.related('wf_info_for') |
|
90 |
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
|
91 |
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
|
92 |
rset=rset, row=0) |
0 | 93 |
except RegistryException: |
94 |
return |
|
95 |
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
|
96 |
# 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
|
97 |
# #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
|
98 |
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
|
99 |
comment = normalize_text(comment, 80) |
0 | 100 |
RenderAndSendNotificationView(session, view=view, viewargs={ |
101 |
'comment': comment, 'previous_state': entity.previous_state.name, |
|
102 |
'current_state': entity.new_state.name}) |
|
103 |
||
104 |
||
105 |
class RelationChangeHook(Hook): |
|
106 |
events = ('before_add_relation', 'after_add_relation', |
|
107 |
'before_delete_relation', 'after_delete_relation') |
|
108 |
accepts = ('Any',) |
|
109 |
def call(self, session, fromeid, rtype, toeid): |
|
110 |
"""if a notification view is defined for the event, send notification |
|
111 |
email defined by the view |
|
112 |
""" |
|
113 |
rset = session.eid_rset(fromeid) |
|
114 |
vid = 'notif_%s_%s' % (self.event, rtype) |
|
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 |
class EntityChangeHook(Hook): |
|
123 |
events = ('after_add_entity', |
|
124 |
'after_update_entity') |
|
125 |
accepts = ('Any',) |
|
126 |
def call(self, session, entity): |
|
127 |
"""if a notification view is defined for the event, send notification |
|
128 |
email defined by the view |
|
129 |
""" |
|
130 |
rset = entity.as_rset() |
|
131 |
vid = 'notif_%s' % self.event |
|
132 |
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
|
133 |
view = session.vreg['views'].select(vid, session, rset=rset, row=0) |
0 | 134 |
except RegistryException: |
135 |
return |
|
136 |
RenderAndSendNotificationView(session, view=view) |
|
137 |
||
3525
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
138 |
class EntityUpdateHook(Hook): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
139 |
events = ('before_update_entity',) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
140 |
accepts = () |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
141 |
skip_attrs = set() |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
142 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
143 |
def call(self, session, entity): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
144 |
if entity.eid in session.transaction_data.get('neweids', ()): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
145 |
return # entity is being created |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
146 |
if session.is_super_session: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
147 |
return # ignore changes triggered by hooks |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
148 |
# then compute changes |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
149 |
changes = session.transaction_data.setdefault('changes', {}) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
150 |
thisentitychanges = changes.setdefault(entity.eid, set()) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
151 |
attrs = [k for k in entity.edited_attributes if not k in self.skip_attrs] |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
152 |
if not attrs: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
153 |
return |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
154 |
rqlsel, rqlrestr = [], ['X eid %(x)s'] |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
155 |
for i, attr in enumerate(attrs): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
156 |
var = chr(65+i) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
157 |
rqlsel.append(var) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
158 |
rqlrestr.append('X %s %s' % (attr, var)) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
159 |
rql = 'Any %s WHERE %s' % (','.join(rqlsel), ','.join(rqlrestr)) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
160 |
rset = session.execute(rql, {'x': entity.eid}, 'x') |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
161 |
for i, attr in enumerate(attrs): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
162 |
oldvalue = rset[0][i] |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
163 |
newvalue = entity[attr] |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
164 |
if oldvalue != newvalue: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
165 |
thisentitychanges.add((attr, oldvalue, newvalue)) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
166 |
if thisentitychanges: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
167 |
EntityUpdatedNotificationOp(session) |
0 | 168 |
|
169 |
# abstract or deactivated notification views and mixin ######################## |
|
170 |
||
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
|
171 |
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
|
172 |
"""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
|
173 |
default |
0 | 174 |
""" |
175 |
||
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
|
176 |
def send_on_commit(self, recipients, msg): |
0 | 177 |
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
|
178 |
send = send_on_commit |
0 | 179 |
|
180 |
class StatusChangeMixIn(object): |
|
181 |
id = 'notif_status_change' |
|
182 |
msgid_timestamp = True |
|
183 |
message = _('status changed') |
|
184 |
content = _(""" |
|
185 |
%(user)s changed status from <%(previous_state)s> to <%(current_state)s> for entity |
|
186 |
'%(title)s' |
|
187 |
||
188 |
%(comment)s |
|
189 |
||
190 |
url: %(url)s |
|
191 |
""") |
|
192 |
||
193 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
194 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
195 |
# Actual notification views. # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
196 |
# # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
197 |
# 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
|
198 |
############################################################################### |
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 |
# 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
|
201 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
202 |
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
|
203 |
"""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
|
204 |
|
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
|
205 |
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
|
206 |
* 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
|
207 |
* 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
|
208 |
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
|
209 |
""" |
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
210 |
__abstract__ = True |
1477 | 211 |
id = 'notif_after_add_entity' |
0 | 212 |
msgid_timestamp = False |
213 |
message = _('new') |
|
214 |
content = """ |
|
215 |
%(title)s |
|
216 |
||
217 |
%(content)s |
|
218 |
||
219 |
url: %(url)s |
|
220 |
""" |
|
221 |
||
222 |
def context(self, **kwargs): |
|
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
223 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 224 |
content = entity.printable_value(self.content_attr, format='text/plain') |
225 |
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
|
226 |
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
|
227 |
'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
|
228 |
# 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
|
229 |
# #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
|
230 |
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
|
231 |
content = normalize_text(content, 80) |
761 | 232 |
return super(ContentAddedView, self).context(content=content, **kwargs) |
1477 | 233 |
|
0 | 234 |
def subject(self): |
2572
58556f9317c9
[notification view] consider row/col
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2565
diff
changeset
|
235 |
entity = self.entity(self.row or 0, self.col or 0) |
0 | 236 |
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
|
237 |
entity.eid, self.user_data['login']) |
0 | 238 |
|
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
239 |
NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView) |
3525
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
240 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
241 |
def format_value(value): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
242 |
if isinstance(value, unicode): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
243 |
return u'"%s"' % value |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
244 |
return value |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
245 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
246 |
class EntityUpdatedNotificationView(NotificationView): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
247 |
"""abstract class for notification on entity/relation |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
248 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
249 |
all you have to do by default is : |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
250 |
* set id and __select__ attributes to match desired events and entity types |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
251 |
* set a content attribute to define the content of the email (unless you |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
252 |
override call) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
253 |
""" |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
254 |
__abstract__ = True |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
255 |
id = 'notif_entity_updated' |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
256 |
msgid_timestamp = False |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
257 |
message = _('updated') |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
258 |
no_detailed_change_attrs = () |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
259 |
content = """ |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
260 |
Properties have been updated by %(user)s: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
261 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
262 |
%(changes)s |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
263 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
264 |
url: %(url)s |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
265 |
""" |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
266 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
267 |
def context(self, **kwargs): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
268 |
context = super(EntityUpdatedNotificationView, self).context(**kwargs) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
269 |
changes = self.req.transaction_data['changes'][self.rset[0][0]] |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
270 |
_ = self.req._ |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
271 |
formatted_changes = [] |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
272 |
for attr, oldvalue, newvalue in sorted(changes): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
273 |
# check current user has permission to see the attribute |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
274 |
rschema = self.vreg.schema[attr] |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3580
diff
changeset
|
275 |
if rschema.final: |
3525
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
276 |
if not rschema.has_perm(self.req, 'read', eid=self.rset[0][0]): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
277 |
continue |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
278 |
# XXX suppose it's a subject relation... |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
279 |
elif not rschema.has_perm(self.req, 'read', fromeid=self.rset[0][0]): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
280 |
continue |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
281 |
if attr in self.no_detailed_change_attrs: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
282 |
msg = _('%s updated') % _(attr) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
283 |
elif oldvalue not in (None, ''): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
284 |
msg = _('%(attr)s updated from %(oldvalue)s to %(newvalue)s') % { |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
285 |
'attr': _(attr), |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
286 |
'oldvalue': format_value(oldvalue), |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
287 |
'newvalue': format_value(newvalue)} |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
288 |
else: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
289 |
msg = _('%(attr)s set to %(newvalue)s') % { |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
290 |
'attr': _(attr), 'newvalue': format_value(newvalue)} |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
291 |
formatted_changes.append('* ' + msg) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
292 |
if not formatted_changes: |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
293 |
# current user isn't allowed to see changes, skip this notification |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
294 |
raise SkipEmail() |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
295 |
context['changes'] = '\n'.join(formatted_changes) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
296 |
return context |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
297 |
|
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
298 |
def subject(self): |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
299 |
entity = self.entity(self.row or 0, self.col or 0) |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
300 |
return u'%s #%s (%s)' % (self.req.__('Updated %s' % entity.e_schema), |
2dc3908f667f
[notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3110
diff
changeset
|
301 |
entity.eid, self.user_data['login']) |