hooks/syncschema.py
branchstable
changeset 6261 82d3ac90e47c
parent 6208 07b176640a8c
child 6279 42079f752a9c
child 6294 a1535abe6ab2
equal deleted inserted replaced
6260:e06750b556ad 6261:82d3ac90e47c
    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)
   873     events = ('before_delete_entity',)
   866     events = ('before_delete_entity',)
   874 
   867 
   875     def __call__(self):
   868     def __call__(self):
   876         # final entities can't be deleted, don't care about that
   869         # final entities can't be deleted, don't care about that
   877         name = self.entity.name
   870         name = self.entity.name
   878         if name in CORE_ETYPES:
   871         if name in CORE_TYPES:
   879             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   872             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   880         # delete every entities of this type
   873         # delete every entities of this type
   881         if not name in ETYPE_NAME_MAP:
   874         if not name in ETYPE_NAME_MAP:
   882             self._cw.execute('DELETE %s X' % name)
   875             self._cw.execute('DELETE %s X' % name)
   883             MemSchemaCWETypeDel(self._cw, etype=name)
   876             MemSchemaCWETypeDel(self._cw, etype=name)
   939     __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
   932     __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
   940     events = ('before_delete_entity',)
   933     events = ('before_delete_entity',)
   941 
   934 
   942     def __call__(self):
   935     def __call__(self):
   943         name = self.entity.name
   936         name = self.entity.name
   944         if name in CORE_RTYPES:
   937         if name in CORE_TYPES:
   945             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   938             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   946         # delete relation definitions using this relation type
   939         # delete relation definitions using this relation type
   947         self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
   940         self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
   948                         {'x': self.entity.eid})
   941                         {'x': self.entity.eid})
   949         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
   942         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',