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