Ejemplo n.º 1
0
def delete_collection(collection_id=None):
    # Deleting a collection affects many associated objects and requires
    # checks, so this is done manually and in detail here.
    q = db.session.query(Collection).filter(Collection.id == collection_id)
    collection = q.first()
    if collection is None:
        log.error("No collection with ID: %r", collection_id)

    log.info("Deleting collection [%r]: %r", collection.id, collection.label)
    deleted_at = datetime.utcnow()
    for entity in collection.entities:
        entity.collections = [c for c in entity.collections
                              if c.id != collection.id]
        db.session.add(entity)
        if not len(entity.collections):
            delete_entity(entity)
        else:
            update_entity(entity)

    for document in collection.documents:
        document.collections = [c for c in document.collections
                                if c.id != collection.id]
        if not len(document.collections):
            delete_document(document, deleted_at=deleted_at)
        else:
            if collection_id == document.source_collection_id:
                document.source_collection_id = None
            db.session.add(document)
            update_document(document)

    collection.delete(deleted_at=deleted_at)
    db.session.commit()
    index_delete(collection_id)
    with graph.transaction() as tx:
        graph.remove_collection(tx, collection_id)
Ejemplo n.º 2
0
def ingest_upload(collection_id):
    require(request.authz.can(collection_id, request.authz.WRITE))
    sync = get_flag('sync')
    meta, foreign_id = _load_metadata()
    parent_id = _load_parent(collection_id, meta)
    upload_dir = mkdtemp(prefix='aleph.upload.')
    try:
        path = None
        content_hash = None
        for storage in request.files.values():
            path = safe_filename(storage.filename, default='upload')
            path = os.path.join(upload_dir, path)
            storage.save(path)
            content_hash = checksum(path)
        document = Document.by_keys(collection_id=collection_id,
                                    parent_id=parent_id,
                                    foreign_id=foreign_id,
                                    content_hash=content_hash)
        document.update(meta)
        document.schema = Document.SCHEMA
        if content_hash is None:
            document.schema = Document.SCHEMA_FOLDER
        ingest_document(document,
                        path,
                        role_id=request.authz.id,
                        content_hash=content_hash)
    finally:
        shutil.rmtree(upload_dir)

    # Make sure collection counts are always accurate.
    update_document(document, shallow=True, sync=sync)
    return jsonify({'status': 'ok', 'id': stringify(document.id)}, status=201)
Ejemplo n.º 3
0
def update(document_id):
    document = get_document(document_id, request.authz.WRITE)
    data = parse_request(schema=DocumentSchema)
    document.update(data)
    db.session.commit()
    update_document(document)
    return view(document_id)
Ejemplo n.º 4
0
def update(document_id):
    document = get_db_document(document_id, request.authz.WRITE)
    data = parse_request(DocumentUpdateSchema)
    document.update(data)
    db.session.commit()
    update_document(document, shallow=True, sync=get_flag('sync', True))
    return view(document_id)
Ejemplo n.º 5
0
def ingest_upload(collection_id):
    require(request.authz.can(collection_id, request.authz.WRITE))
    sync = get_flag('sync')
    meta, foreign_id = _load_metadata()
    parent_id = _load_parent(collection_id, meta)
    upload_dir = mkdtemp(prefix='aleph.upload.')
    try:
        path = None
        content_hash = None
        for storage in request.files.values():
            path = safe_filename(storage.filename, default='upload')
            path = os.path.join(upload_dir, path)
            storage.save(path)
            content_hash = checksum(path)
        document = Document.by_keys(collection_id=collection_id,
                                    parent_id=parent_id,
                                    foreign_id=foreign_id,
                                    content_hash=content_hash)
        document.update(meta)
        document.schema = Document.SCHEMA
        if content_hash is None:
            document.schema = Document.SCHEMA_FOLDER
        ingest_document(document, path,
                        role_id=request.authz.id,
                        content_hash=content_hash)
    finally:
        shutil.rmtree(upload_dir)

    if document.collection.casefile:
        # Make sure collection counts are always accurate.
        update_document(document, sync=sync)
    return jsonify({
        'status': 'ok',
        'id': stringify(document.id)
    }, status=201)
Ejemplo n.º 6
0
def update(document_id):
    document = get_db_document(document_id, request.authz.WRITE)
    data = parse_request(DocumentUpdateSchema)
    document.update(data)
    db.session.commit()
    update_document(document)
    update_collection(document.collection)
    return view(document_id)
Ejemplo n.º 7
0
def ingest_upload(id):
    collection = get_db_collection(id, request.authz.WRITE)
    meta, foreign_id = _load_metadata(collection)
    parent_id = _load_parent(collection, meta)
    upload_dir = mkdtemp(prefix='aleph.upload.')
    try:
        documents = []
        for storage in request.files.values():
            path = safe_filename(storage.filename, default='upload')
            path = os.path.join(upload_dir, path)
            storage.save(path)
            content_hash = checksum(path)
            document = Document.by_keys(collection_id=collection.id,
                                        parent_id=parent_id,
                                        foreign_id=foreign_id,
                                        content_hash=content_hash)
            document.update(meta)
            document.schema = Document.SCHEMA
            ingest_document(document,
                            path,
                            role_id=request.authz.id,
                            content_hash=content_hash)
            documents.append(document)

        if not len(request.files):
            # If there is no files uploaded, try to create an empty
            # directory instead. Maybe this should be more explicit,
            # but it seemed like the most simple way of fitting it
            # into the API.
            document = Document.by_keys(collection_id=collection.id,
                                        parent_id=parent_id,
                                        foreign_id=foreign_id)
            document.update(meta)
            document.schema = Document.SCHEMA_FOLDER
            ingest_document(document, None, role_id=request.authz.id)
            documents.append(document)
    finally:
        shutil.rmtree(upload_dir)

    # Update child counts in index.
    if parent_id is not None:
        index_document_id.apply_async([parent_id], priority=1)

    # Make sure collection counts are always accurate.
    if get_flag('sync'):
        for document in documents:
            update_document(document, shallow=True, sync=True)

    return jsonify({
        'status':
        'ok',
        'documents': [CombinedSchema().dump(d).data for d in documents]
    })
Ejemplo n.º 8
0
def delete_collection(collection_id=None):
    # Deleting a collection affects many associated objects and requires
    # checks, so this is done manually and in detail here.
    q = db.session.query(Collection).filter(Collection.id == collection_id)
    collection = q.first()
    if collection is None:
        log.error("No collection with ID: %r", collection_id)

    log.info("Deleting collection [%r]: %r", collection.id, collection.label)
    deleted_at = datetime.utcnow()
    for entity in collection.entities:
        entity.collections = [
            c for c in entity.collections if c.id != collection.id
        ]
        db.session.add(entity)
        if not len(entity.collections):
            delete_entity(entity)
        else:
            update_entity(entity)

    for document in collection.documents:
        document.collections = [
            c for c in document.collections if c.id != collection.id
        ]
        if not len(document.collections):
            delete_document(document, deleted_at=deleted_at)
        else:
            if collection_id == document.source_collection_id:
                document.source_collection_id = None
            db.session.add(document)
            update_document(document)

    collection.delete(deleted_at=deleted_at)
    db.session.commit()
    index_delete(collection_id)
    with graph.transaction() as tx:
        graph.remove_collection(tx, collection_id)