Esempio n. 1
0
def list_db_published_records():
    """A generator for all the published records"""
    query = RecordMetadata.query.filter(RecordMetadata.json is not None)
    for obj in query.all():
        record = Record(obj.json, model=obj)
        if is_publication(record.model):
            yield record
def test_record_files_factory(app, db, location, record):
    """Test record file factory."""
    record.files['test.txt'] = BytesIO(b'Hello world!')

    # Get a valid file
    fileobj = record_file_factory(None, record, 'test.txt')
    assert fileobj.key == 'test.txt'

    # Get a invalid file
    assert record_file_factory(None, record, 'invalid') is None

    # Record has no files property
    assert record_file_factory(None, Record({}), 'invalid') is None
    assert record_file_factory(None, BaseRecord({}), 'invalid') is None
    baserecord = BaseRecord.create({})
    RecordsBuckets(bucket=Bucket.create(), record=baserecord)
    assert record_file_factory(None, baserecord, 'invalid') is None
def record_files_permission_factory(obj, action):
    """Files permission factory for any action.

    :param obj: An instance of `invenio_files_rest.models.Bucket
                <https://invenio-files-rest.readthedocs.io/en/latest/api.html
                #invenio_files_rest.models.Bucket>`_.
    :param action: The required action.
    :raises RuntimeError: If the object is unknown or no record.
    :returns: A
        :class:`invenio_records_permissions.policies.base.BasePermissionPolicy`
        instance.
    """
    if isinstance(obj, Bucket):
        # File creation
        bucket_id = str(obj.id)
    elif isinstance(obj, ObjectVersion):
        # File download
        bucket_id = str(obj.bucket_id)
    else:
        # TODO: Reassess if covering FileObject, MultipartObject
        #       makes sense via bucket_id = str(obj.bucket_id)
        raise RuntimeError('Unknown object')

    # Retrieve record
    # WARNING: invenio-records-files implies a one-to-one relationship
    #          between Record and Bucket, but does not enforce it
    #          "for better future" the invenio-records-files code says
    record_bucket = \
        RecordsBuckets.query.filter_by(bucket_id=bucket_id).one_or_none()
    if record_bucket:
        record_metadata = record_bucket.record
        record = Record(record_metadata.json, model=record_metadata)
    else:
        raise RuntimeError('No record')

    PermissionPolicy = get_record_permission_policy()

    return PermissionPolicy(action=action, record=record)
Esempio n. 4
0
def test_files_property(app, db, location, bucket):
    """Test record files property."""
    with pytest.raises(MissingModelError):
        Record({}).files

    record = Record.create({})
    RecordsBuckets.create(bucket=bucket, record=record.model)

    assert 0 == len(record.files)
    assert 'invalid' not in record.files
    # make sure that _files key is not added after accessing record.files
    assert '_files' not in record

    with pytest.raises(KeyError):
        record.files['invalid']

    bucket = record.files.bucket
    assert bucket

    # Create first file:
    record.files['hello.txt'] = BytesIO(b'Hello world!')

    file_0 = record.files['hello.txt']
    assert 'hello.txt' == file_0['key']
    assert 1 == len(record.files)
    assert 1 == len(record['_files'])

    # Update first file with new content:
    record.files['hello.txt'] = BytesIO(b'Hola mundo!')
    file_1 = record.files['hello.txt']
    assert 'hello.txt' == file_1['key']
    assert 1 == len(record.files)
    assert 1 == len(record['_files'])

    assert file_0['version_id'] != file_1['version_id']

    # Create second file and check number of items in files.
    record.files['second.txt'] = BytesIO(b'Second file.')
    record.files['second.txt']
    assert 2 == len(record.files)
    assert 'hello.txt' in record.files
    assert 'second.txt' in record.files

    # Check order of files.
    order_0 = [f['key'] for f in record.files]
    assert ['hello.txt', 'second.txt'] == order_0

    record.files.sort_by(*reversed(order_0))
    order_1 = [f['key'] for f in record.files]
    assert ['second.txt', 'hello.txt'] == order_1

    # Try to rename second file to 'hello.txt'.
    with pytest.raises(Exception):
        record.files.rename('second.txt', 'hello.txt')

    # Remove the 'hello.txt' file.
    del record.files['hello.txt']
    assert 'hello.txt' not in record.files
    # Make sure that 'second.txt' is still there.
    assert 'second.txt' in record.files

    with pytest.raises(KeyError):
        del record.files['hello.txt']

    # Now you can rename 'second.txt' to 'hello.txt'.
    record.files.rename('second.txt', 'hello.txt')
    assert 'second.txt' not in record.files
    assert 'hello.txt' in record.files