def merge(self, other): if self.id == other.id: raise ValueError("Cannot merge an entity with itself.") if self.collection_id != other.collection_id: raise ValueError( "Cannot merge entities from different collections.") # noqa data = merge_data(self.data, other.data) if self.name.lower() != other.name.lower(): data = merge_data(data, {'alias': [other.name]}) self.data = data self.state = self.STATE_ACTIVE self.foreign_ids = self.foreign_ids or [] self.foreign_ids += other.foreign_ids or [] self.created_at = min((self.created_at, other.created_at)) self.updated_at = datetime.utcnow() # update alerts from aleph.model.alert import Alert q = db.session.query(Alert).filter(Alert.entity_id == other.id) q.update({'entity_id': self.id}) # update document references from aleph.model.reference import Reference q = db.session.query(Reference).filter(Reference.entity_id == other.id) q.update({'entity_id': self.id}) # delete source entities other.delete() db.session.add(self) db.session.commit() db.session.refresh(other)
def update(id): _, entity = get_entity(id, request.authz.WRITE) data = parse_request(schema=EntitySchema) if arg_bool('merge'): data['data'] = merge_data(data.get('data') or {}, entity.data or {}) entity.update(data) db.session.commit() update_entity(entity) update_collection(entity.collection) return view(entity.id)
def combined_entity(entity): """Use EntityIdentity mappings to construct a combined model of the entity with all data applied.""" if 'id' not in entity: return entity if 'ids' not in entity: entity['ids'] = EntityIdentity.entity_ids(entity['id']) combined = dict(entity) for mapped_id in entity['ids']: if mapped_id == entity['id']: continue mapped = load_entity(mapped_id) if mapped is None: continue combined = merge_data(combined, mapped) return combined
def save(cls, data, collection, merge=False): ent = cls.by_id(data.get('id')) if ent is None: ent = cls() ent.type = data.pop('schema', None) if ent.type is None: raise ValueError("No schema provided.") ent.id = make_textid() if merge: data = merge_data(data, ent.to_dict()) if collection is None: raise ValueError("No collection specified.") ent.collection = collection ent.update(data) return ent