本文整理汇总了Python中grano.model.Entity类的典型用法代码示例。如果您正苦于以下问题:Python Entity类的具体用法?Python Entity怎么用?Python Entity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Entity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
def save(self):
""" Save the entity to the database. Do this only once, after all
properties have been set. """
# fetch existing:
q = Entity.all()
q = q.filter(Entity.project==self.loader.project)
for name, only_active in self.update_criteria:
value = self.properties.get(name).get('value')
attr = self.loader.project.get_attribute('entity', name)
q = Entity._filter_property(q, [attr], value,
only_active=only_active)
entity = q.first()
try:
data = {
'project': self.loader.project,
'author': self.loader.account,
'schemata': self.schemata,
'properties': self.properties
}
self._entity = entities.save(data, entity=entity)
except Invalid, inv:
log.warning("Validation error: %r", inv)
示例2: save
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
示例3: apply_alias
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')
# 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:
properties_logic.set(alias, author, 'name', schema, canonical_name,
active=True, source_url=None)
_entity_changed.delay(alias.id)
return log.info("Renamed: %s", alias_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)
示例4: view
def view(id, slug=None):
entity = Entity.by_id(id)
if entity is None:
raise NotFound()
if entity.same_as is not None:
canonical = Entity.by_id(entity.same_as)
return redirect(entity_link(canonical))
inbound_sections = []
slug = url_slug(entity['name'].value)
for schema in entity.inbound_schemata:
pager_name = schema.name + '_in'
pager = Pager(entity.inbound_by_schema(schema), pager_name, id=id, slug=slug, limit=15)
inbound_sections.append((schema, pager))
outbound_sections = []
for schema in entity.outbound_schemata:
pager_name = schema.name + '_out'
pager = Pager(entity.outbound_by_schema(schema), pager_name, id=id, slug=slug, limit=15)
outbound_sections.append((schema, pager))
canonical_url = entity_link(entity, **dict(request.args.items()))
entity_hairball = app.config.get('ENTITY_HAIRBALL', True)
return render_template('entity.html', entity=entity,
canonical_url=canonical_url,
entity_hairball=entity_hairball,
inbound_sections=inbound_sections,
outbound_sections=outbound_sections,
render_relation=render_relation)
示例5: setUp
def setUp(self):
self.app = make_test_app()
Entity.all().delete()
# Consistently include an extra private project with Entity
# that should not show in any test results
project, permission = _project_and_permission(private=True)
entity = Entity(project=project, status=authz.PUBLISHED_THRESHOLD)
db.session.add(entity)
示例6: decode
def decode(self, node, cstruct):
if isinstance(cstruct, Entity):
if cstruct.project == self.project:
return cstruct
if isinstance(cstruct, basestring):
entity = Entity.by_id(cstruct)
if entity.project == self.project:
return entity
if isinstance(cstruct, dict):
if cstruct.get('id'):
entity = Entity.by_id(cstruct.get('id'))
if entity.project == self.project:
return entity
return None
示例7: rebuild
def rebuild():
""" Execute the change processing handlers for all entities and
relations currently loaded. This can be used as a housekeeping
function. """
for project in Project.all():
_project_changed(project.slug, 'delete')
_project_changed(project.slug, 'create')
for schema in project.schemata:
_schema_changed(schema.project.slug, schema.name, 'delete')
_schema_changed(schema.project.slug, schema.name, 'create')
eq = Entity.all().filter_by(same_as=None)
eq = eq.filter_by(project=project)
for i, entity in enumerate(eq):
if i > 0 and i % 1000 == 0:
log.info("Rebuilt: %s entities", i)
_entity_changed(entity.id, 'delete')
_entity_changed(entity.id, 'create')
rq = Relation.all().filter_by(project=project)
for i, relation in enumerate(rq):
if i > 0 and i % 1000 == 0:
log.info("Rebuilt: %s relation", i)
_relation_changed(relation.id, 'delete')
_relation_changed(relation.id, 'create')
示例8: update
def update(id):
entity = object_or_404(Entity.by_id(id))
authz.require(authz.project_edit(entity.project))
data = request_data({'author': request.account})
entity = entities.save(data, entity=entity)
db.session.commit()
return jsonify(entities.to_rest(entity))
示例9: save
def save(self):
""" Save the relation to the database. Do this only once, after all
properties have been set. """
# fetch existing:
q = Relation.all()
q = q.filter(Relation.project==self.loader.project)
q = q.filter(Relation.source==self.source.entity)
q = q.filter(Relation.target==self.target.entity)
for name, only_active in self.update_criteria:
value = self.properties.get(name).get('value')
q = Entity._filter_property(q, name, value,
only_active=only_active)
relation = q.first()
try:
data = {
'project': self.loader.project,
'author': self.loader.account,
'schema': self.schemata.pop(),
'properties': self.properties,
'source': self.source.entity,
'target': self.target.entity
}
self._relation = relations.save(data, relation=relation)
except Invalid, inv:
log.warning("Validation error: %r", inv)
示例10: graph
def graph(id):
entity = object_or_404(Entity.by_id(id))
extractor = GraphExtractor(root_id=entity.id)
validate_cache(keys=extractor.to_hash())
if extractor.format == 'gexf':
return Response(extractor.to_gexf(),
mimetype='text/xml')
return jsonify(extractor.to_dict())
示例11: index_single
def index_single(entity_id):
""" Index a single entity. """
entity = Entity.by_id(entity_id)
if entity.same_as is not None:
return
log.debug("Indexing: %s", entity['name'].value)
body = entities.to_index(entity)
es.index(index=es_index, doc_type='entity', id=body.pop('id'), body=body)
示例12: entity_changed
def entity_changed(self, entity_id, operation):
if operation == 'delete':
es.delete(index=es_index, doc_type='entity', id=entity_id)
else:
entity = Entity.by_id(entity_id)
if entity is None:
return
self.index_entity(entity)
es.indices.refresh(index=es_index)
示例13: save
def save(data, entity=None):
""" Save or update an entity. """
data = validate(data)
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')))
properties_logic.set_many(entity, data.get('author'),
data.get('properties'))
db.session.flush()
_entity_changed.delay(entity.id)
return entity
示例14: search
def search():
# TODO: move to be project-specific, the implement access control!
searcher = ESSearcher(request.args)
if 'project' in request.args:
searcher.add_filter('project.slug', request.args.get('project'))
pager = Pager(searcher)
# TODO: get all entities at once:
conv = lambda res: [entities.to_rest_index(Entity.by_id(r.get('id'))) for r in res]
data = pager.to_dict(results_converter=conv)
data['facets'] = searcher.facets()
return jsonify(data)
示例15: index
def index():
query = filter_query(Entity, Entity.all(), request.args)
for schema in request.args.getlist('schema'):
alias = aliased(Schema)
query = query.join(alias, Entity.schemata)
query = query.filter(alias.name.in_(schema.split(',')))
pager = Pager(query)
conv = lambda es: [entities.to_rest_index(e) for e in es]
return jsonify(pager.to_dict(conv))