Example #1
0
    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

        self.schema = model.precise_schema(self.schema, other.schema)
        self.foreign_ids = string_set(self.foreign_ids, self.foreign_ids)
        self.created_at = min((self.created_at, other.created_at))
        self.updated_at = datetime.utcnow()

        data = merge_data(self.data, other.data)
        if self.name != other.name:
            data = merge_data(data, {'alias': [other.name]})
        self.data = data

        # update alerts
        from aleph.model.alert import Alert
        q = db.session.query(Alert).filter(Alert.entity_id == other.id)
        q.update({Alert.entity_id: self.id})

        # delete source entities
        other.delete()
        db.session.add(self)
        db.session.commit()
        db.session.refresh(other)
Example #2
0
 def create(cls, data, collection):
     foreign_ids = string_set(data.get('foreign_ids'))
     ent = cls.by_foreign_ids(foreign_ids, collection.id, deleted=True)
     if ent is None:
         ent = cls()
         ent.id = make_textid()
         ent.collection = collection
         ent.foreign_ids = foreign_ids
     ent.update(data)
     ent.deleted_at = None
     return ent