--- a/server/hooks.py Thu May 14 10:24:56 2009 +0200
+++ b/server/hooks.py Thu May 14 11:38:40 2009 +0200
@@ -18,13 +18,13 @@
def relation_deleted(session, eidfrom, rtype, eidto):
session.add_query_data('pendingrelations', (eidfrom, rtype, eidto))
-
+
# base meta-data handling #####################################################
def setctime_before_add_entity(session, entity):
"""before create a new entity -> set creation and modification date
-
+
this is a conveniency hook, you shouldn't have to disable it
"""
if not 'creation_date' in entity:
@@ -36,9 +36,9 @@
"""update an entity -> set modification date"""
if not 'modification_date' in entity:
entity['modification_date'] = datetime.now()
-
+
class SetCreatorOp(PreCommitOperation):
-
+
def precommit_event(self):
if self.eid in self.session.query_data('pendingeids', ()):
# entity have been created and deleted in the same transaction
@@ -46,7 +46,7 @@
ueid = self.session.user.eid
execute = self.session.unsafe_execute
if not execute('Any X WHERE X created_by U, X eid %(x)s',
- {'x': self.eid}, 'x'):
+ {'x': self.eid}, 'x'):
execute('SET X created_by U WHERE X eid %(x)s, U eid %(u)s',
{'x': self.eid, 'u': ueid}, 'x')
@@ -93,15 +93,15 @@
if session.repo.schema.rschema(rtype).fulltext_container:
FTIndexEntityOp(session, entity=session.entity(eidto))
FTIndexEntityOp(session, entity=session.entity(eidfrom))
-
+
class SyncOwnersOp(PreCommitOperation):
-
+
def precommit_event(self):
self.session.unsafe_execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,'
'NOT EXISTS(X owned_by U, X eid %(x)s)',
{'c': self.compositeeid, 'x': self.composedeid},
('c', 'x'))
-
+
def sync_owner_after_add_composite_relation(session, eidfrom, rtype, eidto):
"""when adding composite relation, the composed should have the same owners
has the composite
@@ -114,7 +114,7 @@
SyncOwnersOp(session, compositeeid=eidfrom, composedeid=eidto)
elif composite == 'object':
SyncOwnersOp(session, compositeeid=eidto, composedeid=eidfrom)
-
+
def _register_metadata_hooks(hm):
"""register meta-data related hooks on the hooks manager"""
hm.register_hook(setctime_before_add_entity, 'before_add_entity', '')
@@ -127,14 +127,14 @@
hm.register_hook(setis_after_add_entity, 'after_add_entity', '')
if 'CWUser' in hm.schema:
hm.register_hook(setowner_after_add_user, 'after_add_entity', 'CWUser')
-
+
# core hooks ##################################################################
-
+
class DelayedDeleteOp(PreCommitOperation):
"""delete the object of composite relation except if the relation
has actually been redirected to another composite
"""
-
+
def precommit_event(self):
session = self.session
if not self.eid in session.query_data('pendingeids', ()):
@@ -142,7 +142,7 @@
session.unsafe_execute('DELETE %s X WHERE X eid %%(x)s, NOT %s'
% (etype, self.relation),
{'x': self.eid}, 'x')
-
+
def handle_composite_before_del_relation(session, eidfrom, rtype, eidto):
"""delete the object of composite relation"""
composite = rproperty(session, rtype, eidfrom, eidto, 'composite')
@@ -157,7 +157,7 @@
# schema validation hooks #####################################################
-
+
class CheckConstraintsOperation(LateOperation):
"""check a new relation satisfy its constraints
"""
@@ -176,10 +176,10 @@
except NotImplementedError:
self.critical('can\'t check constraint %s, not supported',
constraint)
-
+
def commit_event(self):
pass
-
+
def cstrcheck_after_add_relation(session, eidfrom, rtype, eidto):
"""check the relation satisfy its constraints
@@ -213,7 +213,7 @@
case the relation is being replaced
"""
eid, rtype = None, None
-
+
def precommit_event(self):
# recheck pending eids
if self.eid in self.session.query_data('pendingeids', ()):
@@ -224,18 +224,18 @@
raise ValidationError(self.eid, {self.rtype: msg % {'rtype': self.rtype,
'etype': etype,
'eid': self.eid}})
-
+
def commit_event(self):
pass
-
+
def _rql(self):
raise NotImplementedError()
-
+
class CheckSRelationOp(CheckRequiredRelationOperation):
"""check required subject relation"""
def _rql(self):
return 'Any O WHERE S eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x'
-
+
class CheckORelationOp(CheckRequiredRelationOperation):
"""check required object relation"""
def _rql(self):
@@ -248,7 +248,7 @@
break
else:
opcls(session, rtype=rtype, eid=eid)
-
+
def cardinalitycheck_after_add_entity(session, entity):
"""check cardinalities are satisfied"""
eid = entity.eid
@@ -283,7 +283,7 @@
def _register_core_hooks(hm):
hm.register_hook(handle_composite_before_del_relation, 'before_delete_relation', '')
hm.register_hook(before_del_group, 'before_delete_entity', 'CWGroup')
-
+
#hm.register_hook(cstrcheck_before_update_entity, 'before_update_entity', '')
hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '')
hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '')
@@ -293,13 +293,13 @@
# user/groups synchronisation #################################################
-
+
class GroupOperation(Operation):
"""base class for group operation"""
geid = None
def __init__(self, session, *args, **kwargs):
"""override to get the group name before actual groups manipulation:
-
+
we may temporarily loose right access during a commit event, so
no query should be emitted while comitting
"""
@@ -318,13 +318,13 @@
except KeyError:
self.error('user %s not in group %s', self.cnxuser, self.group)
return
-
+
def after_del_in_group(session, fromeid, rtype, toeid):
"""modify user permission, need to update users"""
for session_ in get_user_sessions(session.repo, fromeid):
DeleteGroupOp(session, cnxuser=session_.user, geid=toeid)
-
+
class AddGroupOp(GroupOperation):
"""synchronize user when a in_group relation has been added"""
def commit_event(self):
@@ -347,7 +347,7 @@
def __init__(self, session, cnxid):
self.cnxid = cnxid
Operation.__init__(self, session)
-
+
def commit_event(self):
"""the observed connections pool has been commited"""
try:
@@ -359,7 +359,7 @@
"""modify user permission, need to update users"""
for session_ in get_user_sessions(session.repo, eid):
DelUserOp(session, session_.id)
-
+
def _register_usergroup_hooks(hm):
"""register user/group related hooks on the hooks manager"""
hm.register_hook(after_del_user, 'after_delete_entity', 'CWUser')
@@ -430,7 +430,7 @@
def set_initial_state_after_add(session, entity):
SetInitialStateOp(session, entity=entity)
-
+
def _register_wf_hooks(hm):
"""register workflow related hooks on the hooks manager"""
if 'in_state' in hm.schema:
@@ -447,7 +447,7 @@
class DelCWPropertyOp(Operation):
"""a user's custom properties has been deleted"""
-
+
def commit_event(self):
"""the observed connections pool has been commited"""
try:
@@ -457,14 +457,14 @@
class ChangeCWPropertyOp(Operation):
"""a user's custom properties has been added/changed"""
-
+
def commit_event(self):
"""the observed connections pool has been commited"""
self.epropdict[self.key] = self.value
class AddCWPropertyOp(Operation):
"""a user's custom properties has been added/changed"""
-
+
def commit_event(self):
"""the observed connections pool has been commited"""
eprop = self.eprop
@@ -485,7 +485,7 @@
{'x': entity.eid, 'u': session.user.eid}, 'x')
else:
AddCWPropertyOp(session, eprop=entity)
-
+
def after_update_eproperty(session, entity):
key, value = entity.pkey, entity.value
try:
@@ -502,7 +502,7 @@
# site wide properties
ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values,
key=key, value=value)
-
+
def before_del_eproperty(session, eid):
for eidfrom, rtype, eidto in session.query_data('pendingrelations', ()):
if rtype == 'for_user' and eidfrom == eid:
@@ -524,7 +524,7 @@
for session_ in get_user_sessions(session.repo, toeid):
ChangeCWPropertyOp(session, epropdict=session_.user.properties,
key=key, value=value)
-
+
def before_del_for_user(session, fromeid, rtype, toeid):
key = session.execute('Any K WHERE P eid %(x)s, P pkey K',
{'x': fromeid}, 'x')[0][0]