Beispiel #1
0
def collection_entity_save(collection):
    collection = get_collection(collection, authz.WRITE)
    data = request_data()
    update_operation = 'id' in data

    entities = get_loom_config().entities
    schema = data.get('$schema')
    if update_operation and schema is None:
        schema = entities.get_schema(data['id'], right=authz.entity_right())

    if schema not in get_loom_config().schemas.values():
        raise BadRequest()

    # this will raise if it fails:
    validate(data, schema)
    subject = entities.save(schema, data, collection_id=collection.id,
                            author=request.auth_user,
                            right=authz.entity_right())
    collection_add_entity(collection, subject)
    get_loom_indexer().index_one(subject, schema=schema)
    entity = entities.get(subject, schema=schema, depth=2,
                          right=authz.entity_right())
    return jsonify({
        'status': 'ok',
        'data': entity
    }, status=200 if update_operation else 201)
Beispiel #2
0
def collection_index(collection):
    collection = get_collection(collection, authz.READ)
    results = collection_entities(collection, depth=get_depth(2),
                                  filter_schema=request.args.get('$schema'))
    return jsonify({
        'results': results
    })
Beispiel #3
0
def collection_entity_save(collection):
    collection = get_collection(collection, authz.WRITE)
    data = request_data()
    update_operation = 'id' in data

    entities = get_loom_config().entities
    schema = data.get('$schema')
    if update_operation and schema is None:
        schema = entities.get_schema(data['id'], right=authz.entity_right())

    if schema not in get_loom_config().schemas.values():
        raise BadRequest()

    # this will raise if it fails:
    validate(data, schema)
    subject = entities.save(schema,
                            data,
                            collection_id=collection.id,
                            author=request.auth_user,
                            right=authz.entity_right())
    collection_add_entity(collection, subject)
    get_loom_indexer().index_one(subject, schema=schema)
    entity = entities.get(subject,
                          schema=schema,
                          depth=2,
                          right=authz.entity_right())
    return jsonify({
        'status': 'ok',
        'data': entity
    },
                   status=200 if update_operation else 201)
Beispiel #4
0
def collection_graph(collection):
    collection = get_collection(collection, authz.READ)
    network = Network(get_loom_config().resolver)
    schema = request.args.get('$schema')
    for entity in collection_entities(collection, depth=get_depth(3),
                                      filter_schema=schema):
        network.add(entity)
    return jsonify(network)
Beispiel #5
0
def collection_entity_remove(collection):
    collection = get_collection(collection, authz.WRITE)
    subject = request.args.get('subject')
    if subject is None:
        raise BadRequest()
    collection_remove_entity(collection, subject)
    get_loom_indexer().index_one(subject)
    return jsonify({'status': 'ok'}, status=204)
Beispiel #6
0
def collection_graph(collection):
    collection = get_collection(collection, authz.READ)
    network = Network(get_loom_config().resolver)
    schema = request.args.get('$schema')
    for entity in collection_entities(collection,
                                      depth=get_depth(3),
                                      filter_schema=schema):
        network.add(entity)
    return jsonify(network)
Beispiel #7
0
def collection_csv(collection):
    collection = get_collection(collection, authz.READ)
    schema = request.args.get('$schema')
    if schema is None:
        raise BadRequest()
    results = collection_entities(collection, depth=get_depth(2),
                                  filter_schema=schema)
    visitor, table = entities_to_table(schema, results)
    basename = '%s - %s' % (collection.title, visitor.plural)
    return make_csv_response(table, basename)
Beispiel #8
0
def collection_entity_remove(collection):
    collection = get_collection(collection, authz.WRITE)
    subject = request.args.get('subject')
    if subject is None:
        raise BadRequest()
    collection_remove_entity(collection, subject)
    get_loom_indexer().index_one(subject)
    return jsonify({
        'status': 'ok'
    }, status=204)
Beispiel #9
0
def collection_csv(collection):
    collection = get_collection(collection, authz.READ)
    schema = request.args.get('$schema')
    if schema is None:
        raise BadRequest()
    results = collection_entities(collection,
                                  depth=get_depth(2),
                                  filter_schema=schema)
    visitor, table = entities_to_table(schema, results)
    basename = '%s - %s' % (collection.title, visitor.plural)
    return make_csv_response(table, basename)
Beispiel #10
0
def collection_xlsx(collection):
    collection = get_collection(collection, authz.READ)
    results = collection_entities(collection, depth=get_depth(2),
                                  filter_schema=request.args.get('$schema'))
    by_schema = OrderedDict()
    for result in results:
        by_schema.setdefault(result.get('$schema'), [])
        by_schema[result.get('$schema')].append(result)
    sheets = OrderedDict()
    for schema, schema_results in by_schema.items():
        schema_results = list(schema_results)
        visitor, table = entities_to_table(schema, schema_results)
        sheets[visitor.plural] = table

    if '$schema' in request.args and len(sheets):
        basename = '%s - %s' % (collection.title, sheets.keys()[0])
    else:
        basename = collection.title
    return make_xlsx_response(sheets, basename)
Beispiel #11
0
def collection_xlsx(collection):
    collection = get_collection(collection, authz.READ)
    results = collection_entities(collection,
                                  depth=get_depth(2),
                                  filter_schema=request.args.get('$schema'))
    by_schema = OrderedDict()
    for result in results:
        by_schema.setdefault(result.get('$schema'), [])
        by_schema[result.get('$schema')].append(result)
    sheets = OrderedDict()
    for schema, schema_results in by_schema.items():
        schema_results = list(schema_results)
        visitor, table = entities_to_table(schema, schema_results)
        sheets[visitor.plural] = table

    if '$schema' in request.args and len(sheets):
        basename = '%s - %s' % (collection.title, sheets.keys()[0])
    else:
        basename = collection.title
    return make_xlsx_response(sheets, basename)
Beispiel #12
0
def collection_index(collection):
    collection = get_collection(collection, authz.READ)
    results = collection_entities(collection,
                                  depth=get_depth(2),
                                  filter_schema=request.args.get('$schema'))
    return jsonify({'results': results})