author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 09 Sep 2009 22:28:09 +0200 | |
branch | stable |
changeset 3157 | f4b94d03f86f |
parent 3131 | 596f893451c0 |
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): |
3131
596f893451c0
[supervision] ignore internal changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
26 |
if session.is_super_session or session.repo.config.repairing: |
596f893451c0
[supervision] ignore internal changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
27 |
return # ignore changes triggered by hooks or maintainance shell |
0 | 28 |
dest = self.config['supervising-addrs'] |
29 |
if not dest: # no supervisors, don't do this for nothing... |
|
30 |
return |
|
31 |
self.session = session |
|
32 |
if self._call(*args): |
|
33 |
SupervisionMailOp(session) |
|
1723 | 34 |
|
0 | 35 |
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
|
36 |
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
|
37 |
if args[0].eid in self.session.transaction_data.get('neweids', ()): |
0 | 38 |
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
|
39 |
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
|
40 |
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
|
41 |
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
|
42 |
'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
|
43 |
# 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
|
44 |
# 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
|
45 |
return False |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
46 |
self.session.transaction_data.setdefault('pendingchanges', []).append( |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
47 |
(self._event(), args)) |
0 | 48 |
return True |
1723 | 49 |
|
0 | 50 |
def _event(self): |
51 |
return self.event.split('_', 1)[1] |
|
52 |
||
53 |
||
54 |
class EntityDeleteHook(SomethingChangedHook): |
|
55 |
events = ('before_delete_entity',) |
|
1723 | 56 |
|
0 | 57 |
def _call(self, eid): |
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2102
diff
changeset
|
58 |
entity = self.session.entity_from_eid(eid) |
0 | 59 |
try: |
60 |
title = entity.dc_title() |
|
61 |
except: |
|
62 |
# may raise an error during deletion process, for instance due to |
|
63 |
# missing required relation |
|
64 |
title = '#%s' % eid |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
65 |
self.session.transaction_data.setdefault('pendingchanges', []).append( |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
66 |
('delete_entity', (eid, str(entity.e_schema), title))) |
0 | 67 |
return True |
68 |
||
69 |
||
70 |
def filter_changes(changes): |
|
71 |
""" |
|
72 |
* when an entity has been deleted: |
|
73 |
* don't show deletion of its relations |
|
74 |
* don't show related TrInfo deletion if any |
|
75 |
* when an entity has been added don't show owned_by relation addition |
|
76 |
* don't show new TrInfo entities if any |
|
77 |
""" |
|
78 |
# first build an index of changes |
|
79 |
index = {} |
|
80 |
added, deleted = set(), set() |
|
81 |
for change in changes[:]: |
|
82 |
event, changedescr = change |
|
83 |
if event == 'add_entity': |
|
84 |
entity = changedescr[0] |
|
85 |
added.add(entity.eid) |
|
86 |
if entity.e_schema == 'TrInfo': |
|
87 |
changes.remove(change) |
|
88 |
if entity.from_state: |
|
89 |
try: |
|
1723 | 90 |
changes.remove( ('delete_relation', |
91 |
(entity.wf_info_for[0].eid, 'in_state', |
|
0 | 92 |
entity.from_state[0].eid)) ) |
93 |
except ValueError: |
|
94 |
pass |
|
95 |
try: |
|
1723 | 96 |
changes.remove( ('add_relation', |
97 |
(entity.wf_info_for[0].eid, 'in_state', |
|
0 | 98 |
entity.to_state[0].eid)) ) |
99 |
except ValueError: |
|
100 |
pass |
|
101 |
event = 'change_state' |
|
1723 | 102 |
change = (event, |
0 | 103 |
(entity.wf_info_for[0], |
104 |
entity.from_state[0], entity.to_state[0])) |
|
105 |
changes.append(change) |
|
106 |
elif event == 'delete_entity': |
|
107 |
deleted.add(changedescr[0]) |
|
108 |
index.setdefault(event, set()).add(change) |
|
109 |
# filter changes |
|
110 |
for eid in added: |
|
111 |
try: |
|
112 |
for change in index['add_relation'].copy(): |
|
113 |
changedescr = change[1] |
|
114 |
# skip meta-relations which are set automatically |
|
115 |
# XXX generate list below using rtags (category = 'generated') |
|
116 |
if changedescr[1] in ('created_by', 'owned_by', 'is', 'is_instance_of', |
|
117 |
'from_state', 'to_state', 'wf_info_for',) \ |
|
118 |
and changedescr[0] == eid: |
|
119 |
index['add_relation'].remove(change) |
|
120 |
# skip in_state relation if the entity is being created |
|
121 |
# XXX this may be automatized by skipping all mandatory relation |
|
122 |
# at entity creation time |
|
123 |
elif changedescr[1] == 'in_state' and changedescr[0] in added: |
|
124 |
index['add_relation'].remove(change) |
|
1723 | 125 |
|
0 | 126 |
except KeyError: |
127 |
break |
|
128 |
for eid in deleted: |
|
129 |
try: |
|
130 |
for change in index['delete_relation'].copy(): |
|
131 |
fromeid, rtype, toeid = change[1] |
|
132 |
if fromeid == eid: |
|
133 |
index['delete_relation'].remove(change) |
|
134 |
elif toeid == eid: |
|
135 |
index['delete_relation'].remove(change) |
|
136 |
if rtype == 'wf_info_for': |
|
137 |
for change in index['delete_entity'].copy(): |
|
138 |
if change[1][0] == fromeid: |
|
139 |
index['delete_entity'].remove(change) |
|
140 |
except KeyError: |
|
141 |
break |
|
142 |
for change in changes: |
|
143 |
event, changedescr = change |
|
144 |
if change in index[event]: |
|
145 |
yield change |
|
146 |
||
147 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
655
diff
changeset
|
148 |
class SupervisionEmailView(Component): |
0 | 149 |
"""view implementing the email API for data changes supervision notification |
150 |
""" |
|
730 | 151 |
__select__ = none_rset() |
0 | 152 |
id = 'supervision_notif' |
153 |
||
154 |
def recipients(self): |
|
155 |
return self.config['supervising-addrs'] |
|
1723 | 156 |
|
0 | 157 |
def subject(self): |
158 |
return self.req._('[%s supervision] changes summary') % self.config.appid |
|
1723 | 159 |
|
0 | 160 |
def call(self, changes): |
161 |
user = self.req.actual_session().user |
|
162 |
self.w(self.req._('user %s has made the following change(s):\n\n') |
|
163 |
% user.login) |
|
164 |
for event, changedescr in filter_changes(changes): |
|
165 |
self.w(u'* ') |
|
166 |
getattr(self, event)(*changedescr) |
|
167 |
self.w(u'\n\n') |
|
168 |
||
169 |
def _entity_context(self, entity): |
|
170 |
return {'eid': entity.eid, |
|
171 |
'etype': entity.dc_type().lower(), |
|
172 |
'title': entity.dc_title()} |
|
1723 | 173 |
|
0 | 174 |
def add_entity(self, entity): |
175 |
msg = self.req._('added %(etype)s #%(eid)s (%(title)s)') |
|
176 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
177 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 178 |
|
0 | 179 |
def update_entity(self, entity): |
180 |
msg = self.req._('updated %(etype)s #%(eid)s (%(title)s)') |
|
181 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
182 |
# XXX print changes |
|
183 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 184 |
|
0 | 185 |
def delete_entity(self, eid, etype, title): |
186 |
msg = self.req._('deleted %(etype)s #%(eid)s (%(title)s)') |
|
187 |
etype = display_name(self.req, etype).lower() |
|
188 |
self.w(msg % locals()) |
|
1723 | 189 |
|
0 | 190 |
def change_state(self, entity, fromstate, tostate): |
191 |
msg = self.req._('changed state of %(etype)s #%(eid)s (%(title)s)') |
|
192 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
1723 | 193 |
self.w(_(' from state %(fromstate)s to state %(tostate)s\n' % |
0 | 194 |
{'fromstate': _(fromstate.name), 'tostate': _(tostate.name)})) |
195 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 196 |
|
0 | 197 |
def _relation_context(self, fromeid, rtype, toeid): |
198 |
_ = self.req._ |
|
199 |
session = self.req.actual_session() |
|
200 |
def describe(eid): |
|
201 |
try: |
|
202 |
return _(session.describe(eid)[0]).lower() |
|
203 |
except UnknownEid: |
|
204 |
# may occurs when an entity has been deleted from an external |
|
205 |
# source and we're cleaning its relation |
|
206 |
return _('unknown external entity') |
|
207 |
return {'rtype': _(rtype), |
|
208 |
'fromeid': fromeid, |
|
209 |
'frometype': describe(fromeid), |
|
210 |
'toeid': toeid, |
|
211 |
'toetype': describe(toeid)} |
|
1723 | 212 |
|
0 | 213 |
def add_relation(self, fromeid, rtype, toeid): |
214 |
msg = self.req._('added relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s') |
|
215 |
self.w(msg % self._relation_context(fromeid, rtype, toeid)) |
|
216 |
||
217 |
def delete_relation(self, fromeid, rtype, toeid): |
|
218 |
msg = self.req._('deleted relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s') |
|
219 |
self.w(msg % self._relation_context(fromeid, rtype, toeid)) |
|
1723 | 220 |
|
221 |
||
0 | 222 |
class SupervisionMailOp(SendMailOp): |
223 |
"""special send email operation which should be done only once for a bunch |
|
224 |
of changes |
|
225 |
""" |
|
226 |
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
|
227 |
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
|
228 |
self.session) |
1723 | 229 |
|
0 | 230 |
def _prepare_email(self): |
231 |
session = self.session |
|
232 |
config = session.vreg.config |
|
233 |
uinfo = {'email': config['sender-addr'], |
|
234 |
'name': config['sender-name']} |
|
235 |
view = self._get_view() |
|
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
236 |
content = view.render(changes=session.transaction_data.get('pendingchanges')) |
0 | 237 |
recipients = view.recipients() |
238 |
msg = format_mail(uinfo, recipients, content, view.subject(), config=config) |
|
239 |
self.to_send = [(msg, recipients)] |
|
240 |
||
241 |
def commit_event(self): |
|
242 |
self._prepare_email() |
|
243 |
SendMailOp.commit_event(self) |