hooks/metadata.py
changeset 4812 2eb6a9b33aa5
parent 4811 6c3d5f2a4e01
child 4813 5a55c223612e
equal deleted inserted replaced
4808:23df4a120c96 4812:2eb6a9b33aa5
    12 
    12 
    13 from cubicweb.selectors import implements
    13 from cubicweb.selectors import implements
    14 from cubicweb.server import hook
    14 from cubicweb.server import hook
    15 
    15 
    16 
    16 
    17 def eschema_type_eid(session, etype):
    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 = session.repo.schema.eschema(etype)
       
    20     # 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
    21     # from the database (eg during tests)
    20     # from the database (eg during tests)
    22     if eschema.eid is None:
    21     if eschema.eid is None:
    23         eschema.eid = session.unsafe_execute(
    22         eschema.eid = session.unsafe_execute(
    24             'Any X WHERE X is CWEType, X name %(name)s',
    23             'Any X WHERE X is CWEType, X name %(name)s',
    73         if not self.entity.created_by:
    72         if not self.entity.created_by:
    74             session.add_relation(self.entity.eid, 'created_by', session.user.eid)
    73             session.add_relation(self.entity.eid, 'created_by', session.user.eid)
    75 
    74 
    76 
    75 
    77 class SetIsHook(MetaDataHook):
    76 class SetIsHook(MetaDataHook):
    78     """create a new entity -> set is relation"""
    77     """create a new entity -> set is and is_instance_of relations
       
    78 
       
    79     those relations are inserted using sql so they are not hookable.
       
    80     """
    79     __regid__ = 'setis'
    81     __regid__ = 'setis'
    80     events = ('after_add_entity',)
    82     events = ('after_add_entity',)
    81 
    83 
    82     def __call__(self):
    84     def __call__(self):
    83         if hasattr(self.entity, '_cw_recreating'):
    85         if hasattr(self.entity, '_cw_recreating'):
    84             return
    86             return
    85         session = self._cw
    87         session = self._cw
    86         entity = self.entity
    88         entity = self.entity
    87         try:
    89         try:
    88             #session.add_relation(entity.eid, 'is',
       
    89             #                     eschema_type_eid(session, entity.__regid__))
       
    90             session.system_sql('INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)'
    90             session.system_sql('INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)'
    91                            % (entity.eid, eschema_type_eid(session, entity.__regid__)))
    91                            % (entity.eid, eschema_eid(session, entity.e_schema)))
    92         except IndexError:
    92         except IndexError:
    93             # during schema serialization, skip
    93             # during schema serialization, skip
    94             return
    94             return
    95         for etype in entity.e_schema.ancestors() + [entity.e_schema]:
    95         for eschema in entity.e_schema.ancestors() + [entity.e_schema]:
    96             #session.add_relation(entity.eid, 'is_instance_of',
       
    97             #                     eschema_type_eid(session, etype))
       
    98             session.system_sql('INSERT INTO is_instance_of_relation(eid_from,eid_to) VALUES (%s,%s)'
    96             session.system_sql('INSERT INTO is_instance_of_relation(eid_from,eid_to) VALUES (%s,%s)'
    99                                % (entity.eid, eschema_type_eid(session, etype)))
    97                                % (entity.eid, eschema_eid(session, eschema)))
   100 
    98 
   101 
    99 
   102 class SetOwnershipHook(MetaDataHook):
   100 class SetOwnershipHook(MetaDataHook):
   103     """create a new entity -> set owner and creator metadata"""
   101     """create a new entity -> set owner and creator metadata"""
   104     __regid__ = 'setowner'
   102     __regid__ = 'setowner'