hooks/syncsources.py
changeset 6944 0cf10429ad39
parent 6724 24bf6f181d0e
child 6945 28bf94d062a9
equal deleted inserted replaced
6943:406a41c25e13 6944:0cf10429ad39
    35     def __call__(self):
    35     def __call__(self):
    36         if self.entity.name == 'system':
    36         if self.entity.name == 'system':
    37             raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
    37             raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
    38         SourceRemovedOp(self._cw, uri=self.entity.name)
    38         SourceRemovedOp(self._cw, uri=self.entity.name)
    39 
    39 
    40 class SourceRemovedHook(SourceHook):
    40 # source mapping synchronization. Expect cw_for_source/cw_schema are immutable
    41     __regid__ = 'cw.sources.removed'
    41 # relations (i.e. can't change from a source or schema to another).
    42     __select__ = SourceHook.__select__ & hook.match_rtype('cw_support', 'cw_may_cross')
    42 
    43     events = ('after_add_relation',)
    43 class SourceMappingDeleteHook(SourceHook):
       
    44     """check cw_for_source and cw_schema are immutable relations
       
    45 
       
    46     XXX empty delete perms would be enough?
       
    47     """
       
    48     __regid__ = 'cw.sources.delschemaconfig'
       
    49     __select__ = SourceHook.__select__ & hook.match_rtype('cw_for_source', 'cw_schema')
       
    50     events = ('before_add_relation',)
    44     def __call__(self):
    51     def __call__(self):
    45         entity = self._cw.entity_from_eid(self.eidto)
    52         if not self._cw.added_in_transaction(self.eidfrom):
    46         if entity.__regid__ == 'CWRType' and entity.name in  ('is', 'is_instance_of', 'cw_source'):
    53             msg = self._cw._("can't change this relation")
    47             msg = self._cw._('the %s relation type can\'t be used here') % entity.name
    54             raise ValidationError(self.eidfrom, {self.rtype: msg})
    48             raise ValidationError(self.eidto, {role_name(self.rtype, 'subject'): msg})
    55 
       
    56 
       
    57 class SourceMappingChangedOp(hook.DataOperationMixIn, hook.Operation):
       
    58     def check_or_update(self, checkonly):
       
    59         session = self.session
       
    60         # take care, can't call get_data() twice
       
    61         try:
       
    62             data = self.__data
       
    63         except AttributeError:
       
    64             data = self.__data = self.get_data()
       
    65         for schemacfg, source in data:
       
    66             if source is None:
       
    67                 source = schemacfg.source
       
    68             if session.added_in_transaction(schemacfg.eid):
       
    69                 if not session.deleted_in_transaction(schemacfg.eid):
       
    70                     source.add_schema_config(schemacfg, checkonly=checkonly)
       
    71             elif session.deleted_in_transaction(schemacfg.eid):
       
    72                 source.delete_schema_config(schemacfg, checkonly=checkonly)
       
    73             else:
       
    74                 source.update_schema_config(schemacfg, checkonly=checkonly)
       
    75 
       
    76     def precommit_event(self):
       
    77         self.check_or_update(True)
       
    78 
       
    79     def postcommit_event(self):
       
    80         self.check_or_update(False)
       
    81 
       
    82 
       
    83 class SourceMappingChangedHook(SourceHook):
       
    84     __regid__ = 'cw.sources.schemaconfig'
       
    85     __select__ = SourceHook.__select__ & is_instance('CWSourceSchemaConfig')
       
    86     events = ('after_add_entity', 'after_update_entity')
       
    87     def __call__(self):
       
    88         if self.event == 'after_add_entity' or (
       
    89             self.event == 'after_update_entity' and 'options' in self.entity.cw_edited):
       
    90             SourceMappingChangedOp.get_instance(self._cw).add_data(
       
    91                 (self.entity, None) )
       
    92 
       
    93 class SourceMappingDeleteHook(SourceHook):
       
    94     __regid__ = 'cw.sources.delschemaconfig'
       
    95     __select__ = SourceHook.__select__ & hook.match_rtype('cw_for_source')
       
    96     events = ('before_delete_relation',)
       
    97     def __call__(self):
       
    98         SourceMappingChangedOp.get_instance(self._cw).add_data(
       
    99             (self._cw.entity_from_eid(self.eidfrom),
       
   100              self._cw.entity_from_eid(self.eidto)) )