author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 21 Aug 2009 14:52:07 +0200 | |
branch | 3.5 |
changeset 2955 | 6bb5025c9fc7 |
parent 2920 | 64322aa83a1d |
child 2968 | 0e3460341023 |
child 3136 | ed303cf3a048 |
permissions | -rw-r--r-- |
0 | 1 |
"""some hooks and views to handle supervising of any data changes |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1723
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
from cubicweb import UnknownEid |
|
684 | 12 |
from cubicweb.selectors import none_rset |
2639
5c926026d5e7
R import to avoid warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
13 |
from cubicweb.schema import display_name |
984 | 14 |
from cubicweb.view import Component |
0 | 15 |
from cubicweb.common.mail import format_mail |
16 |
from cubicweb.server.hooksmanager import Hook |
|
17 |
from cubicweb.server.hookhelper import SendMailOp |
|
18 |
||
19 |
||
20 |
class SomethingChangedHook(Hook): |
|
21 |
events = ('before_add_relation', 'before_delete_relation', |
|
22 |
'after_add_entity', 'before_update_entity') |
|
23 |
accepts = ('Any',) |
|
1723 | 24 |
|
0 | 25 |
def call(self, session, *args): |
26 |
dest = self.config['supervising-addrs'] |
|
27 |
if not dest: # no supervisors, don't do this for nothing... |
|
28 |
return |
|
29 |
self.session = session |
|
30 |
if self._call(*args): |
|
31 |
SupervisionMailOp(session) |
|
1723 | 32 |
|
0 | 33 |
def _call(self, *args): |
2649
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
34 |
if self._event() == 'update_entity': |
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
35 |
if args[0].eid in self.session.transaction_data.get('neweids', ()): |
0 | 36 |
return False |
2649
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
37 |
if args[0].e_schema == 'CWUser': |
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
38 |
updated = set(args[0].iterkeys()) |
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
39 |
if not (updated - frozenset(('eid', 'modification_date', |
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
40 |
'last_login_time'))): |
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
41 |
# don't record last_login_time update which are done |
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
42 |
# automatically at login time |
5d4a943695d1
B [supervising] ignore updated event for entities created in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2639
diff
changeset
|
43 |
return False |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
44 |
self.session.transaction_data.setdefault('pendingchanges', []).append( |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
45 |
(self._event(), args)) |
0 | 46 |
return True |
1723 | 47 |
|
0 | 48 |
def _event(self): |
49 |
return self.event.split('_', 1)[1] |
|
50 |
||
51 |
||
52 |
class EntityDeleteHook(SomethingChangedHook): |
|
53 |
events = ('before_delete_entity',) |
|
1723 | 54 |
|
0 | 55 |
def _call(self, eid): |
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2102
diff
changeset
|
56 |
entity = self.session.entity_from_eid(eid) |
0 | 57 |
try: |
58 |
title = entity.dc_title() |
|
59 |
except: |
|
60 |
# may raise an error during deletion process, for instance due to |
|
61 |
# missing required relation |
|
62 |
title = '#%s' % eid |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
63 |
self.session.transaction_data.setdefault('pendingchanges', []).append( |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
64 |
('delete_entity', (eid, str(entity.e_schema), title))) |
0 | 65 |
return True |
66 |
||
67 |
||
68 |
def filter_changes(changes): |
|
69 |
""" |
|
70 |
* when an entity has been deleted: |
|
71 |
* don't show deletion of its relations |
|
72 |
* don't show related TrInfo deletion if any |
|
73 |
* when an entity has been added don't show owned_by relation addition |
|
74 |
* don't show new TrInfo entities if any |
|
75 |
""" |
|
76 |
# first build an index of changes |
|
77 |
index = {} |
|
78 |
added, deleted = set(), set() |
|
79 |
for change in changes[:]: |
|
80 |
event, changedescr = change |
|
81 |
if event == 'add_entity': |
|
82 |
entity = changedescr[0] |
|
83 |
added.add(entity.eid) |
|
84 |
if entity.e_schema == 'TrInfo': |
|
85 |
changes.remove(change) |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
86 |
event = 'change_state' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
87 |
change = (event, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
88 |
(entity.wf_info_for[0], |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
89 |
entity.from_state[0], entity.to_state[0])) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
90 |
changes.append(change) |
0 | 91 |
elif event == 'delete_entity': |
92 |
deleted.add(changedescr[0]) |
|
93 |
index.setdefault(event, set()).add(change) |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
94 |
for key in ('delete_relation', 'add_relation'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
95 |
for change in index.get(key, {}).copy(): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
96 |
if change[1][1] == 'in_state': |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
97 |
index[key].remove(change) |
0 | 98 |
# filter changes |
99 |
for eid in added: |
|
100 |
try: |
|
101 |
for change in index['add_relation'].copy(): |
|
102 |
changedescr = change[1] |
|
103 |
# skip meta-relations which are set automatically |
|
104 |
# XXX generate list below using rtags (category = 'generated') |
|
105 |
if changedescr[1] in ('created_by', 'owned_by', 'is', 'is_instance_of', |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
106 |
'from_state', 'to_state', 'by_transition', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
107 |
'wf_info_for') \ |
0 | 108 |
and changedescr[0] == eid: |
109 |
index['add_relation'].remove(change) |
|
1723 | 110 |
|
0 | 111 |
except KeyError: |
112 |
break |
|
113 |
for eid in deleted: |
|
114 |
try: |
|
115 |
for change in index['delete_relation'].copy(): |
|
116 |
fromeid, rtype, toeid = change[1] |
|
117 |
if fromeid == eid: |
|
118 |
index['delete_relation'].remove(change) |
|
119 |
elif toeid == eid: |
|
120 |
index['delete_relation'].remove(change) |
|
121 |
if rtype == 'wf_info_for': |
|
122 |
for change in index['delete_entity'].copy(): |
|
123 |
if change[1][0] == fromeid: |
|
124 |
index['delete_entity'].remove(change) |
|
125 |
except KeyError: |
|
126 |
break |
|
127 |
for change in changes: |
|
128 |
event, changedescr = change |
|
129 |
if change in index[event]: |
|
130 |
yield change |
|
131 |
||
132 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
655
diff
changeset
|
133 |
class SupervisionEmailView(Component): |
0 | 134 |
"""view implementing the email API for data changes supervision notification |
135 |
""" |
|
730 | 136 |
__select__ = none_rset() |
0 | 137 |
id = 'supervision_notif' |
138 |
||
139 |
def recipients(self): |
|
140 |
return self.config['supervising-addrs'] |
|
1723 | 141 |
|
0 | 142 |
def subject(self): |
143 |
return self.req._('[%s supervision] changes summary') % self.config.appid |
|
1723 | 144 |
|
0 | 145 |
def call(self, changes): |
146 |
user = self.req.actual_session().user |
|
147 |
self.w(self.req._('user %s has made the following change(s):\n\n') |
|
148 |
% user.login) |
|
149 |
for event, changedescr in filter_changes(changes): |
|
150 |
self.w(u'* ') |
|
151 |
getattr(self, event)(*changedescr) |
|
152 |
self.w(u'\n\n') |
|
153 |
||
154 |
def _entity_context(self, entity): |
|
155 |
return {'eid': entity.eid, |
|
156 |
'etype': entity.dc_type().lower(), |
|
157 |
'title': entity.dc_title()} |
|
1723 | 158 |
|
0 | 159 |
def add_entity(self, entity): |
160 |
msg = self.req._('added %(etype)s #%(eid)s (%(title)s)') |
|
161 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
162 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 163 |
|
0 | 164 |
def update_entity(self, entity): |
165 |
msg = self.req._('updated %(etype)s #%(eid)s (%(title)s)') |
|
166 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
167 |
# XXX print changes |
|
168 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 169 |
|
0 | 170 |
def delete_entity(self, eid, etype, title): |
171 |
msg = self.req._('deleted %(etype)s #%(eid)s (%(title)s)') |
|
172 |
etype = display_name(self.req, etype).lower() |
|
173 |
self.w(msg % locals()) |
|
1723 | 174 |
|
0 | 175 |
def change_state(self, entity, fromstate, tostate): |
176 |
msg = self.req._('changed state of %(etype)s #%(eid)s (%(title)s)') |
|
177 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
1723 | 178 |
self.w(_(' from state %(fromstate)s to state %(tostate)s\n' % |
0 | 179 |
{'fromstate': _(fromstate.name), 'tostate': _(tostate.name)})) |
180 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 181 |
|
0 | 182 |
def _relation_context(self, fromeid, rtype, toeid): |
183 |
_ = self.req._ |
|
184 |
session = self.req.actual_session() |
|
185 |
def describe(eid): |
|
186 |
try: |
|
187 |
return _(session.describe(eid)[0]).lower() |
|
188 |
except UnknownEid: |
|
189 |
# may occurs when an entity has been deleted from an external |
|
190 |
# source and we're cleaning its relation |
|
191 |
return _('unknown external entity') |
|
192 |
return {'rtype': _(rtype), |
|
193 |
'fromeid': fromeid, |
|
194 |
'frometype': describe(fromeid), |
|
195 |
'toeid': toeid, |
|
196 |
'toetype': describe(toeid)} |
|
1723 | 197 |
|
0 | 198 |
def add_relation(self, fromeid, rtype, toeid): |
199 |
msg = self.req._('added relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s') |
|
200 |
self.w(msg % self._relation_context(fromeid, rtype, toeid)) |
|
201 |
||
202 |
def delete_relation(self, fromeid, rtype, toeid): |
|
203 |
msg = self.req._('deleted relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s') |
|
204 |
self.w(msg % self._relation_context(fromeid, rtype, toeid)) |
|
1723 | 205 |
|
206 |
||
0 | 207 |
class SupervisionMailOp(SendMailOp): |
208 |
"""special send email operation which should be done only once for a bunch |
|
209 |
of changes |
|
210 |
""" |
|
211 |
def _get_view(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:
2649
diff
changeset
|
212 |
return self.session.vreg['components'].select('supervision_notif', |
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:
2649
diff
changeset
|
213 |
self.session) |
1723 | 214 |
|
0 | 215 |
def _prepare_email(self): |
216 |
session = self.session |
|
217 |
config = session.vreg.config |
|
218 |
uinfo = {'email': config['sender-addr'], |
|
219 |
'name': config['sender-name']} |
|
220 |
view = self._get_view() |
|
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
221 |
content = view.render(changes=session.transaction_data.get('pendingchanges')) |
0 | 222 |
recipients = view.recipients() |
223 |
msg = format_mail(uinfo, recipients, content, view.subject(), config=config) |
|
224 |
self.to_send = [(msg, recipients)] |
|
225 |
||
226 |
def commit_event(self): |
|
227 |
self._prepare_email() |
|
228 |
SendMailOp.commit_event(self) |