diff -r e20057a9ceea -r ae0a567dff30 server/repository.py --- a/server/repository.py Fri Oct 12 15:38:58 2012 +0200 +++ b/server/repository.py Fri Oct 12 16:05:16 2012 +0200 @@ -119,6 +119,26 @@ {'x': eidfrom, 'y': eidto}) +def preprocess_inlined_relations(session, entity): + """when an entity is added, check if it has some inlined relation which + requires to be extrated for proper call hooks + """ + relations = [] + activeintegrity = session.is_hook_category_activated('activeintegrity') + eschema = entity.e_schema + for attr in entity.cw_edited.iterkeys(): + rschema = eschema.subjrels[attr] + if not rschema.final: # inlined relation + value = entity.cw_edited[attr] + relations.append((attr, value)) + session.update_rel_cache_add(entity.eid, attr, value) + rdef = session.rtype_eids_rdef(attr, entity.eid, value) + if rdef.cardinality[1] in '1?' and activeintegrity: + with session.security_enabled(read=False): + session.execute('DELETE X %s Y WHERE Y eid %%(y)s' % attr, + {'x': entity.eid, 'y': value}) + return relations + class NullEventBus(object): def publish(self, msg): @@ -1333,7 +1353,6 @@ entity._cw_is_saved = False # entity has an eid but is not yet saved # init edited_attributes before calling before_add_entity hooks entity.cw_edited = edited - eschema = entity.e_schema source = self.locate_etype_source(entity.__regid__) # allocate an eid to the entity before calling hooks entity.eid = self.system_source.create_eid(session) @@ -1344,19 +1363,7 @@ prefill_entity_caches(entity) if source.should_call_hooks: self.hm.call_hooks('before_add_entity', session, entity=entity) - relations = [] - activeintegrity = session.is_hook_category_activated('activeintegrity') - for attr in edited.iterkeys(): - rschema = eschema.subjrels[attr] - if not rschema.final: # inlined relation - value = edited[attr] - relations.append((attr, value)) - session.update_rel_cache_add(entity.eid, attr, value) - rdef = session.rtype_eids_rdef(attr, entity.eid, value) - if rdef.cardinality[1] in '1?' and activeintegrity: - with session.security_enabled(read=False): - session.execute('DELETE X %s Y WHERE Y eid %%(y)s' % attr, - {'x': entity.eid, 'y': value}) + relations = preprocess_inlined_relations(session, entity) edited.set_defaults() if session.is_hook_category_activated('integrity'): edited.check(creation=True) @@ -1519,7 +1526,7 @@ activintegrity = session.is_hook_category_activated('activeintegrity') for rtype, eids_subj_obj in relations.iteritems(): if server.DEBUG & server.DBG_REPO: - for subjeid, objeid in relations: + for subjeid, objeid in eids_subj_obj: print 'ADD relation', subjeid, rtype, objeid for subjeid, objeid in eids_subj_obj: source = self.locate_relation_source(session, subjeid, rtype, objeid)