author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 12 Nov 2009 13:53:26 +0100 | |
branch | stable |
changeset 3829 | 9674d62a54f9 |
parent 3689 | deb13e88e037 |
child 3720 | 5376aaadd16b |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
"""Hooks management |
2 |
||
3 |
Hooks are called before / after any individual update of entities / relations |
|
4 |
in the repository. |
|
5 |
||
6 |
Here is the prototype of the different hooks: |
|
7 |
||
8 |
* filtered on the entity's type: |
|
9 |
||
10 |
before_add_entity (session, entity) |
|
11 |
after_add_entity (session, entity) |
|
12 |
before_update_entity (session, entity) |
|
13 |
after_update_entity (session, entity) |
|
14 |
before_delete_entity (session, eid) |
|
15 |
after_delete_entity (session, eid) |
|
16 |
||
17 |
* filtered on the relation's type: |
|
18 |
||
19 |
before_add_relation (session, fromeid, rtype, toeid) |
|
20 |
after_add_relation (session, fromeid, rtype, toeid) |
|
21 |
before_delete_relation (session, fromeid, rtype, toeid) |
|
22 |
after_delete_relation (session, fromeid, rtype, toeid) |
|
23 |
||
24 |
||
25 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
26 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 27 |
: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:
1802
diff
changeset
|
28 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 29 |
""" |
30 |
__docformat__ = "restructuredtext en" |
|
31 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
32 |
ENTITIES_HOOKS = ('before_add_entity', 'after_add_entity', |
0 | 33 |
'before_update_entity', 'after_update_entity', |
34 |
'before_delete_entity', 'after_delete_entity') |
|
35 |
RELATIONS_HOOKS = ('before_add_relation', 'after_add_relation' , |
|
36 |
'before_delete_relation','after_delete_relation') |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2492
diff
changeset
|
37 |
SYSTEM_HOOKS = ('server_backup', 'server_restore', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2492
diff
changeset
|
38 |
'server_startup', 'server_shutdown', |
0 | 39 |
'session_open', 'session_close') |
40 |
||
41 |
ALL_HOOKS = frozenset(ENTITIES_HOOKS + RELATIONS_HOOKS + SYSTEM_HOOKS) |
|
42 |
||
43 |
class HooksManager(object): |
|
44 |
"""handle hooks registration and calls |
|
45 |
""" |
|
46 |
verification_hooks_activated = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
47 |
|
0 | 48 |
def __init__(self, schema): |
49 |
self.set_schema(schema) |
|
50 |
||
51 |
def set_schema(self, schema): |
|
52 |
self._hooks = {} |
|
53 |
self.schema = schema |
|
54 |
self._init_hooks(schema) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
55 |
|
0 | 56 |
def register_hooks(self, hooks): |
57 |
"""register a dictionary of hooks : |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
58 |
|
0 | 59 |
{'event': {'entity or relation type': [callbacks list]}} |
60 |
""" |
|
61 |
for event, subevents in hooks.items(): |
|
62 |
for subevent, callbacks in subevents.items(): |
|
63 |
for callback in callbacks: |
|
64 |
self.register_hook(callback, event, subevent) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
65 |
|
0 | 66 |
def register_hook(self, function, event, etype=''): |
67 |
"""register a function to call when <event> occurs |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
68 |
|
2579
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
69 |
<etype> is an entity/relation type or an empty string. |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
70 |
|
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
71 |
If etype is the empty string, the function will be called at each event, |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
72 |
else the function will be called only when event occurs on an entity or |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
73 |
relation of the given type. |
0 | 74 |
""" |
75 |
assert event in ALL_HOOKS, '%r NOT IN %r' % (event, ALL_HOOKS) |
|
76 |
assert (not event in SYSTEM_HOOKS or not etype), (event, etype) |
|
77 |
etype = etype or '' |
|
78 |
try: |
|
79 |
self._hooks[event][etype].append(function) |
|
80 |
self.debug('registered hook %s on %s (%s)', event, etype or 'any', |
|
81 |
function.func_name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
82 |
|
0 | 83 |
except KeyError: |
84 |
self.error('can\'t register hook %s on %s (%s)', |
|
85 |
event, etype or 'any', function.func_name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
86 |
|
2579
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
87 |
def unregister_hook(self, function_or_cls, event=None, etype=''): |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
88 |
"""unregister a function to call when <event> occurs, or a Hook subclass. |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
89 |
In the later case, event/type information are extracted from the given |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
90 |
class. |
0 | 91 |
""" |
2579
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
92 |
if isinstance(function_or_cls, type) and issubclass(function_or_cls, Hook): |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
93 |
for event, ertype in function_or_cls.register_to(): |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
94 |
for hook in self._hooks[event][ertype]: |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
95 |
if getattr(hook, 'im_self', None).__class__ is function_or_cls: |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
96 |
self._hooks[event][ertype].remove(hook) |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
97 |
self.info('unregister hook %s on %s (%s)', event, etype, |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
98 |
function_or_cls.__name__) |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
99 |
break |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
100 |
else: |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
101 |
self.warning("can't unregister hook %s on %s (%s), not found", |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
102 |
event, etype, function_or_cls.__name__) |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
103 |
else: |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
104 |
assert event in ALL_HOOKS, event |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
105 |
etype = etype or '' |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
106 |
self.info('unregister hook %s on %s (%s)', event, etype, |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
107 |
function_or_cls.func_name) |
e69b2342bd8b
[hooksmanager] nicer unregister_hook implementation, allowing to give a Hook subclass as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
108 |
self._hooks[event][etype].remove(function_or_cls) |
0 | 109 |
|
110 |
def call_hooks(self, __event, __type='', *args, **kwargs): |
|
111 |
"""call hook matching event and optional type""" |
|
112 |
if __type: |
|
113 |
self.info('calling hooks for event %s (%s)', __event, __type) |
|
114 |
else: |
|
115 |
self.info('calling hooks for event %s', __event) |
|
116 |
# call generic hooks first |
|
117 |
for hook in self._hooks[__event]['']: |
|
118 |
#print '[generic]', hook.__name__ |
|
119 |
hook(*args, **kwargs) |
|
120 |
if __type: |
|
121 |
for hook in self._hooks[__event][__type]: |
|
122 |
#print '[%s]'%__type, hook.__name__ |
|
123 |
hook(*args, **kwargs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
124 |
|
0 | 125 |
def _init_hooks(self, schema): |
126 |
"""initialize the hooks map""" |
|
127 |
for hook_event in ENTITIES_HOOKS: |
|
128 |
self._hooks[hook_event] = {'': []} |
|
129 |
for etype in schema.entities(): |
|
130 |
self._hooks[hook_event][etype] = [] |
|
131 |
for hook_event in RELATIONS_HOOKS: |
|
132 |
self._hooks[hook_event] = {'': []} |
|
133 |
for r_type in schema.relations(): |
|
134 |
self._hooks[hook_event][r_type] = [] |
|
135 |
for hook_event in SYSTEM_HOOKS: |
|
136 |
self._hooks[hook_event] = {'': []} |
|
137 |
||
138 |
def register_system_hooks(self, config): |
|
139 |
"""register system hooks according to the configuration""" |
|
140 |
self.info('register core hooks') |
|
141 |
from cubicweb.server.hooks import _register_metadata_hooks, _register_wf_hooks |
|
142 |
_register_metadata_hooks(self) |
|
143 |
self.info('register workflow hooks') |
|
144 |
_register_wf_hooks(self) |
|
145 |
if config.core_hooks: |
|
146 |
from cubicweb.server.hooks import _register_core_hooks |
|
147 |
_register_core_hooks(self) |
|
148 |
if config.schema_hooks: |
|
149 |
from cubicweb.server.schemahooks import _register_schema_hooks |
|
150 |
self.info('register schema hooks') |
|
151 |
_register_schema_hooks(self) |
|
152 |
if config.usergroup_hooks: |
|
153 |
from cubicweb.server.hooks import _register_usergroup_hooks |
|
154 |
from cubicweb.server.hooks import _register_eproperty_hooks |
|
155 |
self.info('register user/group hooks') |
|
156 |
_register_usergroup_hooks(self) |
|
157 |
_register_eproperty_hooks(self) |
|
158 |
if config.security_hooks: |
|
159 |
from cubicweb.server.securityhooks import register_security_hooks |
|
160 |
self.info('register security hooks') |
|
161 |
register_security_hooks(self) |
|
162 |
if not self.verification_hooks_activated: |
|
163 |
self.deactivate_verification_hooks() |
|
164 |
||
165 |
def deactivate_verification_hooks(self): |
|
166 |
from cubicweb.server.hooks import (cardinalitycheck_after_add_entity, |
|
167 |
cardinalitycheck_before_del_relation, |
|
168 |
cstrcheck_after_add_relation, |
|
169 |
uniquecstrcheck_before_modification) |
|
170 |
self.warning('deactivating verification hooks') |
|
171 |
self.verification_hooks_activated = False |
|
172 |
self.unregister_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
173 |
self.unregister_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
174 |
self.unregister_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
175 |
self.unregister_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
176 |
self.unregister_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
177 |
# self.unregister_hook(tidy_html_fields('before_add_entity'), 'before_add_entity', '') |
|
178 |
# self.unregister_hook(tidy_html_fields('before_update_entity'), 'before_update_entity', '') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
179 |
|
0 | 180 |
def reactivate_verification_hooks(self): |
181 |
from cubicweb.server.hooks import (cardinalitycheck_after_add_entity, |
|
182 |
cardinalitycheck_before_del_relation, |
|
183 |
cstrcheck_after_add_relation, |
|
184 |
uniquecstrcheck_before_modification) |
|
185 |
self.warning('reactivating verification hooks') |
|
186 |
self.verification_hooks_activated = True |
|
187 |
self.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
188 |
self.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
189 |
self.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
190 |
self.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
191 |
self.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
192 |
# self.register_hook(tidy_html_fields('before_add_entity'), 'before_add_entity', '') |
|
193 |
# self.register_hook(tidy_html_fields('before_update_entity'), 'before_update_entity', '') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
194 |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
672
diff
changeset
|
195 |
from cubicweb.selectors import yes |
722 | 196 |
from cubicweb.appobject import AppObject |
0 | 197 |
|
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
198 |
class autoid(type): |
0 | 199 |
"""metaclass to create an unique 'id' attribute on the class using it""" |
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
200 |
# XXX is this metaclass really necessary ? |
0 | 201 |
def __new__(mcs, name, bases, classdict): |
202 |
cls = super(autoid, mcs).__new__(mcs, name, bases, classdict) |
|
203 |
cls.id = str(id(cls)) |
|
204 |
return cls |
|
205 |
||
206 |
class Hook(AppObject): |
|
207 |
__metaclass__ = autoid |
|
208 |
__registry__ = 'hooks' |
|
730 | 209 |
__select__ = yes() |
0 | 210 |
# set this in derivated classes |
211 |
events = None |
|
212 |
accepts = None |
|
213 |
enabled = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
214 |
|
0 | 215 |
def __init__(self, event=None): |
216 |
super(Hook, self).__init__() |
|
217 |
self.event = event |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
218 |
|
0 | 219 |
@classmethod |
220 |
def registered(cls, vreg): |
|
221 |
super(Hook, cls).registered(vreg) |
|
222 |
return cls() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
223 |
|
0 | 224 |
@classmethod |
225 |
def register_to(cls): |
|
226 |
if not cls.enabled: |
|
227 |
cls.warning('%s hook has been disabled', cls) |
|
228 |
return |
|
229 |
done = set() |
|
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
230 |
assert isinstance(cls.events, (tuple, list)), \ |
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
231 |
'%s: events is expected to be a tuple, not %s' % ( |
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
232 |
cls, type(cls.events)) |
0 | 233 |
for event in cls.events: |
2492
c51be1cf8317
should check for every system hooks, not only server_startup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2452
diff
changeset
|
234 |
if event in SYSTEM_HOOKS: |
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
235 |
assert not cls.accepts or cls.accepts == ('Any',), \ |
2492
c51be1cf8317
should check for every system hooks, not only server_startup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2452
diff
changeset
|
236 |
'%s doesnt make sense on %s' % (cls.accepts, event) |
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
237 |
cls.accepts = ('Any',) |
0 | 238 |
for ertype in cls.accepts: |
239 |
if (event, ertype) in done: |
|
240 |
continue |
|
241 |
yield event, ertype |
|
242 |
done.add((event, ertype)) |
|
243 |
try: |
|
244 |
eschema = cls.schema.eschema(ertype) |
|
245 |
except KeyError: |
|
246 |
# relation schema |
|
247 |
pass |
|
248 |
else: |
|
249 |
for eetype in eschema.specialized_by(): |
|
250 |
if (event, eetype) in done: |
|
251 |
continue |
|
252 |
yield event, str(eetype) |
|
253 |
done.add((event, eetype)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
254 |
|
0 | 255 |
|
256 |
def make_callback(self, event): |
|
257 |
if len(self.events) == 1: |
|
258 |
return self.call |
|
259 |
return self.__class__(event=event).call |
|
260 |
||
261 |
def call(self): |
|
262 |
raise NotImplementedError |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
263 |
|
0 | 264 |
class SystemHook(Hook): |
2452
868e0c75a57d
fix SystemHook declaration, accepts=('',) doesn't mean anything
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2350
diff
changeset
|
265 |
accepts = () |
0 | 266 |
|
267 |
from logging import getLogger |
|
268 |
from cubicweb import set_log_methods |
|
269 |
set_log_methods(HooksManager, getLogger('cubicweb.hooksmanager')) |
|
270 |
set_log_methods(Hook, getLogger('cubicweb.hooks')) |
|
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
271 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
272 |
# base classes for relation propagation ######################################## |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
273 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
274 |
from cubicweb.server.pool import PreCommitOperation |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
275 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
276 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
277 |
class PropagateSubjectRelationHook(Hook): |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
278 |
"""propagate permissions and nosy list when new entity are added""" |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
279 |
events = ('after_add_relation',) |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
280 |
# to set in concrete class |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
281 |
rtype = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
282 |
subject_relations = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
283 |
object_relations = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
284 |
accepts = None # subject_relations + object_relations |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
285 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
286 |
def call(self, session, fromeid, rtype, toeid): |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
287 |
for eid in (fromeid, toeid): |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
288 |
etype = session.describe(eid)[0] |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3088
diff
changeset
|
289 |
if self.rtype not in self.schema.eschema(etype).subjrels: |
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
290 |
return |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
291 |
if rtype in self.subject_relations: |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
292 |
meid, seid = fromeid, toeid |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
293 |
else: |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
294 |
assert rtype in self.object_relations |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
295 |
meid, seid = toeid, fromeid |
3088
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
296 |
session.unsafe_execute( |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
297 |
'SET E %s P WHERE X %s P, X eid %%(x)s, E eid %%(e)s, NOT E %s P'\ |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
298 |
% (self.rtype, self.rtype, self.rtype), |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
299 |
{'x': meid, 'e': seid}, ('x', 'e')) |
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
300 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
301 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
302 |
class PropagateSubjectRelationAddHook(Hook): |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
303 |
"""propagate on existing entities when a permission or nosy list is added""" |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
304 |
events = ('after_add_relation',) |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
305 |
# to set in concrete class |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
306 |
rtype = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
307 |
subject_relations = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
308 |
object_relations = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
309 |
accepts = None # (self.rtype,) |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
310 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
311 |
def call(self, session, fromeid, rtype, toeid): |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
312 |
eschema = self.schema.eschema(session.describe(fromeid)[0]) |
3088
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
313 |
execute = session.unsafe_execute |
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
314 |
for rel in self.subject_relations: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3088
diff
changeset
|
315 |
if rel in eschema.subjrels: |
3088
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
316 |
execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
317 |
'X %s R, NOT R %s P' % (rtype, rel, rtype), |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
318 |
{'x': fromeid, 'p': toeid}, 'x') |
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
319 |
for rel in self.object_relations: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3088
diff
changeset
|
320 |
if rel in eschema.objrels: |
3088
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
321 |
execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
322 |
'R %s X, NOT R %s P' % (rtype, rel, rtype), |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
323 |
{'x': fromeid, 'p': toeid}, 'x') |
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
324 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
325 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
326 |
class PropagateSubjectRelationDelHook(Hook): |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
327 |
"""propagate on existing entities when a permission is deleted""" |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
328 |
events = ('after_delete_relation',) |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
329 |
# to set in concrete class |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
330 |
rtype = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
331 |
subject_relations = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
332 |
object_relations = None |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
333 |
accepts = None # (self.rtype,) |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
334 |
|
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
335 |
def call(self, session, fromeid, rtype, toeid): |
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
336 |
eschema = self.schema.eschema(session.describe(fromeid)[0]) |
3088
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
337 |
execute = session.unsafe_execute |
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
338 |
for rel in self.subject_relations: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3088
diff
changeset
|
339 |
if rel in eschema.subjrels: |
3088
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
340 |
execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
341 |
'X %s R' % (rtype, rel), |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
342 |
{'x': fromeid, 'p': toeid}, 'x') |
2918
452b4c9ee61d
work in progress: backport some generic relation propagation hooks from tracker
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2579
diff
changeset
|
343 |
for rel in self.object_relations: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3088
diff
changeset
|
344 |
if rel in eschema.objrels: |
3088
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
345 |
execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
346 |
'R %s X' % (rtype, rel), |
854a30d8c092
do not delay sync queries to an operation, it may cause pb (nosy list propagation for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2918
diff
changeset
|
347 |
{'x': fromeid, 'p': toeid}, 'x') |