hooks/metadata.py
changeset 4835 13b0b96d7982
parent 4813 5a55c223612e
child 5066 bf5cbc351e99
equal deleted inserted replaced
4834:b718626a0e60 4835:13b0b96d7982
    17 def eschema_eid(session, eschema):
    17 def eschema_eid(session, eschema):
    18     """get eid of the CWEType entity for the given yams type"""
    18     """get eid of the CWEType entity for the given yams type"""
    19     # eschema.eid is None if schema has been readen from the filesystem, not
    19     # eschema.eid is None if schema has been readen from the filesystem, not
    20     # from the database (eg during tests)
    20     # from the database (eg during tests)
    21     if eschema.eid is None:
    21     if eschema.eid is None:
    22         eschema.eid = session.unsafe_execute(
    22         eschema.eid = session.execute(
    23             'Any X WHERE X is CWEType, X name %(name)s',
    23             'Any X WHERE X is CWEType, X name %(name)s',
    24             {'name': str(eschema)})[0][0]
    24             {'name': str(eschema)})[0][0]
    25     return eschema.eid
    25     return eschema.eid
    26 
    26 
    27 
    27 
   101     """create a new entity -> set owner and creator metadata"""
   101     """create a new entity -> set owner and creator metadata"""
   102     __regid__ = 'setowner'
   102     __regid__ = 'setowner'
   103     events = ('after_add_entity',)
   103     events = ('after_add_entity',)
   104 
   104 
   105     def __call__(self):
   105     def __call__(self):
   106         asession = self._cw.actual_session()
   106         if not self._cw.is_internal_session:
   107         if not asession.is_internal_session:
   107             self._cw.add_relation(self.entity.eid, 'owned_by', self._cw.user.eid)
   108             self._cw.add_relation(self.entity.eid, 'owned_by', asession.user.eid)
   108             _SetCreatorOp(self._cw, entity=self.entity)
   109             _SetCreatorOp(asession, entity=self.entity)
       
   110 
   109 
   111 
   110 
   112 class _SyncOwnersOp(hook.Operation):
   111 class _SyncOwnersOp(hook.Operation):
   113     def precommit_event(self):
   112     def precommit_event(self):
   114         self.session.unsafe_execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,'
   113         self.session.execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,'
   115                                     'NOT EXISTS(X owned_by U, X eid %(x)s)',
   114                              'NOT EXISTS(X owned_by U, X eid %(x)s)',
   116                                     {'c': self.compositeeid, 'x': self.composedeid},
   115                              {'c': self.compositeeid, 'x': self.composedeid},
   117                                     ('c', 'x'))
   116                              ('c', 'x'))
   118 
   117 
   119 
   118 
   120 class SyncCompositeOwner(MetaDataHook):
   119 class SyncCompositeOwner(MetaDataHook):
   121     """when adding composite relation, the composed should have the same owners
   120     """when adding composite relation, the composed should have the same owners
   122     has the composite
   121     has the composite