def save(data, entity=None): """ Save or update an entity. """ data = validate(data, entity) operation = 'create' if entity is None else 'update' if entity is None: entity = Entity() entity.project = data.get('project') entity.author = data.get('author') db.session.add(entity) entity.schemata = list(set(data.get('schemata'))) prop_names = set() for name, prop in data.get('properties').items(): prop_names.add(name) prop['name'] = name prop['author'] = data.get('author') properties_logic.save(entity, prop) for prop in entity.properties: if prop.name not in prop_names: prop.active = False db.session.flush() _entity_changed.delay(entity.id, operation) return entity
def save(data, files=None, entity=None): """ Save or update an entity. """ data = validate(data, entity) operation = 'create' if entity is None else 'update' if entity is None: entity = Entity() entity.project = data.get('project') entity.author = data.get('author') db.session.add(entity) entity.schema = data.get('schema') prop_names = set() for name, prop in data.get('properties').items(): prop_names.add(name) prop['project'] = entity.project prop['name'] = name prop['author'] = data.get('author') properties_logic.save(entity, prop, files=files) for prop in entity.properties: if prop.name not in prop_names: prop.active = False db.session.flush() _entity_changed.delay(entity.id, operation) return entity
def save(data, relation=None): """ Save or update a relation with the given properties. """ data = validate(data, relation) operation = 'create' if relation is None else 'update' if relation is None: relation = Relation() relation.project = data.get('project') relation.author = data.get('author') db.session.add(relation) relation.source = data.get('source') relation.target = data.get('target') relation.schema = data.get('schema') prop_names = set() for name, prop in data.get('properties').items(): prop_names.add(name) prop['name'] = name prop['author'] = data.get('author') properties_logic.save(relation, prop) for prop in relation.properties: if prop.name not in prop_names: prop.active = False db.session.flush() _relation_changed.delay(relation.id, operation) return relation
def save(data, relation=None): """ Save or update a relation with the given properties. """ data = validate(data, relation) operation = "create" if relation is None else "update" if relation is None: relation = Relation() relation.project = data.get("project") relation.author = data.get("author") db.session.add(relation) relation.source = data.get("source") relation.target = data.get("target") relation.schema = data.get("schema") prop_names = set() for name, prop in data.get("properties").items(): prop_names.add(name) prop["name"] = name prop["author"] = data.get("author") properties_logic.save(relation, prop) for prop in relation.properties: if prop.name not in prop_names: prop.active = False db.session.flush() _relation_changed.delay(relation.id, operation) return relation
def apply_alias(project, author, canonical_name, alias_name): """ Given two names, find out if there are existing entities for one or both of them. If so, merge them into a single entity - or, if only the entity associated with the alias exists - re-name the entity. """ canonical_name = canonical_name.strip() # Don't import meaningless aliases. if canonical_name == alias_name or not len(canonical_name): return log.info("No alias: %s", canonical_name) canonical = Entity.by_name(project, canonical_name) alias = Entity.by_name(project, alias_name) schema = Schema.by_name(project, 'base') attribute = schema.get_attribute('name') # Don't artificially increase entity counts. if canonical is None and alias is None: return log.info("Neither alias nor canonical exist: %s", canonical_name) # Rename an alias to its new, canonical name. if canonical is None: data = { 'value': canonical_name, 'schema': schema, 'attribute': attribute, 'active': True, 'name': 'name', 'source_url': None } properties_logic.save(alias, data) _entity_changed.delay(alias.id) return log.info("Renamed: %s -> %s", alias_name, canonical_name) # Already done, thanks. if canonical == alias: return log.info("Already aliased: %s", canonical_name) # Merge two existing entities, declare one as "same_as" if canonical is not None and alias is not None: _merge_entities(alias, canonical) _entity_changed.delay(canonical.id) return log.info("Mapped: %s -> %s", alias.id, canonical.id)
def apply_alias(project, author, canonical_name, alias_name, source_url=None): """ Given two names, find out if there are existing entities for one or both of them. If so, merge them into a single entity - or, if only the entity associated with the alias exists - re-name the entity. """ # Don't import meaningless aliases. if not len(canonical_name) or not len(alias_name): return log.info("Not an alias: %s", canonical_name) canonical = None # de-duplicate existing entities with the same name. known_names = set() for existing in Entity.by_name_many(project, canonical_name): for prop in existing.properties: if prop.name != 'name': continue known_names.add(prop.value) # make sure the canonical name is actually active # TODO: is this desirable? if prop.value == canonical_name: prop.active = True else: prop.active = False if canonical is not None and canonical.id != existing.id: canonical = merge(existing, canonical) else: canonical = existing schema = Schema.by_name(project, 'base') attribute = schema.get_attribute('name') # Find aliases, i.e. entities with the alias name which are not # the canonical entity. q = Entity.by_name_many(project, alias_name) if canonical is not None: q = q.filter(Entity.id != canonical.id) aliases = q.all() # If there are no existing aliases with that name, add the alias # name to the canonical entity. if not len(aliases) and canonical is not None: if alias_name not in known_names: data = { 'value': alias_name, 'schema': schema, 'attribute': attribute, 'active': False, 'name': 'name', 'source_url': source_url } properties_logic.save(canonical, data) _entity_changed.delay(canonical.id, 'update') log.info("Alias: %s -> %s", alias_name, canonical_name) for alias in aliases: if canonical is None: # Rename an alias to its new, canonical name. data = { 'value': canonical_name, 'schema': schema, 'attribute': attribute, 'active': True, 'name': 'name', 'source_url': source_url } properties_logic.save(alias, data) _entity_changed.delay(alias.id, 'update') log.info("Renamed: %s -> %s", alias_name, canonical_name) else: # Merge two existing entities, declare one as "same_as" merge(alias, canonical) log.info("Mapped: %s -> %s", alias.id, canonical.id) db.session.commit()
def apply_alias(project, author, canonical_name, alias_name, source_url=None): """ Given two names, find out if there are existing entities for one or both of them. If so, merge them into a single entity - or, if only the entity associated with the alias exists - re-name the entity. """ # Don't import meaningless aliases. if not len(canonical_name) or not len(alias_name): return log.info("Not an alias: %s", canonical_name) canonical = None # de-duplicate existing entities with the same name. known_names = set() for existing in Entity.by_name_many(project, canonical_name): for prop in existing.properties: if prop.name != 'name': continue known_names.add(prop.value) # make sure the canonical name is actually active if prop.value == canonical_name: prop.active = True else: prop.active = False if canonical is not None and canonical.id != existing.id: canonical = merge(existing, canonical) else: canonical = existing # Find aliases, i.e. entities with the alias name which are not # the canonical entity. q = Entity.by_name_many(project, alias_name) if canonical is not None: q = q.filter(Entity.id != canonical.id) aliases = q.all() # If there are no existing aliases with that name, add the alias # name to the canonical entity. if not len(aliases) and canonical is not None: if alias_name not in known_names: data = { 'value': alias_name, 'active': False, 'name': 'name', 'source_url': source_url } properties_logic.save(canonical, data) _entity_changed.delay(canonical.id, 'update') log.info("Alias: %s -> %s", alias_name, canonical_name) for alias in aliases: if canonical is None: # Rename an alias to its new, canonical name. data = { 'value': canonical_name, 'active': True, 'name': 'name', 'source_url': source_url } properties_logic.save(alias, data) _entity_changed.delay(alias.id, 'update') log.info("Renamed: %s -> %s", alias_name, canonical_name) else: # Merge two existing entities, declare one as "same_as" merge(alias, canonical) log.info("Mapped: %s -> %s", alias.id, canonical.id) db.session.commit()