author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 25 Sep 2009 11:50:22 +0200 | |
changeset 3477 | 8a9e26b93bb7 |
parent 3423 | d1356e008b19 |
child 3659 | 993997b4b41d |
permissions | -rw-r--r-- |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
1 |
"""Hooks management |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
2 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
3 |
This module defined the `Hook` class and registry and a set of abstract classes |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
4 |
for operations. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
5 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
6 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
7 |
Hooks are called before / after any individual update of entities / relations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
8 |
in the repository and on special events such as server startup or shutdown. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
9 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
10 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
11 |
Operations may be registered by hooks during a transaction, which will be |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
12 |
fired when the pool is commited or rollbacked. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
13 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
14 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
15 |
Entity hooks (eg before_add_entity, after_add_entity, before_update_entity, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
16 |
after_update_entity, before_delete_entity, after_delete_entity) all have an |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
17 |
`entity` attribute |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
18 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
19 |
Relation (eg before_add_relation, after_add_relation, before_delete_relation, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
20 |
after_delete_relation) all have `eidfrom`, `rtype`, `eidto` attributes. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
21 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
22 |
Server start/stop hooks (eg server_startup, server_shutdown) have a `repo` |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
23 |
attribute, but *their `_cw` attribute is None*. |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
24 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
25 |
Backup/restore hooks (eg server_backup, server_restore) have a `repo` and a |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
26 |
`timestamp` attributes, but *their `_cw` attribute is None*. |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
27 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
28 |
Session hooks (eg session_open, session_close) have no special attribute. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
29 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
30 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
31 |
:organization: Logilab |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
32 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
33 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
34 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
35 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
36 |
__docformat__ = "restructuredtext en" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
37 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
38 |
from warnings import warn |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
39 |
from logging import getLogger |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
40 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
41 |
from logilab.common.decorators import classproperty |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
42 |
from logilab.common.deprecation import deprecated |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
43 |
from logilab.common.logging_ext import set_log_methods |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
44 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
45 |
from cubicweb.cwvreg import CWRegistry, VRegistry |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
46 |
from cubicweb.selectors import (objectify_selector, lltrace, match_search_state, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
47 |
entity_implements) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
48 |
from cubicweb.appobject import AppObject |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
49 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
50 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
51 |
ENTITIES_HOOKS = set(('before_add_entity', 'after_add_entity', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
52 |
'before_update_entity', 'after_update_entity', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
53 |
'before_delete_entity', 'after_delete_entity')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
54 |
RELATIONS_HOOKS = set(('before_add_relation', 'after_add_relation' , |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
55 |
'before_delete_relation','after_delete_relation')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
56 |
SYSTEM_HOOKS = set(('server_backup', 'server_restore', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
57 |
'server_startup', 'server_shutdown', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
58 |
'session_open', 'session_close')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
59 |
ALL_HOOKS = ENTITIES_HOOKS | RELATIONS_HOOKS | SYSTEM_HOOKS |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
60 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
61 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
62 |
class HooksRegistry(CWRegistry): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
63 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
64 |
def register(self, obj, **kwargs): |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
65 |
try: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
66 |
iter(obj.events) |
3394
51a25bdd7bdc
[hooks] fix check for .events attribute in hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3195
diff
changeset
|
67 |
except AttributeError: |
51a25bdd7bdc
[hooks] fix check for .events attribute in hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3195
diff
changeset
|
68 |
raise |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
69 |
except: |
3415
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
70 |
raise Exception('bad .events attribute %s on %s.%s' % ( |
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
71 |
obj.events, obj.__module__, obj.__name__)) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
72 |
for event in obj.events: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
73 |
if event not in ALL_HOOKS: |
3415
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
74 |
raise Exception('bad event %s on %s.%s' % ( |
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
75 |
event, obj.__module__, obj.__name__)) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
76 |
super(HooksRegistry, self).register(obj, **kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
77 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
78 |
def call_hooks(self, event, req=None, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
79 |
kwargs['event'] = event |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
80 |
for hook in sorted(self.possible_objects(req, **kwargs), key=lambda x: x.order): |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
81 |
if hook.enabled: |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
82 |
hook() |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
83 |
else: |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
84 |
warn('[3.6] %s: enabled is deprecated' % cls) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
85 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
86 |
VRegistry.REGISTRY_FACTORY['hooks'] = HooksRegistry |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
87 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
88 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
89 |
# some hook specific selectors ################################################# |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
90 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
91 |
@objectify_selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
92 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
93 |
def match_event(cls, req, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
94 |
if kwargs.get('event') in cls.events: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
95 |
return 1 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
96 |
return 0 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
97 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
98 |
@objectify_selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
99 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
100 |
def enabled_category(cls, req, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
101 |
if req is None: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
102 |
# server startup / shutdown event |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
103 |
config = kwargs['repo'].config |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
104 |
else: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
105 |
config = req.vreg.config |
3416
d74c627981e1
fix hook category selector
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3415
diff
changeset
|
106 |
if cls.category in config.disabled_hooks_categories: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
107 |
return 0 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
108 |
return 1 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
109 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
110 |
@objectify_selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
111 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
112 |
def regular_session(cls, req, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
113 |
if req is None or req.is_super_session: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
114 |
return 0 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
115 |
return 1 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
116 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
117 |
class match_rtype(match_search_state): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
118 |
"""accept if parameters specified as initializer arguments are specified |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
119 |
in named arguments given to the selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
120 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
121 |
:param *expected: parameters (eg `basestring`) which are expected to be |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
122 |
found in named arguments (kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
123 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
124 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
125 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
126 |
def __call__(self, cls, req, *args, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
127 |
return kwargs.get('rtype') in self.expected |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
128 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
129 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
130 |
# base class for hook ########################################################## |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
131 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
132 |
class Hook(AppObject): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
133 |
__registry__ = 'hooks' |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
134 |
__select__ = match_event() & enabled_category() |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
135 |
# set this in derivated classes |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
136 |
events = None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
137 |
category = None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
138 |
order = 0 |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
139 |
# XXX deprecated |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
140 |
enabled = True |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
141 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
142 |
@classproperty |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3195
diff
changeset
|
143 |
def __regid__(cls): |
3383
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
144 |
warn('[3.6] %s.%s: please specify an id for your hook' |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
145 |
% (cls.__module__, cls.__name__), DeprecationWarning) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
146 |
return str(id(cls)) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
147 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
148 |
@classmethod |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
149 |
def __registered__(cls, vreg): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
150 |
super(Hook, cls).__registered__(vreg) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
151 |
if getattr(cls, 'accepts', None): |
3383
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
152 |
warn('[3.6] %s.%s: accepts is deprecated, define proper __select__' |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
153 |
% (cls.__module__, cls.__name__), DeprecationWarning) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
154 |
rtypes = [] |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
155 |
for ertype in cls.accepts: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
156 |
if ertype.islower(): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
157 |
rtypes.append(ertype) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
158 |
else: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
159 |
cls.__select__ = cls.__select__ & entity_implements(ertype) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
160 |
if rtypes: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
161 |
cls.__select__ = cls.__select__ & match_rtype(*rtypes) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
162 |
return cls |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
163 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
164 |
known_args = set(('entity', 'rtype', 'eidfrom', 'eidto', 'repo', 'timestamp')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
165 |
def __init__(self, req, event, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
166 |
for arg in self.known_args: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
167 |
if arg in kwargs: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
168 |
setattr(self, arg, kwargs.pop(arg)) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
169 |
super(Hook, self).__init__(req, **kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
170 |
self.event = event |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
171 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
172 |
def __call__(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
173 |
if hasattr(self, 'call'): |
3383
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
174 |
cls = self.__class__ |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
175 |
warn('[3.6] %s.%s: call is deprecated, implements __call__' |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
176 |
% (cls.__module__, cls.__name__), DeprecationWarning) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
177 |
if self.event.endswith('_relation'): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
178 |
self.call(self._cw, self.eidfrom, self.rtype, self.eidto) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
179 |
elif 'delete' in self.event: |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
180 |
self.call(self._cw, self.entity.eid) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
181 |
elif self.event.startswith('server_'): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
182 |
self.call(self.repo) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
183 |
elif self.event.startswith('session_'): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
184 |
self.call(self._cw) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
185 |
else: |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
186 |
self.call(self._cw, self.entity) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
187 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
188 |
set_log_methods(Hook, getLogger('cubicweb.hook')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
189 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
190 |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
191 |
# base classes for relation propagation ######################################## |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
192 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
193 |
class PropagateSubjectRelationHook(Hook): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
194 |
"""propagate permissions and nosy list when new entity are added""" |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
195 |
events = ('after_add_relation',) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
196 |
# to set in concrete class |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
197 |
rtype = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
198 |
subject_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
199 |
object_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
200 |
accepts = None # subject_relations + object_relations |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
201 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
202 |
def call(self, session, fromeid, rtype, toeid): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
203 |
for eid in (fromeid, toeid): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
204 |
etype = session.describe(eid)[0] |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
205 |
if not self.schema.eschema(etype).has_subject_relation(self.rtype): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
206 |
return |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
207 |
if rtype in self.subject_relations: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
208 |
meid, seid = fromeid, toeid |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
209 |
else: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
210 |
assert rtype in self.object_relations |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
211 |
meid, seid = toeid, fromeid |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
212 |
session.unsafe_execute( |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
213 |
'SET E %s P WHERE X %s P, X eid %%(x)s, E eid %%(e)s, NOT E %s P'\ |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
214 |
% (self.rtype, self.rtype, self.rtype), |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
215 |
{'x': meid, 'e': seid}, ('x', 'e')) |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
216 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
217 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
218 |
class PropagateSubjectRelationAddHook(Hook): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
219 |
"""propagate on existing entities when a permission or nosy list is added""" |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
220 |
events = ('after_add_relation',) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
221 |
# to set in concrete class |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
222 |
rtype = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
223 |
subject_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
224 |
object_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
225 |
accepts = None # (self.rtype,) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
226 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
227 |
def call(self, session, fromeid, rtype, toeid): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
228 |
eschema = self.schema.eschema(session.describe(fromeid)[0]) |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
229 |
execute = session.unsafe_execute |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
230 |
for rel in self.subject_relations: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
231 |
if eschema.has_subject_relation(rel): |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
232 |
execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
233 |
'X %s R, NOT R %s P' % (rtype, rel, rtype), |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
234 |
{'x': fromeid, 'p': toeid}, 'x') |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
235 |
for rel in self.object_relations: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
236 |
if eschema.has_object_relation(rel): |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
237 |
execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
238 |
'R %s X, NOT R %s P' % (rtype, rel, rtype), |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
239 |
{'x': fromeid, 'p': toeid}, 'x') |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
240 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
241 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
242 |
class PropagateSubjectRelationDelHook(Hook): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
243 |
"""propagate on existing entities when a permission is deleted""" |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
244 |
events = ('after_delete_relation',) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
245 |
# to set in concrete class |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
246 |
rtype = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
247 |
subject_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
248 |
object_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
249 |
accepts = None # (self.rtype,) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
250 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
251 |
def call(self, session, fromeid, rtype, toeid): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
252 |
eschema = self.schema.eschema(session.describe(fromeid)[0]) |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
253 |
execute = session.unsafe_execute |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
254 |
for rel in self.subject_relations: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
255 |
if eschema.has_subject_relation(rel): |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
256 |
execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
257 |
'X %s R' % (rtype, rel), |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
258 |
{'x': fromeid, 'p': toeid}, 'x') |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
259 |
for rel in self.object_relations: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
260 |
if eschema.has_object_relation(rel): |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
261 |
execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
262 |
'R %s X' % (rtype, rel), |
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
263 |
{'x': fromeid, 'p': toeid}, 'x') |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
264 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
265 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
266 |
# abstract classes for operation ############################################### |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
267 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
268 |
class Operation(object): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
269 |
"""an operation is triggered on connections pool events related to |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
270 |
commit / rollback transations. Possible events are: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
271 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
272 |
precommit: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
273 |
the pool is preparing to commit. You shouldn't do anything things which |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
274 |
has to be reverted if the commit fail at this point, but you can freely |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
275 |
do any heavy computation or raise an exception if the commit can't go. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
276 |
You can add some new operation during this phase but their precommit |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
277 |
event won't be triggered |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
278 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
279 |
commit: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
280 |
the pool is preparing to commit. You should avoid to do to expensive |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
281 |
stuff or something that may cause an exception in this event |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
282 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
283 |
revertcommit: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
284 |
if an operation failed while commited, this event is triggered for |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
285 |
all operations which had their commit event already to let them |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
286 |
revert things (including the operation which made fail the commit) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
287 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
288 |
rollback: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
289 |
the transaction has been either rollbacked either |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
290 |
* intentionaly |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
291 |
* a precommit event failed, all operations are rollbacked |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
292 |
* a commit event failed, all operations which are not been triggered for |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
293 |
commit are rollbacked |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
294 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
295 |
order of operations may be important, and is controlled according to: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
296 |
* operation's class |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
297 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
298 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
299 |
def __init__(self, session, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
300 |
self.session = session |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
301 |
self.__dict__.update(kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
302 |
self.register(session) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
303 |
# execution information |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
304 |
self.processed = None # 'precommit', 'commit' |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
305 |
self.failed = False |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
306 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
307 |
def register(self, session): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
308 |
session.add_operation(self, self.insert_index()) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
309 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
310 |
def insert_index(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
311 |
"""return the index of the lastest instance which is not a |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
312 |
LateOperation instance |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
313 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
314 |
for i, op in enumerate(self.session.pending_operations): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
315 |
if isinstance(op, (LateOperation, SingleLastOperation)): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
316 |
return i |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
317 |
return None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
318 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
319 |
def handle_event(self, event): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
320 |
"""delegate event handling to the opertaion""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
321 |
getattr(self, event)() |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
322 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
323 |
def precommit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
324 |
"""the observed connections pool is preparing a commit""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
325 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
326 |
def revertprecommit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
327 |
"""an error went when pre-commiting this operation or a later one |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
328 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
329 |
should revert pre-commit's changes but take care, they may have not |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
330 |
been all considered if it's this operation which failed |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
331 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
332 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
333 |
def commit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
334 |
"""the observed connections pool is commiting""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
335 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
336 |
def revertcommit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
337 |
"""an error went when commiting this operation or a later one |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
338 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
339 |
should revert commit's changes but take care, they may have not |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
340 |
been all considered if it's this operation which failed |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
341 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
342 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
343 |
def rollback_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
344 |
"""the observed connections pool has been rollbacked |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
345 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
346 |
do nothing by default, the operation will just be removed from the pool |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
347 |
operation list |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
348 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
349 |
|
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
350 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
351 |
@deprecated('[3.6] use self.session.user') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
352 |
def user(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
353 |
return self.session.user |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
354 |
|
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
355 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
356 |
@deprecated('[3.6] use self.session.repo') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
357 |
def repo(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
358 |
return self.session.repo |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
359 |
|
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
360 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
361 |
@deprecated('[3.6] use self.session.vreg.schema') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
362 |
def schema(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
363 |
return self.session.repo.schema |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
364 |
|
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
365 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
366 |
@deprecated('[3.6] use self.session.vreg.config') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
367 |
def config(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
368 |
return self.session.repo.config |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
369 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
370 |
set_log_methods(Operation, getLogger('cubicweb.session')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
371 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
372 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
373 |
class LateOperation(Operation): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
374 |
"""special operation which should be called after all possible (ie non late) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
375 |
operations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
376 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
377 |
def insert_index(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
378 |
"""return the index of the lastest instance which is not a |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
379 |
SingleLastOperation instance |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
380 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
381 |
for i, op in enumerate(self.session.pending_operations): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
382 |
if isinstance(op, SingleLastOperation): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
383 |
return i |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
384 |
return None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
385 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
386 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
387 |
class SingleOperation(Operation): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
388 |
"""special operation which should be called once""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
389 |
def register(self, session): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
390 |
"""override register to handle cases where this operation has already |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
391 |
been added |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
392 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
393 |
operations = session.pending_operations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
394 |
index = self.equivalent_index(operations) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
395 |
if index is not None: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
396 |
equivalent = operations.pop(index) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
397 |
else: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
398 |
equivalent = None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
399 |
session.add_operation(self, self.insert_index()) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
400 |
return equivalent |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
401 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
402 |
def equivalent_index(self, operations): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
403 |
"""return the index of the equivalent operation if any""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
404 |
equivalents = [i for i, op in enumerate(operations) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
405 |
if op.__class__ is self.__class__] |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
406 |
if equivalents: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
407 |
return equivalents[0] |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
408 |
return None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
409 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
410 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
411 |
class SingleLastOperation(SingleOperation): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
412 |
"""special operation which should be called once and after all other |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
413 |
operations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
414 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
415 |
def insert_index(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
416 |
return None |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
417 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
418 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
419 |
class SendMailOp(SingleLastOperation): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
420 |
def __init__(self, session, msg=None, recipients=None, **kwargs): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
421 |
# may not specify msg yet, as |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
422 |
# `cubicweb.sobjects.supervision.SupervisionMailOp` |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
423 |
if msg is not None: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
424 |
assert recipients |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
425 |
self.to_send = [(msg, recipients)] |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
426 |
else: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
427 |
assert recipients is None |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
428 |
self.to_send = [] |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
429 |
super(SendMailOp, self).__init__(session, **kwargs) |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
430 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
431 |
def register(self, session): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
432 |
previous = super(SendMailOp, self).register(session) |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
433 |
if previous: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
434 |
self.to_send = previous.to_send + self.to_send |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
435 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
436 |
def commit_event(self): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3396
diff
changeset
|
437 |
self.session.repo.threaded_task(self.sendmails) |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
438 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
439 |
def sendmails(self): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3396
diff
changeset
|
440 |
self.session.vreg.config.sendmails(self.to_send) |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
441 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
442 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
443 |
class RQLPrecommitOperation(Operation): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
444 |
def precommit_event(self): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
445 |
execute = self.session.unsafe_execute |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
446 |
for rql in self.rqls: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
447 |
execute(*rql) |