本文整理汇总了Python中grano.model.Entity.by_name_many方法的典型用法代码示例。如果您正苦于以下问题:Python Entity.by_name_many方法的具体用法?Python Entity.by_name_many怎么用?Python Entity.by_name_many使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grano.model.Entity
的用法示例。
在下文中一共展示了Entity.by_name_many方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: apply_alias
# 需要导入模块: from grano.model import Entity [as 别名]
# 或者: from grano.model.Entity import by_name_many [as 别名]
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()