author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 11 Sep 2009 15:55:15 +0200 | |
branch | 3.5 |
changeset 3184 | 613064b49331 |
parent 2920 | 64322aa83a1d |
parent 3131 | 596f893451c0 |
child 3163 | edfe43ceaa35 |
child 4212 | ab6573088b4a |
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) |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
88 |
event = 'change_state' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
89 |
change = (event, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
90 |
(entity.wf_info_for[0], |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
91 |
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
|
92 |
changes.append(change) |
0 | 93 |
elif event == 'delete_entity': |
94 |
deleted.add(changedescr[0]) |
|
95 |
index.setdefault(event, set()).add(change) |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
96 |
for key in ('delete_relation', 'add_relation'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
97 |
for change in index.get(key, {}).copy(): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
98 |
if change[1][1] == 'in_state': |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
99 |
index[key].remove(change) |
0 | 100 |
# filter changes |
101 |
for eid in added: |
|
102 |
try: |
|
103 |
for change in index['add_relation'].copy(): |
|
104 |
changedescr = change[1] |
|
105 |
# skip meta-relations which are set automatically |
|
106 |
# XXX generate list below using rtags (category = 'generated') |
|
107 |
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
|
108 |
'from_state', 'to_state', 'by_transition', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
109 |
'wf_info_for') \ |
0 | 110 |
and changedescr[0] == eid: |
111 |
index['add_relation'].remove(change) |
|
1723 | 112 |
|
0 | 113 |
except KeyError: |
114 |
break |
|
115 |
for eid in deleted: |
|
116 |
try: |
|
117 |
for change in index['delete_relation'].copy(): |
|
118 |
fromeid, rtype, toeid = change[1] |
|
119 |
if fromeid == eid: |
|
120 |
index['delete_relation'].remove(change) |
|
121 |
elif toeid == eid: |
|
122 |
index['delete_relation'].remove(change) |
|
123 |
if rtype == 'wf_info_for': |
|
124 |
for change in index['delete_entity'].copy(): |
|
125 |
if change[1][0] == fromeid: |
|
126 |
index['delete_entity'].remove(change) |
|
127 |
except KeyError: |
|
128 |
break |
|
129 |
for change in changes: |
|
130 |
event, changedescr = change |
|
131 |
if change in index[event]: |
|
132 |
yield change |
|
133 |
||
134 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
655
diff
changeset
|
135 |
class SupervisionEmailView(Component): |
0 | 136 |
"""view implementing the email API for data changes supervision notification |
137 |
""" |
|
730 | 138 |
__select__ = none_rset() |
0 | 139 |
id = 'supervision_notif' |
140 |
||
141 |
def recipients(self): |
|
142 |
return self.config['supervising-addrs'] |
|
1723 | 143 |
|
0 | 144 |
def subject(self): |
145 |
return self.req._('[%s supervision] changes summary') % self.config.appid |
|
1723 | 146 |
|
0 | 147 |
def call(self, changes): |
148 |
user = self.req.actual_session().user |
|
149 |
self.w(self.req._('user %s has made the following change(s):\n\n') |
|
150 |
% user.login) |
|
151 |
for event, changedescr in filter_changes(changes): |
|
152 |
self.w(u'* ') |
|
153 |
getattr(self, event)(*changedescr) |
|
154 |
self.w(u'\n\n') |
|
155 |
||
156 |
def _entity_context(self, entity): |
|
157 |
return {'eid': entity.eid, |
|
158 |
'etype': entity.dc_type().lower(), |
|
159 |
'title': entity.dc_title()} |
|
1723 | 160 |
|
0 | 161 |
def add_entity(self, entity): |
162 |
msg = self.req._('added %(etype)s #%(eid)s (%(title)s)') |
|
163 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
164 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 165 |
|
0 | 166 |
def update_entity(self, entity): |
167 |
msg = self.req._('updated %(etype)s #%(eid)s (%(title)s)') |
|
168 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
169 |
# XXX print changes |
|
170 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 171 |
|
0 | 172 |
def delete_entity(self, eid, etype, title): |
173 |
msg = self.req._('deleted %(etype)s #%(eid)s (%(title)s)') |
|
174 |
etype = display_name(self.req, etype).lower() |
|
175 |
self.w(msg % locals()) |
|
1723 | 176 |
|
0 | 177 |
def change_state(self, entity, fromstate, tostate): |
178 |
msg = self.req._('changed state of %(etype)s #%(eid)s (%(title)s)') |
|
179 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
|
1723 | 180 |
self.w(_(' from state %(fromstate)s to state %(tostate)s\n' % |
0 | 181 |
{'fromstate': _(fromstate.name), 'tostate': _(tostate.name)})) |
182 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 183 |
|
0 | 184 |
def _relation_context(self, fromeid, rtype, toeid): |
185 |
_ = self.req._ |
|
186 |
session = self.req.actual_session() |
|
187 |
def describe(eid): |
|
188 |
try: |
|
189 |
return _(session.describe(eid)[0]).lower() |
|
190 |
except UnknownEid: |
|
191 |
# may occurs when an entity has been deleted from an external |
|
192 |
# source and we're cleaning its relation |
|
193 |
return _('unknown external entity') |
|
194 |
return {'rtype': _(rtype), |
|
195 |
'fromeid': fromeid, |
|
196 |
'frometype': describe(fromeid), |
|
197 |
'toeid': toeid, |
|
198 |
'toetype': describe(toeid)} |
|
1723 | 199 |
|
0 | 200 |
def add_relation(self, fromeid, rtype, toeid): |
201 |
msg = self.req._('added relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s') |
|
202 |
self.w(msg % self._relation_context(fromeid, rtype, toeid)) |
|
203 |
||
204 |
def delete_relation(self, fromeid, rtype, toeid): |
|
205 |
msg = self.req._('deleted relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s') |
|
206 |
self.w(msg % self._relation_context(fromeid, rtype, toeid)) |
|
1723 | 207 |
|
208 |
||
0 | 209 |
class SupervisionMailOp(SendMailOp): |
210 |
"""special send email operation which should be done only once for a bunch |
|
211 |
of changes |
|
212 |
""" |
|
213 |
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
|
214 |
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
|
215 |
self.session) |
1723 | 216 |
|
0 | 217 |
def _prepare_email(self): |
218 |
session = self.session |
|
219 |
config = session.vreg.config |
|
220 |
uinfo = {'email': config['sender-addr'], |
|
221 |
'name': config['sender-name']} |
|
222 |
view = self._get_view() |
|
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
223 |
content = view.render(changes=session.transaction_data.get('pendingchanges')) |
0 | 224 |
recipients = view.recipients() |
225 |
msg = format_mail(uinfo, recipients, content, view.subject(), config=config) |
|
226 |
self.to_send = [(msg, recipients)] |
|
227 |
||
228 |
def commit_event(self): |
|
229 |
self._prepare_email() |
|
230 |
SendMailOp.commit_event(self) |