server/repository.py
changeset 6464 11f9fbf6a645
parent 6427 c8a5ac2d1eaa
child 6465 6401a9d0b5aa
equal deleted inserted replaced
6463:67b0ad068f5d 6464:11f9fbf6a645
    52                       UnknownEid, AuthenticationError, ExecutionError,
    52                       UnknownEid, AuthenticationError, ExecutionError,
    53                       ETypeNotSupportedBySources, MultiSourcesError,
    53                       ETypeNotSupportedBySources, MultiSourcesError,
    54                       BadConnectionId, Unauthorized, ValidationError,
    54                       BadConnectionId, Unauthorized, ValidationError,
    55                       RepositoryError, UniqueTogetherError, typed_eid, onevent)
    55                       RepositoryError, UniqueTogetherError, typed_eid, onevent)
    56 from cubicweb import cwvreg, schema, server
    56 from cubicweb import cwvreg, schema, server
       
    57 from cubicweb.entity import prefill_entity_caches
    57 from cubicweb.server import utils, hook, pool, querier, sources
    58 from cubicweb.server import utils, hook, pool, querier, sources
    58 from cubicweb.server.session import Session, InternalSession, InternalManager, \
    59 from cubicweb.server.session import Session, InternalSession, InternalManager, \
    59      security_enabled
    60      security_enabled
    60 from cubicweb.server.ssplanner import EditedEntity
    61 from cubicweb.server.ssplanner import EditedEntity
    61 
    62 
  1167             if not rschema.final: # inlined relation
  1168             if not rschema.final: # inlined relation
  1168                 relations.append((attr, edited[attr]))
  1169                 relations.append((attr, edited[attr]))
  1169         edited.set_defaults()
  1170         edited.set_defaults()
  1170         if session.is_hook_category_activated('integrity'):
  1171         if session.is_hook_category_activated('integrity'):
  1171             edited.check(creation=True)
  1172             edited.check(creation=True)
       
  1173         prefill_entity_caches(entity, relations)
  1172         try:
  1174         try:
  1173             source.add_entity(session, entity)
  1175             source.add_entity(session, entity)
  1174         except UniqueTogetherError, exc:
  1176         except UniqueTogetherError, exc:
  1175             etype, rtypes = exc.args
  1177             etype, rtypes = exc.args
  1176             problems = {}
  1178             problems = {}
  1177             for col in rtypes:
  1179             for col in rtypes:
  1178                 problems[col] = session._('violates unique_together constraints (%s)') % (','.join(rtypes))
  1180                 problems[col] = session._('violates unique_together constraints (%s)') % (','.join(rtypes))
  1179             raise ValidationError(entity.eid, problems)
  1181             raise ValidationError(entity.eid, problems)
  1180         self.add_info(session, entity, source, extid, complete=False)
  1182         self.add_info(session, entity, source, extid, complete=False)
  1181         edited.saved = entity._cw_is_saved = True
  1183         edited.saved = entity._cw_is_saved = True
  1182         # prefill entity relation caches
       
  1183         for rschema in eschema.subject_relations():
       
  1184             rtype = str(rschema)
       
  1185             if rtype in schema.VIRTUAL_RTYPES:
       
  1186                 continue
       
  1187             if rschema.final:
       
  1188                 entity.cw_attr_cache.setdefault(rtype, None)
       
  1189             else:
       
  1190                 entity.cw_set_relation_cache(rtype, 'subject',
       
  1191                                              session.empty_rset())
       
  1192         for rschema in eschema.object_relations():
       
  1193             rtype = str(rschema)
       
  1194             if rtype in schema.VIRTUAL_RTYPES:
       
  1195                 continue
       
  1196             entity.cw_set_relation_cache(rtype, 'object', session.empty_rset())
       
  1197         # set inlined relation cache before call to after_add_entity
       
  1198         for attr, value in relations:
       
  1199             session.update_rel_cache_add(entity.eid, attr, value)
       
  1200             del_existing_rel_if_needed(session, entity.eid, attr, value)
       
  1201         # trigger after_add_entity after after_add_relation
  1184         # trigger after_add_entity after after_add_relation
  1202         if source.should_call_hooks:
  1185         if source.should_call_hooks:
  1203             self.hm.call_hooks('after_add_entity', session, entity=entity)
  1186             self.hm.call_hooks('after_add_entity', session, entity=entity)
  1204             # call hooks for inlined relations
  1187             # call hooks for inlined relations
  1205             for attr, value in relations:
  1188             for attr, value in relations: