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)
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
def update(id): entity = object_or_404(Entity.by_id(id)) authz.require(authz.entity_edit(entity)) data = request_data({'author': request.account}) entity = entities.save(data, files=request.files, entity=entity) db.session.commit() return jsonify(entity)
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))
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 self.project is None: return entity if entity is not None and entity.project == self.project: return entity return None
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)
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())
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)
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)
def graph(id): entity = object_or_404(Entity.by_id(id)) authz.require(authz.entity_read(entity)) entity_properties = request.args.getlist('entity_property') extractor = GraphExtractor(root_id=entity.id, entity_properties=entity_properties) validate_cache(keys=extractor.to_hash()) if extractor.format == 'gexf': return Response(extractor.to_gexf(), mimetype='text/xml') return jsonify(extractor)
def merge(orig, dest): """ Copy all properties and relations from one entity onto another, then mark the source entity as an ID alias for the destionation entity. """ if orig.id == dest.id: return orig if dest.same_as == orig.id: return orig if orig.same_as == dest.id: return dest if dest.same_as is not None: # potential infinite recursion here. resolved_dest = Entity.by_id(dest.same_as) if resolved_dest is not None: return merge(orig, resolved_dest) schemata, seen_schemata = list(), set() for schema in dest.schemata + orig.schemata: if schema.id in seen_schemata: continue seen_schemata.add(schema.id) schemata.append(schema) dest.schemata = schemata dest_active = [p.name for p in dest.active_properties] for prop in orig.properties: if prop.name in dest_active: prop.active = False prop.entity = dest for rel in orig.inbound: rel.target = dest for rel in orig.outbound: rel.source = dest orig.same_as = dest.id dest.same_as = None db.session.flush() _entity_changed.delay(dest.id, 'update') _entity_changed.delay(orig.id, 'delete') return dest
def merge(source, dest): """ Copy all properties and relations from one entity onto another, then mark the source entity as an ID alias for the destionation entity. """ if source.id == dest.id: return source if dest.same_as == source.id: return source if source.same_as == dest.id: return dest if dest.same_as is not None: # potential infinite recursion here. canonical = Entity.by_id(dest.same_as) if canonical is not None: return merge(source, canonical) source_schema = source.schema if dest.schema.is_parent(source_schema): dest.schema = source_schema else: source_schema = dest.schema.common_parent(source_schema) source_valid = [a.name for a in source_schema.attributes] dest_active = [p.name for p in dest.active_properties] for prop in source.properties: if prop.name in source_valid: if prop.name in dest_active: prop.active = False prop.entity = dest else: properties_logic.delete(prop) for rel in source.inbound: rel.target = dest for rel in source.outbound: rel.source = dest source.same_as = dest.id db.session.flush() _entity_changed.delay(dest.id, 'update') _entity_changed.delay(source.id, 'delete') return dest
def view(id): entity = object_or_404(Entity.by_id(id)) authz.require(authz.project_read(entity.project)) return jsonify(entities.to_rest(entity))
def delete(id): entity = object_or_404(Entity.by_id(id)) authz.require(authz.project_edit(entity.project)) entities.delete(entity) db.session.commit() raise Gone()
def entities_index(obj_id): query = Property.all() query = query.filter(Property.relation_id != None) obj = Entity.by_id(obj_id) query = query.filter_by(entity_id=obj_id) return _index(query, obj)
def view(id): entity = object_or_404(Entity.by_id(id)) authz.require(authz.entity_read(entity)) return jsonify(entity)
def entities_index(obj_id): query = EntityProperty.all() obj = Entity.by_id(obj_id) query = query.filter_by(entity_id=obj_id) return _index(query, obj)
def view(id): entity = object_or_404(Entity.by_id(id)) return jsonify(entities.to_rest(entity))
def delete(id): entity = object_or_404(Entity.by_id(id)) authz.require(authz.entity_edit(entity)) entities.delete(entity) db.session.commit() raise Gone()