def run(slug, name): """ Get a JSON representation of stored queries. """ # TODO: Use read-only DB connection network, query = _get_query(slug, name) try: limit = int(request.args.get('limit', 100)) offset = int(request.args.get('offset', 0)) rp = query.run(**dict(request.args.items())) except Exception as exc: return jsonify({'error': unicode(exc), 'query': query}, status=400) result = [] for i in count(): row = rp.fetchone() if row is None or i >= limit + offset: break if i < offset: continue row = dict(zip(rp.keys(), row)) result.append(row) data = { 'results': result, 'count': rp.rowcount, 'query': query} request.cache_key['res'] = \ [sorted(r.values()) for r in data['results']] request.cache_key['count'] = data['count'] return jsonify(data)
def handle_exceptions(exc): """ Re-format exceptions to JSON if accept requires that. """ format = response_format(app, request) if format == "json": body = {"status": exc.code, "name": exc.name, "description": exc.get_description(request.environ)} return jsonify(body, status=exc.code, headers=exc.get_headers(request.environ)) return exc
def handle_validation_error(exc): if 'json' == response_format(app, request): body = {'status': 400, 'description': unicode(exc), 'errors': exc.asdict()} return jsonify(body, status=400) return Response(repr(exc.asdict()), status=400, mimetype='text/plain')
def update(slug, type, name): network = _get_network(slug) schema = _get_schema(network, type, name) require.schema.update(network, schema) data = request_content(request) data = validate_schema(dict(data.items())) schema.update(network, type, data) db.session.commit() return jsonify(schema, status=202)
def handle_validation_error(exc): if 'json' == response_format(app, request): body = { 'status': 400, 'description': unicode(exc), 'errors': exc.asdict() } return jsonify(body, status=400) return Response(repr(exc.asdict()), status=400, mimetype='text/plain')
def index(slug, type): """ List all available schemata for a type. """ network = _get_network(slug) require.schema.list(network) _valid_schema(type) schemata = { Schema.ENTITY: network.entity_schemata, Schema.RELATION: network.relation_schemata }.get(type) return jsonify(schemata)
def create(): """ Create a new network. """ require.network.create() data = request_content(request) context = ValidationContext() data = validate_network(dict(data.items()), context) network = Network.create(data) db.session.commit() url = url_for(".get", slug=network.slug) return jsonify(network, status=201, headers={"location": url})
def update(slug, name): """ Update the data of the entity. """ network, query = _get_query(slug, name) require.query.update(network, query) context = ValidationContext(network=network, query=query) data = dict(request_content(request).items()) data = validate_query(dict(data.items()), context) query.update(data) db.session.commit() return jsonify(query, status=202)
def update(slug): """ Update the data of the network. """ network = _get_network(slug) require.network.update(network) data = request_content(request) context = ValidationContext(network=network) data = validate_network(dict(data.items()), context) network.update(data) db.session.commit() return jsonify(network)
def create(slug): """ Create a new query. """ network = _get_network(slug) require.query.create(network) data = request_content(request) context = ValidationContext(network=network) data = validate_query(dict(data.items()), context) query = Query.create(network, data) db.session.commit() url = url_for('.get', slug=network.slug, name=query.name) return jsonify(query, status=201, headers={'location': url})
def update(slug): """ Update the data of the network. """ network = _get_network(slug) require.network.update(network) data = request_content(request) context = ValidationContext(network=network) data = validate_network(dict(data.items()), \ context) network.update(data) db.session.commit() return jsonify(network)
def index(slug): """ List all available relations. """ network = _get_network(slug) require.relation.list(network) type_name = request.args.get("type", None) type_ = _get_schema(network, type_name).cls if type_name else network.Relation count, query = filtered_query(type_, request) data = {"results": query.all(), "count": count} request.cache_key["ids"] = [r.id for r in data["results"]] request.cache_key["count"] = data["count"] return jsonify(data)
def index(slug): """ List all available entities. """ network = _get_network(slug) require.entity.list(network) type_name = request.args.get('type', None) type_ = _get_schema(network, type_name).cls if type_name else network.Entity count, query = filtered_query(type_, request, fts=True) data = {'results': query.all(), 'count': count} request.cache_key['ids'] = [r.id for r in data['results']] request.cache_key['count'] = data['count'] return jsonify(data)
def create(): """ Create a new network. """ require.network.create() data = request_content(request) context = ValidationContext() data = validate_network(dict(data.items()), \ context) network = Network.create(data) db.session.commit() url = url_for('.get', slug=network.slug) return jsonify(network, status=201, headers={'location': url})
def update(slug, id): """ Update the data of the relation. """ network, relation = _get_relation(slug, id) require.relation.update(network, relation) data = dict(request_content(request).items()) data['type'] = relation.type context = ValidationContext(network=network) schema = _get_schema(network, data.get('type')) data = validate_relation(data, schema, context) updated_relation = relation.update(schema, data) db.session.commit() return jsonify(updated_relation)
def create(slug, type): """ Create a new schema. """ network = _get_network(slug) require.schema.create(network) _valid_schema(type) data = request_content(request) data = validate_schema(dict(data.items())) schema = Schema.create(network, type, data) db.session.commit() url = url_for('.get', slug=network.slug, type=schema.entity, name=schema.name) return jsonify(schema, status=201, headers={'location': url})
def update(slug, id): """ Update the data of the relation. """ network, relation = _get_relation(slug, id) require.relation.update(network, relation) data = dict(request_content(request).items()) data["type"] = relation.type context = ValidationContext(network=network) schema = _get_schema(network, data.get("type")) data = validate_relation(data, schema, context) updated_relation = relation.update(schema, data) db.session.commit() return jsonify(updated_relation)
def index(slug): """ List all available relations. """ network = _get_network(slug) require.relation.list(network) type_name = request.args.get('type', None) type_ = _get_schema(network, type_name).cls if type_name else network.Relation count, query = filtered_query(type_, request) data = {'results': query.all(), 'count': count} request.cache_key['ids'] = [r.id for r in data['results']] request.cache_key['count'] = data['count'] return jsonify(data)
def create(slug): """ Create a new relation. """ network = _get_network(slug) require.relation.create(network) data = request_content(request) context = ValidationContext(network=network) schema = _get_schema(network, data.get("type")) data = validate_relation(dict(data.items()), schema, context) relation = network.Relation.create(schema, data) db.session.commit() url = url_for(".get", slug=network.slug, id=relation.id) return jsonify(relation, status=201, headers={"location": url})
def update(slug, id): """ Update the data of the entity. """ network, entity = _get_entity(slug, id) require.entity.update(network, entity) data = dict(request_content(request).items()) data['type'] = entity.type context = ValidationContext(network=network) schema = _get_schema(network, data.get('type')) data = validate_entity(data, schema, context) updated_entity = entity.update(schema, data) _deep_create(data, updated_entity, network) db.session.commit() return jsonify(updated_entity, status=202)
def create(slug): """ Create a new entity. """ network = _get_network(slug) require.entity.create(network) data = request_content(request) context = ValidationContext(network=network) schema = _get_schema(network, data.get('type')) data = validate_entity(dict(data.items()), schema, context) entity = network.Entity.create(schema, data) _deep_create(data, entity, network) db.session.commit() url = url_for('.get', slug=network.slug, id=entity.id) return jsonify(entity, status=201, headers={'location': url})
def handle_exceptions(exc): """ Re-format exceptions to JSON if accept requires that. """ format = response_format(app, request) if format == 'json': body = { 'status': exc.code, 'name': exc.name, 'description': exc.get_description(request.environ) } return jsonify(body, status=exc.code, headers=exc.get_headers(request.environ)) return exc
def create(slug): """ Create a new relation. """ network = _get_network(slug) require.relation.create(network) data = request_content(request) context = ValidationContext(network=network) schema = _get_schema(network, data.get('type')) data = validate_relation(dict(data.items()), \ schema, context) relation = network.Relation.create(schema, data) db.session.commit() url = url_for('.get', slug=network.slug, id=relation.id) return jsonify(relation, status=201, headers={'location': url})
def run(slug, name): """ Get a JSON representation of stored queries. """ # TODO: Use read-only DB connection network, query = _get_query(slug, name) try: limit = int(request.args.get('limit', 100)) offset = int(request.args.get('offset', 0)) rp = query.run(**dict(request.args.items())) except Exception as exc: return jsonify({'error': unicode(exc), 'query': query}, status=400) result = [] for i in count(): row = rp.fetchone() if row is None or i >= limit + offset: break if i < offset: continue row = dict(zip(rp.keys(), row)) result.append(row) data = {'results': result, 'count': rp.rowcount, 'query': query} request.cache_key['res'] = \ [sorted(r.values()) for r in data['results']] request.cache_key['count'] = data['count'] return jsonify(data)
def graph(slug, id): """ Get a JSON representation of the network. """ network, entity = _get_entity(slug, id) entity_types = request.args.getlist('entity_type') rel_types = request.args.getlist('relation_type') exports = set() graph = nx.DiGraph() def export(entity, depth): if entity.id in exports or \ (len(entity_types) and entity.type not in entity_types): return False entity.as_nx(graph) exports.add(entity.id) if depth > 0: for rel in entity.incoming: if len(rel_types) and not rel.type in rel_types: continue if rel.id not in exports and export(rel.source, depth - 1): rel.as_nx(graph) exports.add(rel.id) for rel in entity.outgoing: if len(rel_types) and not rel.type in rel_types: continue if rel.id not in exports and export(rel.target, depth - 1): rel.as_nx(graph) exports.add(rel.id) return True export(entity, 2) out = '' for line in nx.generate_gexf(graph): #print [line] out += line # JSONP for XML. Will now go vomit quietly somewhere. if request.args.get('wrap') == 'json': return jsonify({'xml': out}) return Response(out, status=200, content_type='text/xml')
def handle_validation_error(exc): if "json" == response_format(app, request): body = {"status": 400, "description": unicode(exc), "errors": exc.asdict()} return jsonify(body, status=400) return Response(repr(exc.asdict()), status=400, mimetype="text/plain")
def get(slug): """ Get a JSON representation of the network. """ network = _get_network(slug) return jsonify(network)
def deep(slug, id): """ Get a recursive JSON representation of the relation. """ network, relation = _get_relation(slug, id) return jsonify(relation.as_deep_dict())
def get(slug, name): """ Get a JSON representation of the query. """ network, query = _get_query(slug, name) return jsonify(query)
def index(slug): """ List all available queries. """ network = _get_network(slug) require.query.list(network) return jsonify({'results': Query.all(network)})
def history(slug, id): """ Get a JSON representation of the relation. """ network, relation = _get_relation(slug, id) return jsonify(relation.history)
def history(slug, id): """ Get a JSON representation of the entity's revision history. """ network, entity = _get_entity(slug, id) return jsonify(entity.history)
def index(): """ List all available networks. """ require.network.list() networks = Network.all() return jsonify(networks)
def deep(slug, id): """ Get a recursive JSON representation of the entity. """ network, entity = _get_entity(slug, id) return jsonify(entity.as_deep_dict())
def get(slug, id): """ Get a JSON representation of the entity. """ network, entity = _get_entity(slug, id) return jsonify(entity)
def get(slug, type, name): network = _get_network(slug) schema = _get_schema(network, type, name) return jsonify(schema)
def apiroot(): return jsonify({'api': 'ok', 'version': 1})