31 |
31 |
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 implements |
36 from cubicweb.selectors import is_instance |
37 from cubicweb.schema import (META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS, |
37 from cubicweb.schema import (META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS, |
38 ETYPE_NAME_MAP, display_name) |
38 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 |
807 * check that we don't remove a core entity type |
807 * check that we don't remove a core entity type |
808 * cascade to delete related CWAttribute and CWRelation entities |
808 * cascade to delete related CWAttribute and CWRelation entities |
809 * instantiate an operation to delete the entity type on commit |
809 * instantiate an operation to delete the entity type on commit |
810 """ |
810 """ |
811 __regid__ = 'syncdelcwetype' |
811 __regid__ = 'syncdelcwetype' |
812 __select__ = SyncSchemaHook.__select__ & implements('CWEType') |
812 __select__ = SyncSchemaHook.__select__ & is_instance('CWEType') |
813 events = ('before_delete_entity',) |
813 events = ('before_delete_entity',) |
814 |
814 |
815 def __call__(self): |
815 def __call__(self): |
816 # final entities can't be deleted, don't care about that |
816 # final entities can't be deleted, don't care about that |
817 name = self.entity.name |
817 name = self.entity.name |
912 * check that we don't remove a core relation type |
912 * check that we don't remove a core relation type |
913 * cascade to delete related CWAttribute and CWRelation entities |
913 * cascade to delete related CWAttribute and CWRelation entities |
914 * instantiate an operation to delete the relation type on commit |
914 * instantiate an operation to delete the relation type on commit |
915 """ |
915 """ |
916 __regid__ = 'syncdelcwrtype' |
916 __regid__ = 'syncdelcwrtype' |
917 __select__ = SyncSchemaHook.__select__ & implements('CWRType') |
917 __select__ = SyncSchemaHook.__select__ & is_instance('CWRType') |
918 events = ('before_delete_entity',) |
918 events = ('before_delete_entity',) |
919 |
919 |
920 def __call__(self): |
920 def __call__(self): |
921 name = self.entity.name |
921 name = self.entity.name |
922 if name in CORE_RTYPES: |
922 if name in CORE_RTYPES: |
1030 |
1030 |
1031 # CWAttribute / CWRelation hooks ############################################### |
1031 # CWAttribute / CWRelation hooks ############################################### |
1032 |
1032 |
1033 class AfterAddCWAttributeHook(SyncSchemaHook): |
1033 class AfterAddCWAttributeHook(SyncSchemaHook): |
1034 __regid__ = 'syncaddcwattribute' |
1034 __regid__ = 'syncaddcwattribute' |
1035 __select__ = SyncSchemaHook.__select__ & implements('CWAttribute') |
1035 __select__ = SyncSchemaHook.__select__ & is_instance('CWAttribute') |
1036 events = ('after_add_entity',) |
1036 events = ('after_add_entity',) |
1037 |
1037 |
1038 def __call__(self): |
1038 def __call__(self): |
1039 SourceDbCWAttributeAdd(self._cw, entity=self.entity) |
1039 SourceDbCWAttributeAdd(self._cw, entity=self.entity) |
1040 |
1040 |
1041 |
1041 |
1042 class AfterAddCWRelationHook(AfterAddCWAttributeHook): |
1042 class AfterAddCWRelationHook(AfterAddCWAttributeHook): |
1043 __regid__ = 'syncaddcwrelation' |
1043 __regid__ = 'syncaddcwrelation' |
1044 __select__ = SyncSchemaHook.__select__ & implements('CWRelation') |
1044 __select__ = SyncSchemaHook.__select__ & is_instance('CWRelation') |
1045 |
1045 |
1046 def __call__(self): |
1046 def __call__(self): |
1047 SourceDbCWRelationAdd(self._cw, entity=self.entity) |
1047 SourceDbCWRelationAdd(self._cw, entity=self.entity) |
1048 |
1048 |
1049 |
1049 |
1050 class AfterUpdateCWRDefHook(SyncSchemaHook): |
1050 class AfterUpdateCWRDefHook(SyncSchemaHook): |
1051 __regid__ = 'syncaddcwattribute' |
1051 __regid__ = 'syncaddcwattribute' |
1052 __select__ = SyncSchemaHook.__select__ & implements('CWAttribute', |
1052 __select__ = SyncSchemaHook.__select__ & is_instance('CWAttribute', |
1053 'CWRelation') |
1053 'CWRelation') |
1054 events = ('before_update_entity',) |
1054 events = ('before_update_entity',) |
1055 |
1055 |
1056 def __call__(self): |
1056 def __call__(self): |
1057 entity = self.entity |
1057 entity = self.entity |
1079 |
1079 |
1080 # constraints synchronization hooks ############################################ |
1080 # constraints synchronization hooks ############################################ |
1081 |
1081 |
1082 class AfterAddCWConstraintHook(SyncSchemaHook): |
1082 class AfterAddCWConstraintHook(SyncSchemaHook): |
1083 __regid__ = 'syncaddcwconstraint' |
1083 __regid__ = 'syncaddcwconstraint' |
1084 __select__ = SyncSchemaHook.__select__ & implements('CWConstraint') |
1084 __select__ = SyncSchemaHook.__select__ & is_instance('CWConstraint') |
1085 events = ('after_add_entity', 'after_update_entity') |
1085 events = ('after_add_entity', 'after_update_entity') |
1086 |
1086 |
1087 def __call__(self): |
1087 def __call__(self): |
1088 MemSchemaCWConstraintAdd(self._cw, entity=self.entity) |
1088 MemSchemaCWConstraintAdd(self._cw, entity=self.entity) |
1089 SourceDbCWConstraintAdd(self._cw, entity=self.entity) |
1089 SourceDbCWConstraintAdd(self._cw, entity=self.entity) |