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