hooks/syncschema.py
changeset 6279 42079f752a9c
parent 6225 a176e68b7d0d
parent 6261 82d3ac90e47c
child 6333 e3994fcc21c3
equal deleted inserted replaced
6229:c4a70a5dd144 6279:42079f752a9c
    32 from logilab.common.decorators import clear_cache
    32 from logilab.common.decorators import clear_cache
    33 from logilab.common.testlib import mock_object
    33 from logilab.common.testlib import mock_object
    34 
    34 
    35 from cubicweb import ValidationError
    35 from cubicweb import ValidationError
    36 from cubicweb.selectors import is_instance
    36 from cubicweb.selectors import is_instance
    37 from cubicweb.schema import (META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS,
    37 from cubicweb.schema import (SCHEMA_TYPES, META_RTYPES, VIRTUAL_RTYPES,
    38                              ETYPE_NAME_MAP, display_name)
    38                              CONSTRAINTS, ETYPE_NAME_MAP, display_name)
    39 from cubicweb.server import hook, schemaserial as ss
    39 from cubicweb.server import hook, schemaserial as ss
    40 from cubicweb.server.sqlutils import SQL_PREFIX
    40 from cubicweb.server.sqlutils import SQL_PREFIX
    41 
    41 
    42 
    42 
    43 TYPE_CONVERTER = { # XXX
    43 TYPE_CONVERTER = { # XXX
    50     'Datetime' : unicode,
    50     'Datetime' : unicode,
    51     'Time' : unicode,
    51     'Time' : unicode,
    52     }
    52     }
    53 
    53 
    54 # core entity and relation types which can't be removed
    54 # core entity and relation types which can't be removed
    55 CORE_ETYPES = list(BASE_TYPES) + ['CWEType', 'CWRType', 'CWUser', 'CWGroup',
    55 CORE_TYPES = BASE_TYPES | SCHEMA_TYPES | META_RTYPES | set(
    56                                   'CWConstraint', 'CWAttribute', 'CWRelation']
    56     ('CWUser', 'CWGroup','login', 'upassword', 'name', 'in_group'))
    57 CORE_RTYPES = ['eid', 'creation_date', 'modification_date', 'cwuri',
    57 
    58                'login', 'upassword', 'name',
       
    59                'is', 'instanceof', 'owned_by', 'created_by', 'in_group',
       
    60                'relation_type', 'from_entity', 'to_entity',
       
    61                'constrainted_by',
       
    62                'read_permission', 'add_permission',
       
    63                'delete_permission', 'updated_permission',
       
    64                ]
       
    65 
    58 
    66 def get_constraints(session, entity):
    59 def get_constraints(session, entity):
    67     constraints = []
    60     constraints = []
    68     for cstreid in session.transaction_data.get(entity.eid, ()):
    61     for cstreid in session.transaction_data.get(entity.eid, ()):
    69         cstrent = session.entity_from_eid(cstreid)
    62         cstrent = session.entity_from_eid(cstreid)
   871     events = ('before_delete_entity',)
   864     events = ('before_delete_entity',)
   872 
   865 
   873     def __call__(self):
   866     def __call__(self):
   874         # final entities can't be deleted, don't care about that
   867         # final entities can't be deleted, don't care about that
   875         name = self.entity.name
   868         name = self.entity.name
   876         if name in CORE_ETYPES:
   869         if name in CORE_TYPES:
   877             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   870             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   878         # delete every entities of this type
   871         # delete every entities of this type
   879         if not name in ETYPE_NAME_MAP:
   872         if not name in ETYPE_NAME_MAP:
   880             self._cw.execute('DELETE %s X' % name)
   873             self._cw.execute('DELETE %s X' % name)
   881             MemSchemaCWETypeDel(self._cw, etype=name)
   874             MemSchemaCWETypeDel(self._cw, etype=name)
   937     __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
   930     __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
   938     events = ('before_delete_entity',)
   931     events = ('before_delete_entity',)
   939 
   932 
   940     def __call__(self):
   933     def __call__(self):
   941         name = self.entity.name
   934         name = self.entity.name
   942         if name in CORE_RTYPES:
   935         if name in CORE_TYPES:
   943             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   936             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   944         # delete relation definitions using this relation type
   937         # delete relation definitions using this relation type
   945         self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
   938         self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
   946                         {'x': self.entity.eid})
   939                         {'x': self.entity.eid})
   947         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
   940         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',