Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
Archivo: web.py Proyecto: mihi-tr/grano
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')
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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')
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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})
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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})
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
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)
Ejemplo n.º 17
0
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})
Ejemplo n.º 18
0
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})
Ejemplo n.º 19
0
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)
Ejemplo n.º 20
0
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})
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
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)
Ejemplo n.º 23
0
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})
Ejemplo n.º 24
0
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)
Ejemplo n.º 25
0
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)
Ejemplo n.º 26
0
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})
Ejemplo n.º 27
0
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
Ejemplo n.º 28
0
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})
Ejemplo n.º 29
0
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})
Ejemplo n.º 30
0
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})
Ejemplo n.º 31
0
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)
Ejemplo n.º 32
0
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')
Ejemplo n.º 33
0
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')
Ejemplo n.º 34
0
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")
Ejemplo n.º 35
0
def get(slug):
    """ Get a JSON representation of the network. """
    network = _get_network(slug)
    return jsonify(network)
Ejemplo n.º 36
0
def deep(slug, id):
    """ Get a recursive JSON representation of the relation. """
    network, relation = _get_relation(slug, id)
    return jsonify(relation.as_deep_dict())
Ejemplo n.º 37
0
def get(slug):
    """ Get a JSON representation of the network. """
    network = _get_network(slug)
    return jsonify(network)
Ejemplo n.º 38
0
def get(slug, name):
    """ Get a JSON representation of the query. """
    network, query = _get_query(slug, name)
    return jsonify(query)
Ejemplo n.º 39
0
def index(slug):
    """ List all available queries. """
    network = _get_network(slug)
    require.query.list(network)
    return jsonify({'results': Query.all(network)})
Ejemplo n.º 40
0
def history(slug, id):
    """ Get a JSON representation of the relation. """
    network, relation = _get_relation(slug, id)
    return jsonify(relation.history)
Ejemplo n.º 41
0
def history(slug, id):
    """ Get a JSON representation of the entity's revision history. """
    network, entity = _get_entity(slug, id)
    return jsonify(entity.history)
Ejemplo n.º 42
0
def get(slug, name):
    """ Get a JSON representation of the query. """
    network, query = _get_query(slug, name)
    return jsonify(query)
Ejemplo n.º 43
0
def index(slug):
    """ List all available queries. """
    network = _get_network(slug)
    require.query.list(network)
    return jsonify({'results': Query.all(network)})
Ejemplo n.º 44
0
def index():
    """ List all available networks. """
    require.network.list()
    networks = Network.all()
    return jsonify(networks)
Ejemplo n.º 45
0
def history(slug, id):
    """ Get a JSON representation of the relation. """
    network, relation = _get_relation(slug, id)
    return jsonify(relation.history)
Ejemplo n.º 46
0
def deep(slug, id):
    """ Get a recursive JSON representation of the relation. """
    network, relation = _get_relation(slug, id)
    return jsonify(relation.as_deep_dict())
Ejemplo n.º 47
0
def history(slug, id):
    """ Get a JSON representation of the entity's revision history. """
    network, entity = _get_entity(slug, id)
    return jsonify(entity.history)
Ejemplo n.º 48
0
def deep(slug, id):
    """ Get a recursive JSON representation of the entity. """
    network, entity = _get_entity(slug, id)
    return jsonify(entity.as_deep_dict())
Ejemplo n.º 49
0
def deep(slug, id):
    """ Get a recursive JSON representation of the entity. """
    network, entity = _get_entity(slug, id)
    return jsonify(entity.as_deep_dict())
Ejemplo n.º 50
0
def get(slug, id):
    """ Get a JSON representation of the entity. """
    network, entity = _get_entity(slug, id)
    return jsonify(entity)
Ejemplo n.º 51
0
def get(slug, id):
    """ Get a JSON representation of the entity. """
    network, entity = _get_entity(slug, id)
    return jsonify(entity)
Ejemplo n.º 52
0
def get(slug, type, name):
    network = _get_network(slug)
    schema = _get_schema(network, type, name)
    return jsonify(schema)
Ejemplo n.º 53
0
def get(slug, type, name):
    network = _get_network(slug)
    schema = _get_schema(network, type, name)
    return jsonify(schema)
Ejemplo n.º 54
0
def apiroot():
    return jsonify({'api': 'ok', 'version': 1})
Ejemplo n.º 55
0
def index():
    """ List all available networks. """
    require.network.list()
    networks = Network.all()
    return jsonify(networks)