server/repository.py
changeset 8573 ae0a567dff30
parent 8562 0d2fb4604265
parent 8548 eeabc752c32e
child 8580 d753d6a6798f
equal deleted inserted replaced
8570:e20057a9ceea 8573:ae0a567dff30
   116         with session.security_enabled(read=False):
   116         with session.security_enabled(read=False):
   117             session.execute('DELETE X %s Y WHERE Y eid %%(y)s, '
   117             session.execute('DELETE X %s Y WHERE Y eid %%(y)s, '
   118                             'NOT X eid %%(x)s' % rtype,
   118                             'NOT X eid %%(x)s' % rtype,
   119                             {'x': eidfrom, 'y': eidto})
   119                             {'x': eidfrom, 'y': eidto})
   120 
   120 
       
   121 
       
   122 def preprocess_inlined_relations(session, entity):
       
   123     """when an entity is added, check if it has some inlined relation which
       
   124     requires to be extrated for proper call hooks
       
   125     """
       
   126     relations = []
       
   127     activeintegrity = session.is_hook_category_activated('activeintegrity')
       
   128     eschema = entity.e_schema
       
   129     for attr in entity.cw_edited.iterkeys():
       
   130         rschema = eschema.subjrels[attr]
       
   131         if not rschema.final: # inlined relation
       
   132             value = entity.cw_edited[attr]
       
   133             relations.append((attr, value))
       
   134             session.update_rel_cache_add(entity.eid, attr, value)
       
   135             rdef = session.rtype_eids_rdef(attr, entity.eid, value)
       
   136             if rdef.cardinality[1] in '1?' and activeintegrity:
       
   137                 with session.security_enabled(read=False):
       
   138                     session.execute('DELETE X %s Y WHERE Y eid %%(y)s' % attr,
       
   139                                     {'x': entity.eid, 'y': value})
       
   140     return relations
   121 
   141 
   122 
   142 
   123 class NullEventBus(object):
   143 class NullEventBus(object):
   124     def publish(self, msg):
   144     def publish(self, msg):
   125         pass
   145         pass
  1331         """
  1351         """
  1332         entity = edited.entity
  1352         entity = edited.entity
  1333         entity._cw_is_saved = False # entity has an eid but is not yet saved
  1353         entity._cw_is_saved = False # entity has an eid but is not yet saved
  1334         # init edited_attributes before calling before_add_entity hooks
  1354         # init edited_attributes before calling before_add_entity hooks
  1335         entity.cw_edited = edited
  1355         entity.cw_edited = edited
  1336         eschema = entity.e_schema
       
  1337         source = self.locate_etype_source(entity.__regid__)
  1356         source = self.locate_etype_source(entity.__regid__)
  1338         # allocate an eid to the entity before calling hooks
  1357         # allocate an eid to the entity before calling hooks
  1339         entity.eid = self.system_source.create_eid(session)
  1358         entity.eid = self.system_source.create_eid(session)
  1340         # set caches asap
  1359         # set caches asap
  1341         extid = self.init_entity_caches(session, entity, source)
  1360         extid = self.init_entity_caches(session, entity, source)
  1342         if server.DEBUG & server.DBG_REPO:
  1361         if server.DEBUG & server.DBG_REPO:
  1343             print 'ADD entity', self, entity.__regid__, entity.eid, edited
  1362             print 'ADD entity', self, entity.__regid__, entity.eid, edited
  1344         prefill_entity_caches(entity)
  1363         prefill_entity_caches(entity)
  1345         if source.should_call_hooks:
  1364         if source.should_call_hooks:
  1346             self.hm.call_hooks('before_add_entity', session, entity=entity)
  1365             self.hm.call_hooks('before_add_entity', session, entity=entity)
  1347         relations = []
  1366         relations = preprocess_inlined_relations(session, entity)
  1348         activeintegrity = session.is_hook_category_activated('activeintegrity')
       
  1349         for attr in edited.iterkeys():
       
  1350             rschema = eschema.subjrels[attr]
       
  1351             if not rschema.final: # inlined relation
       
  1352                 value = edited[attr]
       
  1353                 relations.append((attr, value))
       
  1354                 session.update_rel_cache_add(entity.eid, attr, value)
       
  1355                 rdef = session.rtype_eids_rdef(attr, entity.eid, value)
       
  1356                 if rdef.cardinality[1] in '1?' and activeintegrity:
       
  1357                     with session.security_enabled(read=False):
       
  1358                         session.execute('DELETE X %s Y WHERE Y eid %%(y)s' % attr,
       
  1359                                         {'x': entity.eid, 'y': value})
       
  1360         edited.set_defaults()
  1367         edited.set_defaults()
  1361         if session.is_hook_category_activated('integrity'):
  1368         if session.is_hook_category_activated('integrity'):
  1362             edited.check(creation=True)
  1369             edited.check(creation=True)
  1363         try:
  1370         try:
  1364             source.add_entity(session, entity)
  1371             source.add_entity(session, entity)
  1517         subjects_by_types = {}
  1524         subjects_by_types = {}
  1518         objects_by_types = {}
  1525         objects_by_types = {}
  1519         activintegrity = session.is_hook_category_activated('activeintegrity')
  1526         activintegrity = session.is_hook_category_activated('activeintegrity')
  1520         for rtype, eids_subj_obj in relations.iteritems():
  1527         for rtype, eids_subj_obj in relations.iteritems():
  1521             if server.DEBUG & server.DBG_REPO:
  1528             if server.DEBUG & server.DBG_REPO:
  1522                 for subjeid, objeid in relations:
  1529                 for subjeid, objeid in eids_subj_obj:
  1523                     print 'ADD relation', subjeid, rtype, objeid
  1530                     print 'ADD relation', subjeid, rtype, objeid
  1524             for subjeid, objeid in eids_subj_obj:
  1531             for subjeid, objeid in eids_subj_obj:
  1525                 source = self.locate_relation_source(session, subjeid, rtype, objeid)
  1532                 source = self.locate_relation_source(session, subjeid, rtype, objeid)
  1526                 if source not in sources:
  1533                 if source not in sources:
  1527                     relations_by_rtype = {}
  1534                     relations_by_rtype = {}