server/repository.py
changeset 5557 1a534c596bff
parent 5556 9ab2b4c74baf
child 5590 a56eb02f9ce7
equal deleted inserted replaced
5556:9ab2b4c74baf 5557:1a534c596bff
   563             user = self.authenticate_user(session, login, **kwargs)
   563             user = self.authenticate_user(session, login, **kwargs)
   564         finally:
   564         finally:
   565             session.close()
   565             session.close()
   566         session = Session(user, self, cnxprops)
   566         session = Session(user, self, cnxprops)
   567         user._cw = user.cw_rset.req = session
   567         user._cw = user.cw_rset.req = session
   568         user.clear_related_cache()
   568         user.cw_clear_relation_cache()
   569         self._sessions[session.id] = session
   569         self._sessions[session.id] = session
   570         self.info('opened %s', session)
   570         self.info('opened %s', session)
   571         self.hm.call_hooks('session_open', session)
   571         self.hm.call_hooks('session_open', session)
   572         # commit session at this point in case write operation has been done
   572         # commit session at this point in case write operation has been done
   573         # during `session_open` hooks
   573         # during `session_open` hooks
  1019 
  1019 
  1020         the entity eid should originaly be None and a unique eid is assigned to
  1020         the entity eid should originaly be None and a unique eid is assigned to
  1021         the entity instance
  1021         the entity instance
  1022         """
  1022         """
  1023         # init edited_attributes before calling before_add_entity hooks
  1023         # init edited_attributes before calling before_add_entity hooks
  1024         entity._is_saved = False # entity has an eid but is not yet saved
  1024         entity._cw_is_saved = False # entity has an eid but is not yet saved
  1025         entity.edited_attributes = set(entity)
  1025         entity.edited_attributes = set(entity) # XXX cw_edited_attributes
  1026         entity_ = entity.pre_add_hook()
       
  1027         # XXX kill that transmutation feature !
       
  1028         if not entity_ is entity:
       
  1029             entity.__class__ = entity_.__class__
       
  1030             entity.__dict__.update(entity_.__dict__)
       
  1031         eschema = entity.e_schema
  1026         eschema = entity.e_schema
  1032         source = self.locate_etype_source(entity.__regid__)
  1027         source = self.locate_etype_source(entity.__regid__)
  1033         # allocate an eid to the entity before calling hooks
  1028         # allocate an eid to the entity before calling hooks
  1034         entity.set_eid(self.system_source.create_eid(session))
  1029         entity.eid = self.system_source.create_eid(session)
  1035         # set caches asap
  1030         # set caches asap
  1036         extid = self.init_entity_caches(session, entity, source)
  1031         extid = self.init_entity_caches(session, entity, source)
  1037         if server.DEBUG & server.DBG_REPO:
  1032         if server.DEBUG & server.DBG_REPO:
  1038             print 'ADD entity', entity.__regid__, entity.eid, dict(entity)
  1033             print 'ADD entity', entity.__regid__, entity.eid, dict(entity)
  1039         relations = []
  1034         relations = []
  1044         # affectation above)
  1039         # affectation above)
  1045         for attr in entity.iterkeys():
  1040         for attr in entity.iterkeys():
  1046             rschema = eschema.subjrels[attr]
  1041             rschema = eschema.subjrels[attr]
  1047             if not rschema.final: # inlined relation
  1042             if not rschema.final: # inlined relation
  1048                 relations.append((attr, entity[attr]))
  1043                 relations.append((attr, entity[attr]))
  1049         entity.set_defaults()
  1044         entity._cw_set_defaults()
  1050         if session.is_hook_category_activated('integrity'):
  1045         if session.is_hook_category_activated('integrity'):
  1051             entity.check(creation=True)
  1046             entity._cw_check(creation=True)
  1052         source.add_entity(session, entity)
  1047         source.add_entity(session, entity)
  1053         self.add_info(session, entity, source, extid, complete=False)
  1048         self.add_info(session, entity, source, extid, complete=False)
  1054         entity._is_saved = True # entity has an eid and is saved
  1049         entity._cw_is_saved = True # entity has an eid and is saved
  1055         # prefill entity relation caches
  1050         # prefill entity relation caches
  1056         for rschema in eschema.subject_relations():
  1051         for rschema in eschema.subject_relations():
  1057             rtype = str(rschema)
  1052             rtype = str(rschema)
  1058             if rtype in schema.VIRTUAL_RTYPES:
  1053             if rtype in schema.VIRTUAL_RTYPES:
  1059                 continue
  1054                 continue
  1060             if rschema.final:
  1055             if rschema.final:
  1061                 entity.setdefault(rtype, None)
  1056                 entity.setdefault(rtype, None)
  1062             else:
  1057             else:
  1063                 entity.set_related_cache(rtype, 'subject', session.empty_rset())
  1058                 entity.cw_set_relation_cache(rtype, 'subject',
       
  1059                                              session.empty_rset())
  1064         for rschema in eschema.object_relations():
  1060         for rschema in eschema.object_relations():
  1065             rtype = str(rschema)
  1061             rtype = str(rschema)
  1066             if rtype in schema.VIRTUAL_RTYPES:
  1062             if rtype in schema.VIRTUAL_RTYPES:
  1067                 continue
  1063                 continue
  1068             entity.set_related_cache(rtype, 'object', session.empty_rset())
  1064             entity.cw_set_relation_cache(rtype, 'object', session.empty_rset())
  1069         # set inline relation cache before call to after_add_entity
  1065         # set inline relation cache before call to after_add_entity
  1070         for attr, value in relations:
  1066         for attr, value in relations:
  1071             session.update_rel_cache_add(entity.eid, attr, value)
  1067             session.update_rel_cache_add(entity.eid, attr, value)
  1072         # trigger after_add_entity after after_add_relation
  1068         # trigger after_add_entity after after_add_relation
  1073         if source.should_call_hooks:
  1069         if source.should_call_hooks:
  1092         session.set_entity_cache(entity)
  1088         session.set_entity_cache(entity)
  1093         orig_edited_attributes = getattr(entity, 'edited_attributes', None)
  1089         orig_edited_attributes = getattr(entity, 'edited_attributes', None)
  1094         entity.edited_attributes = edited_attributes
  1090         entity.edited_attributes = edited_attributes
  1095         try:
  1091         try:
  1096             if session.is_hook_category_activated('integrity'):
  1092             if session.is_hook_category_activated('integrity'):
  1097                 entity.check()
  1093                 entity._cw_check()
  1098             only_inline_rels, need_fti_update = True, False
  1094             only_inline_rels, need_fti_update = True, False
  1099             relations = []
  1095             relations = []
  1100             source = self.source_from_eid(entity.eid, session)
  1096             source = self.source_from_eid(entity.eid, session)
  1101             for attr in list(edited_attributes):
  1097             for attr in list(edited_attributes):
  1102                 if attr == 'eid':
  1098                 if attr == 'eid':
  1130             if source.should_call_hooks:
  1126             if source.should_call_hooks:
  1131                 if not only_inline_rels:
  1127                 if not only_inline_rels:
  1132                     hm.call_hooks('after_update_entity', session, entity=entity)
  1128                     hm.call_hooks('after_update_entity', session, entity=entity)
  1133                 for attr, value, prevvalue in relations:
  1129                 for attr, value, prevvalue in relations:
  1134                     # if the relation is already cached, update existant cache
  1130                     # if the relation is already cached, update existant cache
  1135                     relcache = entity.relation_cached(attr, 'subject')
  1131                     relcache = entity.cw_relation_cached(attr, 'subject')
  1136                     if prevvalue is not None:
  1132                     if prevvalue is not None:
  1137                         hm.call_hooks('after_delete_relation', session,
  1133                         hm.call_hooks('after_delete_relation', session,
  1138                                       eidfrom=entity.eid, rtype=attr, eidto=prevvalue)
  1134                                       eidfrom=entity.eid, rtype=attr, eidto=prevvalue)
  1139                         if relcache is not None:
  1135                         if relcache is not None:
  1140                             session.update_rel_cache_del(entity.eid, attr, prevvalue)
  1136                             session.update_rel_cache_del(entity.eid, attr, prevvalue)
  1141                     del_existing_rel_if_needed(session, entity.eid, attr, value)
  1137                     del_existing_rel_if_needed(session, entity.eid, attr, value)
  1142                     if relcache is not None:
  1138                     if relcache is not None:
  1143                         session.update_rel_cache_add(entity.eid, attr, value)
  1139                         session.update_rel_cache_add(entity.eid, attr, value)
  1144                     else:
  1140                     else:
  1145                         entity.set_related_cache(attr, 'subject',
  1141                         entity.cw_set_relation_cache(attr, 'subject',
  1146                                                  session.eid_rset(value))
  1142                                                      session.eid_rset(value))
  1147                     hm.call_hooks('after_add_relation', session,
  1143                     hm.call_hooks('after_add_relation', session,
  1148                                   eidfrom=entity.eid, rtype=attr, eidto=value)
  1144                                   eidfrom=entity.eid, rtype=attr, eidto=value)
  1149         finally:
  1145         finally:
  1150             if orig_edited_attributes is not None:
  1146             if orig_edited_attributes is not None:
  1151                 entity.edited_attributes = orig_edited_attributes
  1147                 entity.edited_attributes = orig_edited_attributes