author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 13:42:48 +0200 | |
changeset 3433 | 401ea5e03e9d |
parent 3377 | dd9d292b6a6d |
child 3429 | 531395cb5a22 |
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.hookhelper import SendMailOp |
|
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(): |
|
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
|
66 |
if change.eidfrom == eid: |
0 | 67 |
index['delete_relation'].remove(change) |
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
|
68 |
elif change.eidto == eid: |
0 | 69 |
index['delete_relation'].remove(change) |
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
|
70 |
if change.rtype == 'wf_info_for': |
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(): |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
72 |
if change_[1].eidfrom == change.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
|
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): |
|
89 |
return self.config['supervising-addrs'] |
|
1723 | 90 |
|
0 | 91 |
def subject(self): |
92 |
return self.req._('[%s supervision] changes summary') % self.config.appid |
|
1723 | 93 |
|
0 | 94 |
def call(self, changes): |
95 |
user = self.req.actual_session().user |
|
96 |
self.w(self.req._('user %s has made the following change(s):\n\n') |
|
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): |
0 | 109 |
msg = self.req._('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): |
0 | 114 |
msg = self.req._('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)): |
0 | 120 |
msg = self.req._('deleted %(etype)s #%(eid)s (%(title)s)') |
121 |
etype = display_name(self.req, etype).lower() |
|
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)): |
0 | 125 |
msg = self.req._('changed state of %(etype)s #%(eid)s (%(title)s)') |
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): |
0 | 132 |
_ = self.req._ |
133 |
session = self.req.actual_session() |
|
134 |
def describe(eid): |
|
135 |
try: |
|
136 |
return _(session.describe(eid)[0]).lower() |
|
137 |
except UnknownEid: |
|
138 |
# may occurs when an entity has been deleted from an external |
|
139 |
# source and we're cleaning its relation |
|
140 |
return _('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
|
141 |
eidfrom, rtype, eidto = changedescr.eidfrom, changedescr.rtype, changedescr.eidto |
0 | 142 |
return {'rtype': _(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
|
143 |
'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
|
144 |
'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
|
145 |
'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
|
146 |
'toetype': describe(eidto)} |
1723 | 147 |
|
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
|
148 |
def add_relation(self, changedescr): |
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 |
msg = self.req._('added relation %(rtype)s from %(frometype)s #%(eidfrom)s to %(toetype)s #%(eidto)s') |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
150 |
self.w(msg % self._relation_context(changedescr)) |
0 | 151 |
|
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
|
152 |
def delete_relation(self, eidfrom, rtype, 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
|
153 |
msg = self.req._('deleted relation %(rtype)s from %(frometype)s #%(eidfrom)s to %(toetype)s #%(eidto)s') |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2812
diff
changeset
|
154 |
self.w(msg % self._relation_context(changedescr)) |
1723 | 155 |
|
156 |
||
0 | 157 |
class SupervisionMailOp(SendMailOp): |
158 |
"""special send email operation which should be done only once for a bunch |
|
159 |
of changes |
|
160 |
""" |
|
161 |
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
|
162 |
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
|
163 |
self.session) |
1723 | 164 |
|
0 | 165 |
def _prepare_email(self): |
166 |
session = self.session |
|
167 |
config = session.vreg.config |
|
168 |
uinfo = {'email': config['sender-addr'], |
|
169 |
'name': config['sender-name']} |
|
170 |
view = self._get_view() |
|
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
171 |
content = view.render(changes=session.transaction_data.get('pendingchanges')) |
0 | 172 |
recipients = view.recipients() |
173 |
msg = format_mail(uinfo, recipients, content, view.subject(), config=config) |
|
174 |
self.to_send = [(msg, recipients)] |
|
175 |
||
176 |
def commit_event(self): |
|
177 |
self._prepare_email() |
|
178 |
SendMailOp.commit_event(self) |