author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 10 Sep 2009 12:23:16 +0200 | |
branch | stable |
changeset 3164 | b025592ff478 |
parent 3056 | 61f71ed15cfa |
child 3071 | 6ad4e6d4df86 |
permissions | -rw-r--r-- |
0 | 1 |
"""Core hooks: check schema validity, unsure we are not deleting necessary |
2 |
entities... |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
11 |
from datetime import datetime |
0 | 12 |
|
13 |
from cubicweb import UnknownProperty, ValidationError, BadConnectionId |
|
14 |
||
15 |
from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation |
|
16 |
from cubicweb.server.hookhelper import (check_internal_entity, previous_state, |
|
17 |
get_user_sessions, rproperty) |
|
18 |
from cubicweb.server.repository import FTIndexEntityOp |
|
19 |
||
2605
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
20 |
# special relations that don't have to be checked for integrity, usually |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
21 |
# because they are handled internally by hooks (so we trust ourselves) |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
22 |
DONT_CHECK_RTYPES_ON_ADD = set(('owned_by', 'created_by', |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
23 |
'is', 'is_instance_of', |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
24 |
'wf_info_for', 'from_state', 'to_state')) |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
25 |
DONT_CHECK_RTYPES_ON_DEL = set(('is', 'is_instance_of', |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
26 |
'wf_info_for', 'from_state', 'to_state')) |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
27 |
|
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
28 |
|
0 | 29 |
def relation_deleted(session, eidfrom, rtype, eidto): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
30 |
session.transaction_data.setdefault('pendingrelations', []).append( |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
31 |
(eidfrom, rtype, eidto)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
32 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
33 |
def eschema_type_eid(session, etype): |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
34 |
"""get eid of the CWEType entity for the given yams type""" |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
35 |
eschema = session.repo.schema.eschema(etype) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
36 |
# eschema.eid is None if schema has been readen from the filesystem, not |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
37 |
# from the database (eg during tests) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
38 |
if eschema.eid is None: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
39 |
eschema.eid = session.unsafe_execute( |
3056
61f71ed15cfa
fix necessary for some tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
40 |
'Any X WHERE X is CWEType, X name %(name)s', {'name': str(etype)})[0][0] |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
41 |
return eschema.eid |
0 | 42 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
43 |
|
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
44 |
# base meta-data handling ###################################################### |
0 | 45 |
|
46 |
def setctime_before_add_entity(session, entity): |
|
47 |
"""before create a new entity -> set creation and modification date |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
48 |
|
0 | 49 |
this is a conveniency hook, you shouldn't have to disable it |
50 |
""" |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
51 |
timestamp = datetime.now() |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
52 |
entity.setdefault('creation_date', timestamp) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
53 |
entity.setdefault('modification_date', timestamp) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
54 |
if not session.get_shared_data('do-not-insert-cwuri'): |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
55 |
entity.setdefault('cwuri', u'%seid/%s' % (session.base_url(), entity.eid)) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
56 |
|
0 | 57 |
|
58 |
def setmtime_before_update_entity(session, entity): |
|
59 |
"""update an entity -> set modification date""" |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
60 |
entity.setdefault('modification_date', datetime.now()) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
61 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
62 |
|
0 | 63 |
class SetCreatorOp(PreCommitOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
64 |
|
0 | 65 |
def precommit_event(self): |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
66 |
session = self.session |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
67 |
if self.entity.eid in session.transaction_data.get('pendingeids', ()): |
0 | 68 |
# entity have been created and deleted in the same transaction |
69 |
return |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
70 |
if not self.entity.created_by: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
71 |
session.add_relation(self.entity.eid, 'created_by', session.user.eid) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
72 |
|
0 | 73 |
|
74 |
def setowner_after_add_entity(session, entity): |
|
75 |
"""create a new entity -> set owner and creator metadata""" |
|
76 |
asession = session.actual_session() |
|
77 |
if not asession.is_internal_session: |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
78 |
session.add_relation(entity.eid, 'owned_by', asession.user.eid) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
79 |
SetCreatorOp(asession, entity=entity) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
80 |
|
0 | 81 |
|
82 |
def setis_after_add_entity(session, entity): |
|
83 |
"""create a new entity -> set is relation""" |
|
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
84 |
if hasattr(entity, '_cw_recreating'): |
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
85 |
return |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
86 |
try: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
87 |
session.add_relation(entity.eid, 'is', |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
88 |
eschema_type_eid(session, entity.id)) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
89 |
except IndexError: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
90 |
# during schema serialization, skip |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
91 |
return |
0 | 92 |
# XXX < 2.50 bw compat |
93 |
if not session.get_shared_data('do-not-insert-is_instance_of'): |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
94 |
for etype in entity.e_schema.ancestors() + [entity.e_schema]: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
95 |
session.add_relation(entity.eid, 'is_instance_of', |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
96 |
eschema_type_eid(session, etype)) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
97 |
|
0 | 98 |
|
99 |
def setowner_after_add_user(session, entity): |
|
100 |
"""when a user has been created, add owned_by relation on itself""" |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
101 |
session.add_relation(entity.eid, 'owned_by', entity.eid) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
102 |
|
0 | 103 |
|
104 |
def fti_update_after_add_relation(session, eidfrom, rtype, eidto): |
|
105 |
"""sync fulltext index when relevant relation is added. Reindexing the |
|
106 |
contained entity is enough since it will implicitly reindex the container |
|
107 |
entity. |
|
108 |
""" |
|
109 |
ftcontainer = session.repo.schema.rschema(rtype).fulltext_container |
|
110 |
if ftcontainer == 'subject': |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
111 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidto)) |
0 | 112 |
elif ftcontainer == 'object': |
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
113 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidfrom)) |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
114 |
|
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
115 |
|
0 | 116 |
def fti_update_after_delete_relation(session, eidfrom, rtype, eidto): |
117 |
"""sync fulltext index when relevant relation is deleted. Reindexing both |
|
118 |
entities is necessary. |
|
119 |
""" |
|
120 |
if session.repo.schema.rschema(rtype).fulltext_container: |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
121 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidto)) |
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
122 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidfrom)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
123 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
124 |
|
0 | 125 |
class SyncOwnersOp(PreCommitOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
126 |
|
0 | 127 |
def precommit_event(self): |
128 |
self.session.unsafe_execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,' |
|
129 |
'NOT EXISTS(X owned_by U, X eid %(x)s)', |
|
130 |
{'c': self.compositeeid, 'x': self.composedeid}, |
|
131 |
('c', 'x')) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
132 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
133 |
|
0 | 134 |
def sync_owner_after_add_composite_relation(session, eidfrom, rtype, eidto): |
135 |
"""when adding composite relation, the composed should have the same owners |
|
136 |
has the composite |
|
137 |
""" |
|
138 |
if rtype == 'wf_info_for': |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
139 |
# skip this special composite relation # XXX (syt) why? |
0 | 140 |
return |
141 |
composite = rproperty(session, rtype, eidfrom, eidto, 'composite') |
|
142 |
if composite == 'subject': |
|
143 |
SyncOwnersOp(session, compositeeid=eidfrom, composedeid=eidto) |
|
144 |
elif composite == 'object': |
|
145 |
SyncOwnersOp(session, compositeeid=eidto, composedeid=eidfrom) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
146 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
147 |
|
0 | 148 |
def _register_metadata_hooks(hm): |
149 |
"""register meta-data related hooks on the hooks manager""" |
|
150 |
hm.register_hook(setctime_before_add_entity, 'before_add_entity', '') |
|
151 |
hm.register_hook(setmtime_before_update_entity, 'before_update_entity', '') |
|
152 |
hm.register_hook(setowner_after_add_entity, 'after_add_entity', '') |
|
153 |
hm.register_hook(sync_owner_after_add_composite_relation, 'after_add_relation', '') |
|
154 |
hm.register_hook(fti_update_after_add_relation, 'after_add_relation', '') |
|
155 |
hm.register_hook(fti_update_after_delete_relation, 'after_delete_relation', '') |
|
156 |
if 'is' in hm.schema: |
|
157 |
hm.register_hook(setis_after_add_entity, 'after_add_entity', '') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
158 |
if 'CWUser' in hm.schema: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
159 |
hm.register_hook(setowner_after_add_user, 'after_add_entity', 'CWUser') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
160 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
161 |
|
0 | 162 |
# core hooks ################################################################## |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
163 |
|
0 | 164 |
class DelayedDeleteOp(PreCommitOperation): |
165 |
"""delete the object of composite relation except if the relation |
|
166 |
has actually been redirected to another composite |
|
167 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
168 |
|
0 | 169 |
def precommit_event(self): |
170 |
session = self.session |
|
2607
5d889b4928bb
[F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2606
diff
changeset
|
171 |
# don't do anything if the entity is being created or deleted |
5d889b4928bb
[F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2606
diff
changeset
|
172 |
if not (self.eid in session.transaction_data.get('pendingeids', ()) or |
5d889b4928bb
[F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2606
diff
changeset
|
173 |
self.eid in session.transaction_data.get('neweids', ())): |
0 | 174 |
etype = session.describe(self.eid)[0] |
175 |
session.unsafe_execute('DELETE %s X WHERE X eid %%(x)s, NOT %s' |
|
176 |
% (etype, self.relation), |
|
177 |
{'x': self.eid}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
178 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
179 |
|
0 | 180 |
def handle_composite_before_del_relation(session, eidfrom, rtype, eidto): |
181 |
"""delete the object of composite relation""" |
|
182 |
composite = rproperty(session, rtype, eidfrom, eidto, 'composite') |
|
183 |
if composite == 'subject': |
|
184 |
DelayedDeleteOp(session, eid=eidto, relation='Y %s X' % rtype) |
|
185 |
elif composite == 'object': |
|
186 |
DelayedDeleteOp(session, eid=eidfrom, relation='X %s Y' % rtype) |
|
187 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
188 |
|
0 | 189 |
def before_del_group(session, eid): |
190 |
"""check that we don't remove the owners group""" |
|
191 |
check_internal_entity(session, eid, ('owners',)) |
|
192 |
||
193 |
||
194 |
# schema validation hooks ##################################################### |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
195 |
|
0 | 196 |
class CheckConstraintsOperation(LateOperation): |
197 |
"""check a new relation satisfy its constraints |
|
198 |
""" |
|
199 |
def precommit_event(self): |
|
200 |
eidfrom, rtype, eidto = self.rdef |
|
201 |
# first check related entities have not been deleted in the same |
|
202 |
# transaction |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
203 |
pending = self.session.transaction_data.get('pendingeids', ()) |
0 | 204 |
if eidfrom in pending: |
205 |
return |
|
206 |
if eidto in pending: |
|
207 |
return |
|
208 |
for constraint in self.constraints: |
|
209 |
try: |
|
210 |
constraint.repo_check(self.session, eidfrom, rtype, eidto) |
|
211 |
except NotImplementedError: |
|
212 |
self.critical('can\'t check constraint %s, not supported', |
|
213 |
constraint) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
214 |
|
0 | 215 |
def commit_event(self): |
216 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
217 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
218 |
|
0 | 219 |
def cstrcheck_after_add_relation(session, eidfrom, rtype, eidto): |
220 |
"""check the relation satisfy its constraints |
|
221 |
||
222 |
this is delayed to a precommit time operation since other relation which |
|
223 |
will make constraint satisfied may be added later. |
|
224 |
""" |
|
225 |
constraints = rproperty(session, rtype, eidfrom, eidto, 'constraints') |
|
226 |
if constraints: |
|
227 |
CheckConstraintsOperation(session, constraints=constraints, |
|
228 |
rdef=(eidfrom, rtype, eidto)) |
|
229 |
||
230 |
def uniquecstrcheck_before_modification(session, entity): |
|
231 |
eschema = entity.e_schema |
|
232 |
for attr, val in entity.items(): |
|
233 |
if val is None: |
|
234 |
continue |
|
235 |
if eschema.subject_relation(attr).is_final() and \ |
|
236 |
eschema.has_unique_values(attr): |
|
237 |
rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr) |
|
238 |
rset = session.unsafe_execute(rql, {'val': val}) |
|
239 |
if rset and rset[0][0] != entity.eid: |
|
240 |
msg = session._('the value "%s" is already used, use another one') |
|
241 |
raise ValidationError(entity.eid, {attr: msg % val}) |
|
242 |
||
243 |
||
244 |
class CheckRequiredRelationOperation(LateOperation): |
|
245 |
"""checking relation cardinality has to be done after commit in |
|
246 |
case the relation is being replaced |
|
247 |
""" |
|
248 |
eid, rtype = None, None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
249 |
|
0 | 250 |
def precommit_event(self): |
251 |
# recheck pending eids |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
252 |
if self.eid in self.session.transaction_data.get('pendingeids', ()): |
0 | 253 |
return |
254 |
if self.session.unsafe_execute(*self._rql()).rowcount < 1: |
|
255 |
etype = self.session.describe(self.eid)[0] |
|
2241
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
256 |
_ = self.session._ |
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
257 |
msg = _('at least one relation %(rtype)s is required on %(etype)s (%(eid)s)') |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
258 |
msg %= {'rtype': _(self.rtype), 'etype': _(etype), 'eid': self.eid} |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
259 |
raise ValidationError(self.eid, {self.rtype: msg}) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
260 |
|
0 | 261 |
def commit_event(self): |
262 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
263 |
|
0 | 264 |
def _rql(self): |
265 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
266 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
267 |
|
0 | 268 |
class CheckSRelationOp(CheckRequiredRelationOperation): |
269 |
"""check required subject relation""" |
|
270 |
def _rql(self): |
|
271 |
return 'Any O WHERE S eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
272 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
273 |
|
0 | 274 |
class CheckORelationOp(CheckRequiredRelationOperation): |
275 |
"""check required object relation""" |
|
276 |
def _rql(self): |
|
277 |
return 'Any S WHERE O eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x' |
|
278 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
279 |
|
0 | 280 |
def checkrel_if_necessary(session, opcls, rtype, eid): |
281 |
"""check an equivalent operation has not already been added""" |
|
282 |
for op in session.pending_operations: |
|
283 |
if isinstance(op, opcls) and op.rtype == rtype and op.eid == eid: |
|
284 |
break |
|
285 |
else: |
|
286 |
opcls(session, rtype=rtype, eid=eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
287 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
288 |
|
0 | 289 |
def cardinalitycheck_after_add_entity(session, entity): |
290 |
"""check cardinalities are satisfied""" |
|
291 |
eid = entity.eid |
|
292 |
for rschema, targetschemas, x in entity.e_schema.relation_definitions(): |
|
293 |
# skip automatically handled relations |
|
2605
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
294 |
if rschema.type in DONT_CHECK_RTYPES_ON_ADD: |
0 | 295 |
continue |
296 |
if x == 'subject': |
|
297 |
subjtype = entity.e_schema |
|
298 |
objtype = targetschemas[0].type |
|
299 |
cardindex = 0 |
|
300 |
opcls = CheckSRelationOp |
|
301 |
else: |
|
302 |
subjtype = targetschemas[0].type |
|
303 |
objtype = entity.e_schema |
|
304 |
cardindex = 1 |
|
305 |
opcls = CheckORelationOp |
|
306 |
card = rschema.rproperty(subjtype, objtype, 'cardinality') |
|
307 |
if card[cardindex] in '1+': |
|
308 |
checkrel_if_necessary(session, opcls, rschema.type, eid) |
|
309 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
310 |
|
0 | 311 |
def cardinalitycheck_before_del_relation(session, eidfrom, rtype, eidto): |
312 |
"""check cardinalities are satisfied""" |
|
2605
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
313 |
if rtype in DONT_CHECK_RTYPES_ON_DEL: |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
314 |
return |
0 | 315 |
card = rproperty(session, rtype, eidfrom, eidto, 'cardinality') |
2745
0dafa29ace1f
[schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2695
diff
changeset
|
316 |
pendingrdefs = session.transaction_data.get('pendingrdefs', ()) |
0dafa29ace1f
[schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2695
diff
changeset
|
317 |
if (session.describe(eidfrom)[0], rtype, session.describe(eidto)[0]) in pendingrdefs: |
0dafa29ace1f
[schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2695
diff
changeset
|
318 |
return |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
319 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
0 | 320 |
if card[0] in '1+' and not eidfrom in pendingeids: |
321 |
checkrel_if_necessary(session, CheckSRelationOp, rtype, eidfrom) |
|
322 |
if card[1] in '1+' and not eidto in pendingeids: |
|
323 |
checkrel_if_necessary(session, CheckORelationOp, rtype, eidto) |
|
324 |
||
325 |
||
326 |
def _register_core_hooks(hm): |
|
327 |
hm.register_hook(handle_composite_before_del_relation, 'before_delete_relation', '') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
328 |
hm.register_hook(before_del_group, 'before_delete_entity', 'CWGroup') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
329 |
|
0 | 330 |
#hm.register_hook(cstrcheck_before_update_entity, 'before_update_entity', '') |
331 |
hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
332 |
hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
333 |
hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
334 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
335 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
336 |
||
337 |
||
338 |
# user/groups synchronisation ################################################# |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
339 |
|
0 | 340 |
class GroupOperation(Operation): |
341 |
"""base class for group operation""" |
|
342 |
geid = None |
|
343 |
def __init__(self, session, *args, **kwargs): |
|
344 |
"""override to get the group name before actual groups manipulation: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
345 |
|
0 | 346 |
we may temporarily loose right access during a commit event, so |
347 |
no query should be emitted while comitting |
|
348 |
""" |
|
349 |
rql = 'Any N WHERE G eid %(x)s, G name N' |
|
350 |
result = session.execute(rql, {'x': kwargs['geid']}, 'x', build_descr=False) |
|
351 |
Operation.__init__(self, session, *args, **kwargs) |
|
352 |
self.group = result[0][0] |
|
353 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
354 |
|
0 | 355 |
class DeleteGroupOp(GroupOperation): |
356 |
"""synchronize user when a in_group relation has been deleted""" |
|
357 |
def commit_event(self): |
|
358 |
"""the observed connections pool has been commited""" |
|
359 |
groups = self.cnxuser.groups |
|
360 |
try: |
|
361 |
groups.remove(self.group) |
|
362 |
except KeyError: |
|
363 |
self.error('user %s not in group %s', self.cnxuser, self.group) |
|
364 |
return |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
365 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
366 |
|
0 | 367 |
def after_del_in_group(session, fromeid, rtype, toeid): |
368 |
"""modify user permission, need to update users""" |
|
369 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
370 |
DeleteGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
371 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
372 |
|
0 | 373 |
class AddGroupOp(GroupOperation): |
374 |
"""synchronize user when a in_group relation has been added""" |
|
375 |
def commit_event(self): |
|
376 |
"""the observed connections pool has been commited""" |
|
377 |
groups = self.cnxuser.groups |
|
378 |
if self.group in groups: |
|
379 |
self.warning('user %s already in group %s', self.cnxuser, |
|
380 |
self.group) |
|
381 |
return |
|
382 |
groups.add(self.group) |
|
383 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
384 |
|
0 | 385 |
def after_add_in_group(session, fromeid, rtype, toeid): |
386 |
"""modify user permission, need to update users""" |
|
387 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
388 |
AddGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
389 |
||
390 |
||
391 |
class DelUserOp(Operation): |
|
392 |
"""synchronize user when a in_group relation has been added""" |
|
393 |
def __init__(self, session, cnxid): |
|
394 |
self.cnxid = cnxid |
|
395 |
Operation.__init__(self, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
396 |
|
0 | 397 |
def commit_event(self): |
398 |
"""the observed connections pool has been commited""" |
|
399 |
try: |
|
400 |
self.repo.close(self.cnxid) |
|
401 |
except BadConnectionId: |
|
402 |
pass # already closed |
|
403 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
404 |
|
0 | 405 |
def after_del_user(session, eid): |
406 |
"""modify user permission, need to update users""" |
|
407 |
for session_ in get_user_sessions(session.repo, eid): |
|
408 |
DelUserOp(session, session_.id) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
409 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
410 |
|
0 | 411 |
def _register_usergroup_hooks(hm): |
412 |
"""register user/group related hooks on the hooks manager""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
413 |
hm.register_hook(after_del_user, 'after_delete_entity', 'CWUser') |
0 | 414 |
hm.register_hook(after_add_in_group, 'after_add_relation', 'in_group') |
415 |
hm.register_hook(after_del_in_group, 'after_delete_relation', 'in_group') |
|
416 |
||
417 |
||
418 |
# workflow handling ########################################################### |
|
419 |
||
420 |
def before_add_in_state(session, fromeid, rtype, toeid): |
|
421 |
"""check the transition is allowed and record transition information |
|
422 |
""" |
|
423 |
assert rtype == 'in_state' |
|
424 |
state = previous_state(session, fromeid) |
|
425 |
etype = session.describe(fromeid)[0] |
|
426 |
if not (session.is_super_session or 'managers' in session.user.groups): |
|
427 |
if not state is None: |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
428 |
entity = session.entity_from_eid(fromeid) |
0 | 429 |
# we should find at least one transition going to this state |
430 |
try: |
|
431 |
iter(state.transitions(entity, toeid)).next() |
|
432 |
except StopIteration: |
|
2909
e695b7b0359d
[hooks] say which are the states (again)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2908
diff
changeset
|
433 |
_ = session._ |
e695b7b0359d
[hooks] say which are the states (again)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2908
diff
changeset
|
434 |
msg = _('transition from %s to %s does not exist or is not allowed') % ( |
e695b7b0359d
[hooks] say which are the states (again)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2908
diff
changeset
|
435 |
_(state.name), _(session.entity_from_eid(toeid).name)) |
0 | 436 |
raise ValidationError(fromeid, {'in_state': msg}) |
437 |
else: |
|
438 |
# not a transition |
|
439 |
# check state is initial state if the workflow defines one |
|
440 |
isrset = session.unsafe_execute('Any S WHERE ET initial_state S, ET name %(etype)s', |
|
441 |
{'etype': etype}) |
|
442 |
if isrset and not toeid == isrset[0][0]: |
|
2908
1c42a9826dd4
[hooks] say which are the states
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2745
diff
changeset
|
443 |
_ = session._ |
1c42a9826dd4
[hooks] say which are the states
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2745
diff
changeset
|
444 |
msg = _('%s is not the initial state (%s) for this entity') % ( |
1c42a9826dd4
[hooks] say which are the states
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2745
diff
changeset
|
445 |
_(session.entity_from_eid(toeid).name), _(isrset.get_entity(0,0).name)) |
0 | 446 |
raise ValidationError(fromeid, {'in_state': msg}) |
447 |
eschema = session.repo.schema[etype] |
|
448 |
if not 'wf_info_for' in eschema.object_relations(): |
|
449 |
# workflow history not activated for this entity type |
|
450 |
return |
|
451 |
rql = 'INSERT TrInfo T: T wf_info_for E, T to_state DS, T comment %(comment)s' |
|
452 |
args = {'comment': session.get_shared_data('trcomment', None, pop=True), |
|
453 |
'e': fromeid, 'ds': toeid} |
|
454 |
cformat = session.get_shared_data('trcommentformat', None, pop=True) |
|
455 |
if cformat is not None: |
|
456 |
args['comment_format'] = cformat |
|
457 |
rql += ', T comment_format %(comment_format)s' |
|
458 |
restriction = ['DS eid %(ds)s, E eid %(e)s'] |
|
459 |
if not state is None: # not a transition |
|
460 |
rql += ', T from_state FS' |
|
461 |
restriction.append('FS eid %(fs)s') |
|
462 |
args['fs'] = state.eid |
|
463 |
rql = '%s WHERE %s' % (rql, ', '.join(restriction)) |
|
464 |
session.unsafe_execute(rql, args, 'e') |
|
465 |
||
466 |
||
467 |
class SetInitialStateOp(PreCommitOperation): |
|
468 |
"""make initial state be a default state""" |
|
469 |
||
470 |
def precommit_event(self): |
|
471 |
session = self.session |
|
472 |
entity = self.entity |
|
473 |
# if there is an initial state and the entity's state is not set, |
|
474 |
# use the initial state as a default state |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
475 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
2606
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
476 |
if not entity.eid in pendingeids and not entity.in_state: |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
477 |
rset = session.execute('Any S WHERE ET initial_state S, ET name %(name)s', |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
478 |
{'name': entity.id}) |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
479 |
if rset: |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
480 |
session.add_relation(entity.eid, 'in_state', rset[0][0]) |
0 | 481 |
|
482 |
||
483 |
def set_initial_state_after_add(session, entity): |
|
484 |
SetInitialStateOp(session, entity=entity) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
485 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
486 |
|
0 | 487 |
def _register_wf_hooks(hm): |
488 |
"""register workflow related hooks on the hooks manager""" |
|
489 |
if 'in_state' in hm.schema: |
|
490 |
hm.register_hook(before_add_in_state, 'before_add_relation', 'in_state') |
|
491 |
hm.register_hook(relation_deleted, 'before_delete_relation', 'in_state') |
|
492 |
for eschema in hm.schema.entities(): |
|
493 |
if 'in_state' in eschema.subject_relations(): |
|
494 |
hm.register_hook(set_initial_state_after_add, 'after_add_entity', |
|
495 |
str(eschema)) |
|
496 |
||
497 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
498 |
# CWProperty hooks ############################################################# |
0 | 499 |
|
500 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
501 |
class DelCWPropertyOp(Operation): |
0 | 502 |
"""a user's custom properties has been deleted""" |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
503 |
|
0 | 504 |
def commit_event(self): |
505 |
"""the observed connections pool has been commited""" |
|
506 |
try: |
|
507 |
del self.epropdict[self.key] |
|
508 |
except KeyError: |
|
509 |
self.error('%s has no associated value', self.key) |
|
510 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
511 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
512 |
class ChangeCWPropertyOp(Operation): |
0 | 513 |
"""a user's custom properties has been added/changed""" |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
514 |
|
0 | 515 |
def commit_event(self): |
516 |
"""the observed connections pool has been commited""" |
|
517 |
self.epropdict[self.key] = self.value |
|
518 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
519 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
520 |
class AddCWPropertyOp(Operation): |
0 | 521 |
"""a user's custom properties has been added/changed""" |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
522 |
|
0 | 523 |
def commit_event(self): |
524 |
"""the observed connections pool has been commited""" |
|
525 |
eprop = self.eprop |
|
526 |
if not eprop.for_user: |
|
527 |
self.repo.vreg.eprop_values[eprop.pkey] = eprop.value |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
528 |
# if for_user is set, update is handled by a ChangeCWPropertyOp operation |
0 | 529 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
530 |
|
0 | 531 |
def after_add_eproperty(session, entity): |
532 |
key, value = entity.pkey, entity.value |
|
533 |
try: |
|
534 |
value = session.vreg.typed_value(key, value) |
|
535 |
except UnknownProperty: |
|
536 |
raise ValidationError(entity.eid, {'pkey': session._('unknown property key')}) |
|
537 |
except ValueError, ex: |
|
538 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
539 |
if not session.user.matching_groups('managers'): |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
540 |
session.add_relation(entity.eid, 'for_user', session.user.eid) |
0 | 541 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
542 |
AddCWPropertyOp(session, eprop=entity) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
543 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
544 |
|
0 | 545 |
def after_update_eproperty(session, entity): |
2695
56439c45781c
[hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2607
diff
changeset
|
546 |
if not ('pkey' in entity.edited_attributes or |
56439c45781c
[hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2607
diff
changeset
|
547 |
'value' in entity.edited_attributes): |
56439c45781c
[hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2607
diff
changeset
|
548 |
return |
0 | 549 |
key, value = entity.pkey, entity.value |
550 |
try: |
|
551 |
value = session.vreg.typed_value(key, value) |
|
552 |
except UnknownProperty: |
|
553 |
return |
|
554 |
except ValueError, ex: |
|
555 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
556 |
if entity.for_user: |
|
557 |
for session_ in get_user_sessions(session.repo, entity.for_user[0].eid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
558 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 559 |
key=key, value=value) |
560 |
else: |
|
561 |
# site wide properties |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
562 |
ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values, |
0 | 563 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
564 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
565 |
|
0 | 566 |
def before_del_eproperty(session, eid): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
567 |
for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()): |
0 | 568 |
if rtype == 'for_user' and eidfrom == eid: |
569 |
# if for_user was set, delete has already been handled |
|
570 |
break |
|
571 |
else: |
|
572 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
573 |
{'x': eid}, 'x')[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
574 |
DelCWPropertyOp(session, epropdict=session.vreg.eprop_values, key=key) |
0 | 575 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
576 |
|
0 | 577 |
def after_add_for_user(session, fromeid, rtype, toeid): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
578 |
if not session.describe(fromeid)[0] == 'CWProperty': |
0 | 579 |
return |
580 |
key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V', |
|
581 |
{'x': fromeid}, 'x')[0] |
|
582 |
if session.vreg.property_info(key)['sitewide']: |
|
583 |
raise ValidationError(fromeid, |
|
584 |
{'for_user': session._("site-wide property can't be set for user")}) |
|
585 |
for session_ in get_user_sessions(session.repo, toeid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
586 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 587 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
588 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
589 |
|
0 | 590 |
def before_del_for_user(session, fromeid, rtype, toeid): |
591 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
592 |
{'x': fromeid}, 'x')[0][0] |
|
593 |
relation_deleted(session, fromeid, rtype, toeid) |
|
594 |
for session_ in get_user_sessions(session.repo, toeid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
595 |
DelCWPropertyOp(session, epropdict=session_.user.properties, key=key) |
0 | 596 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
597 |
|
0 | 598 |
def _register_eproperty_hooks(hm): |
599 |
"""register workflow related hooks on the hooks manager""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
600 |
hm.register_hook(after_add_eproperty, 'after_add_entity', 'CWProperty') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
601 |
hm.register_hook(after_update_eproperty, 'after_update_entity', 'CWProperty') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
602 |
hm.register_hook(before_del_eproperty, 'before_delete_entity', 'CWProperty') |
0 | 603 |
hm.register_hook(after_add_for_user, 'after_add_relation', 'for_user') |
604 |
hm.register_hook(before_del_for_user, 'before_delete_relation', 'for_user') |