author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Mon, 12 Apr 2010 15:11:23 +0200 | |
branch | stable |
changeset 5215 | b1327f19d23e |
parent 4843 | 5f7363416765 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""some hooks and views to handle supervising of any data changes |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3136
diff
changeset
|
5 |
:copyright: 2001-2010 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 |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3430
diff
changeset
|
15 |
from cubicweb.mail import format_mail |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
16 |
from cubicweb.server.hook import SendMailOp |
0 | 17 |
|
18 |
||
19 |
def filter_changes(changes): |
|
20 |
""" |
|
21 |
* when an entity has been deleted: |
|
22 |
* don't show deletion of its relations |
|
23 |
* don't show related TrInfo deletion if any |
|
24 |
* when an entity has been added don't show owned_by relation addition |
|
25 |
* don't show new TrInfo entities if any |
|
26 |
""" |
|
27 |
# first build an index of changes |
|
28 |
index = {} |
|
29 |
added, deleted = set(), set() |
|
30 |
for change in changes[:]: |
|
31 |
event, changedescr = change |
|
32 |
if event == 'add_entity': |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
33 |
entity = changedescr.entity |
0 | 34 |
added.add(entity.eid) |
35 |
if entity.e_schema == 'TrInfo': |
|
36 |
changes.remove(change) |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
37 |
event = 'change_state' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
38 |
change = (event, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
39 |
(entity.wf_info_for[0], |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
40 |
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
|
41 |
changes.append(change) |
0 | 42 |
elif event == 'delete_entity': |
43 |
deleted.add(changedescr[0]) |
|
44 |
index.setdefault(event, set()).add(change) |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
45 |
for key in ('delete_relation', 'add_relation'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
46 |
for change in index.get(key, {}).copy(): |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
47 |
if change[1].rtype == 'in_state': |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
48 |
index[key].remove(change) |
0 | 49 |
# filter changes |
50 |
for eid in added: |
|
51 |
try: |
|
52 |
for change in index['add_relation'].copy(): |
|
53 |
changedescr = change[1] |
|
54 |
# skip meta-relations which are set automatically |
|
55 |
# XXX generate list below using rtags (category = 'generated') |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
56 |
if changedescr.rtype 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
|
57 |
'from_state', 'to_state', 'by_transition', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
58 |
'wf_info_for') \ |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
59 |
and changedescr.eidfrom == eid: |
0 | 60 |
index['add_relation'].remove(change) |
61 |
except KeyError: |
|
62 |
break |
|
63 |
for eid in deleted: |
|
64 |
try: |
|
65 |
for change in index['delete_relation'].copy(): |
|
3429
531395cb5a22
[supervising] fix delete_relation() prototype and changedescr usage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
66 |
if change[1].eidfrom == eid: |
0 | 67 |
index['delete_relation'].remove(change) |
3429
531395cb5a22
[supervising] fix delete_relation() prototype and changedescr usage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
68 |
elif change[1].eidto == eid: |
0 | 69 |
index['delete_relation'].remove(change) |
3429
531395cb5a22
[supervising] fix delete_relation() prototype and changedescr usage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
70 |
if change[1].rtype == 'wf_info_for': |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
71 |
for change_ in index['delete_entity'].copy(): |
3429
531395cb5a22
[supervising] fix delete_relation() prototype and changedescr usage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
72 |
if change_[1].eidfrom == change[1].eidfrom: |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
73 |
index['delete_entity'].remove(change_) |
0 | 74 |
except KeyError: |
75 |
break |
|
76 |
for change in changes: |
|
77 |
event, changedescr = change |
|
78 |
if change in index[event]: |
|
79 |
yield change |
|
80 |
||
81 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
655
diff
changeset
|
82 |
class SupervisionEmailView(Component): |
0 | 83 |
"""view implementing the email API for data changes supervision notification |
84 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
85 |
__regid__ = 'supervision_notif' |
730 | 86 |
__select__ = none_rset() |
0 | 87 |
|
88 |
def recipients(self): |
|
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
89 |
return self._cw.vreg.config['supervising-addrs'] |
1723 | 90 |
|
0 | 91 |
def subject(self): |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
92 |
return self._cw._('[%s supervision] changes summary') % self._cw.vreg.config.appid |
1723 | 93 |
|
0 | 94 |
def call(self, changes): |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
95 |
user = self._cw.user |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
96 |
self.w(self._cw._('user %s has made the following change(s):\n\n') |
0 | 97 |
% user.login) |
98 |
for event, changedescr in filter_changes(changes): |
|
99 |
self.w(u'* ') |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
100 |
getattr(self, event)(changedescr) |
0 | 101 |
self.w(u'\n\n') |
102 |
||
103 |
def _entity_context(self, entity): |
|
104 |
return {'eid': entity.eid, |
|
105 |
'etype': entity.dc_type().lower(), |
|
106 |
'title': entity.dc_title()} |
|
1723 | 107 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
108 |
def add_entity(self, changedescr): |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
109 |
msg = self._cw._('added %(etype)s #%(eid)s (%(title)s)') |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
110 |
self.w(u'%s\n' % (msg % self._entity_context(changedescr.entity))) |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
111 |
self.w(u' %s' % changedescr.entity.absolute_url()) |
1723 | 112 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
113 |
def update_entity(self, changedescr): |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
114 |
msg = self._cw._('updated %(etype)s #%(eid)s (%(title)s)') |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
115 |
self.w(u'%s\n' % (msg % self._entity_context(changedescr.entity))) |
0 | 116 |
# XXX print changes |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
117 |
self.w(u' %s' % changedescr.entity.absolute_url()) |
1723 | 118 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
119 |
def delete_entity(self, (eid, etype, title)): |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
120 |
msg = self._cw._('deleted %(etype)s #%(eid)s (%(title)s)') |
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
121 |
etype = display_name(self._cw, etype).lower() |
0 | 122 |
self.w(msg % locals()) |
1723 | 123 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
124 |
def change_state(self, (entity, fromstate, tostate)): |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
125 |
msg = self._cw._('changed state of %(etype)s #%(eid)s (%(title)s)') |
0 | 126 |
self.w(u'%s\n' % (msg % self._entity_context(entity))) |
1723 | 127 |
self.w(_(' from state %(fromstate)s to state %(tostate)s\n' % |
0 | 128 |
{'fromstate': _(fromstate.name), 'tostate': _(tostate.name)})) |
129 |
self.w(u' %s' % entity.absolute_url()) |
|
1723 | 130 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
131 |
def _relation_context(self, changedescr): |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
132 |
session = self._cw |
0 | 133 |
def describe(eid): |
134 |
try: |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
135 |
return session._(session.describe(eid)[0]).lower() |
0 | 136 |
except UnknownEid: |
137 |
# may occurs when an entity has been deleted from an external |
|
138 |
# source and we're cleaning its relation |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
139 |
return session._('unknown external entity') |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
140 |
eidfrom, rtype, eidto = changedescr.eidfrom, changedescr.rtype, changedescr.eidto |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
141 |
return {'rtype': session._(rtype), |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
142 |
'eidfrom': eidfrom, |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
143 |
'frometype': describe(eidfrom), |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
144 |
'eidto': eidto, |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
145 |
'toetype': describe(eidto)} |
1723 | 146 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
147 |
def add_relation(self, changedescr): |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
148 |
msg = self._cw._('added relation %(rtype)s from %(frometype)s #%(eidfrom)s to %(toetype)s #%(eidto)s') |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
149 |
self.w(msg % self._relation_context(changedescr)) |
0 | 150 |
|
3429
531395cb5a22
[supervising] fix delete_relation() prototype and changedescr usage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
151 |
def delete_relation(self, changedescr): |
3430
e6d50513936c
[api] use cw_* in supervising
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3429
diff
changeset
|
152 |
msg = self._cw._('deleted relation %(rtype)s from %(frometype)s #%(eidfrom)s to %(toetype)s #%(eidto)s') |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
153 |
self.w(msg % self._relation_context(changedescr)) |
1723 | 154 |
|
155 |
||
0 | 156 |
class SupervisionMailOp(SendMailOp): |
157 |
"""special send email operation which should be done only once for a bunch |
|
158 |
of changes |
|
159 |
""" |
|
160 |
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
|
161 |
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
|
162 |
self.session) |
1723 | 163 |
|
0 | 164 |
def _prepare_email(self): |
165 |
session = self.session |
|
166 |
config = session.vreg.config |
|
167 |
uinfo = {'email': config['sender-addr'], |
|
168 |
'name': config['sender-name']} |
|
169 |
view = self._get_view() |
|
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
170 |
content = view.render(changes=session.transaction_data.get('pendingchanges')) |
0 | 171 |
recipients = view.recipients() |
172 |
msg = format_mail(uinfo, recipients, content, view.subject(), config=config) |
|
173 |
self.to_send = [(msg, recipients)] |
|
174 |
||
175 |
def commit_event(self): |
|
176 |
self._prepare_email() |
|
177 |
SendMailOp.commit_event(self) |