dataimport.py
branchstable
changeset 5054 cb066d29166a
parent 4913 083b4d454192
child 5063 2a94b61837e1
equal deleted inserted replaced
5053:2021b3237015 5054:cb066d29166a
   556             metagen = MetaGenerator(session, baseurl)
   556             metagen = MetaGenerator(session, baseurl)
   557         self.metagen = metagen
   557         self.metagen = metagen
   558         self._nb_inserted_entities = 0
   558         self._nb_inserted_entities = 0
   559         self._nb_inserted_types = 0
   559         self._nb_inserted_types = 0
   560         self._nb_inserted_relations = 0
   560         self._nb_inserted_relations = 0
   561         self.rql = session.unsafe_execute
   561         self.rql = session.execute
       
   562         # deactivate security
       
   563         session.set_read_security(False)
       
   564         session.set_write_security(False)
   562         # disable undoing
   565         # disable undoing
   563         session.undo_actions = frozenset()
   566         session.undo_actions = frozenset()
   564 
   567 
   565     def create_entity(self, etype, **kwargs):
   568     def create_entity(self, etype, **kwargs):
   566         for k, v in kwargs.iteritems():
   569         for k, v in kwargs.iteritems():
   568         entity, rels = self.metagen.base_etype_dicts(etype)
   571         entity, rels = self.metagen.base_etype_dicts(etype)
   569         entity = copy(entity)
   572         entity = copy(entity)
   570         entity._related_cache = {}
   573         entity._related_cache = {}
   571         self.metagen.init_entity(entity)
   574         self.metagen.init_entity(entity)
   572         entity.update(kwargs)
   575         entity.update(kwargs)
       
   576         entity.edited_attributes = set(entity)
   573         session = self.session
   577         session = self.session
   574         self.source.add_entity(session, entity)
   578         self.source.add_entity(session, entity)
   575         self.source.add_info(session, entity, self.source, complete=False)
   579         self.source.add_info(session, entity, self.source, None, complete=False)
   576         for rtype, targeteids in rels.iteritems():
   580         for rtype, targeteids in rels.iteritems():
   577             # targeteids may be a single eid or a list of eids
   581             # targeteids may be a single eid or a list of eids
   578             inlined = self.rschema(rtype).inlined
   582             inlined = self.rschema(rtype).inlined
   579             try:
   583             try:
   580                 for targeteid in targeteids:
   584                 for targeteid in targeteids:
   619         self.baseurl =  baseurl
   623         self.baseurl =  baseurl
   620         # attributes/relations shared by all entities of the same type
   624         # attributes/relations shared by all entities of the same type
   621         self.etype_attrs = []
   625         self.etype_attrs = []
   622         self.etype_rels = []
   626         self.etype_rels = []
   623         # attributes/relations specific to each entity
   627         # attributes/relations specific to each entity
   624         self.entity_attrs = ['eid', 'cwuri']
   628         self.entity_attrs = ['cwuri']
   625         #self.entity_rels = [] XXX not handled (YAGNI?)
   629         #self.entity_rels = [] XXX not handled (YAGNI?)
   626         schema = session.vreg.schema
   630         schema = session.vreg.schema
   627         rschema = schema.rschema
   631         rschema = schema.rschema
   628         for rtype in META_RTYPES:
   632         for rtype in META_RTYPES:
   629             if rtype in ('eid', 'cwuri') or rtype in VIRTUAL_RTYPES:
   633             if rtype in ('eid', 'cwuri') or rtype in VIRTUAL_RTYPES:
   648         for rel in self.etype_rels:
   652         for rel in self.etype_rels:
   649             rels[rel] = self.generate(entity, rel)
   653             rels[rel] = self.generate(entity, rel)
   650         return entity, rels
   654         return entity, rels
   651 
   655 
   652     def init_entity(self, entity):
   656     def init_entity(self, entity):
       
   657         entity.eid = self.source.create_eid(self.session)
   653         for attr in self.entity_attrs:
   658         for attr in self.entity_attrs:
   654             entity[attr] = self.generate(entity, attr)
   659             entity[attr] = self.generate(entity, attr)
   655         entity.eid = entity['eid']
       
   656 
   660 
   657     def generate(self, entity, rtype):
   661     def generate(self, entity, rtype):
   658         return getattr(self, 'gen_%s' % rtype)(entity)
   662         return getattr(self, 'gen_%s' % rtype)(entity)
   659 
   663 
   660     def gen_eid(self, entity):
       
   661         return self.source.create_eid(self.session)
       
   662 
       
   663     def gen_cwuri(self, entity):
   664     def gen_cwuri(self, entity):
   664         return u'%seid/%s' % (self.baseurl, entity['eid'])
   665         return u'%seid/%s' % (self.baseurl, entity.eid)
   665 
   666 
   666     def gen_creation_date(self, entity):
   667     def gen_creation_date(self, entity):
   667         return self.time
   668         return self.time
   668     def gen_modification_date(self, entity):
   669     def gen_modification_date(self, entity):
   669         return self.time
   670         return self.time