API Docs

Record API

API for manipulating files associated to a record.

class invenio_records_files.api.FileObject(obj, data)[source]

Wrapper for files.

Bind to current bucket.

dumps()[source]

Create a dump of the metadata associated to the record.

get(key, default=None)[source]

Proxy to obj.

Parameters:key – Metadata key which holds the value.
Returns:Metadata value of the specified key or default.
get_version(version_id=None)[source]

Return specific version ObjectVersion instance or HEAD.

Parameters:version_id – Version ID of the object.
Returns:ObjectVersion instance or HEAD of the stored object.
class invenio_records_files.api.FilesIterator(record, bucket=None, file_cls=None)[source]

Iterator for files.

Initialize iterator.

dumps(bucket=None)[source]

Serialize files from a bucket.

Parameters:bucket – Instance of files invenio_files_rest.models.Bucket. (Default: self.bucket)
Returns:List of serialized files.
flush()[source]

Flush changes to record.

keys

Return file keys.

next()[source]

Python 2.7 compatibility.

rename(*args, **kwargs)[source]

Rename a file.

Parameters:
  • old_key – Old key that holds the object.
  • new_key – New key that will hold the object.
Returns:

The object that has been renamed.

sort_by(*ids)[source]

Update files order.

Parameters:ids – List of ids specifying the final status of the list.
class invenio_records_files.api.FilesMixin[source]

Implement files attribute for Record models.

Note

Implement _create_bucket() in subclass to allow files property to automatically create a bucket in case no bucket is present.

file_cls

File class used to generate the instance of files. Default to FileObject

alias of FileObject

files

Get files iterator.

Returns:Files iterator.
files_iter_cls

Files iterator class used to generate the files iterator. Default to FilesIterator

alias of FilesIterator

class invenio_records_files.api.Record(data, model=None)[source]

Define API for files manipulation using FilesMixin.

Initialize instance with dictionary data and SQLAlchemy model.

Parameters:
  • data – Dict with record metadata.
  • modelRecordMetadata instance.
delete(force=False)[source]

Delete a record and also remove the RecordsBuckets if necessary.

Parameters:force – True to remove also the RecordsBuckets object.
Returns:Deleted record.

Utilities

Implementention of various utility functions.

invenio_records_files.utils.file_download_ui(pid, record, _record_file_factory=None, **kwargs)[source]

File download view for a given record.

Plug this method into your RECORDS_UI_ENDPOINTS configuration:

RECORDS_UI_ENDPOINTS = dict(
    recid=dict(
        # ...
        route='/records/<pid_value/files/<filename>',
        view_imp='invenio_records_files.utils:file_download_ui',
        record_class='invenio_records_files.api:Record',
    )
)
Parameters:
invenio_records_files.utils.record_file_factory(pid, record, filename)[source]

Get file from a record.

Parameters:
  • pid – Not used. It keeps the function signature.
  • record – Record which contains the files.
  • filename – Name of the file to be returned.
Returns:

File object or None if not found.

invenio_records_files.utils.sorted_files_from_bucket(bucket, keys=None)[source]

Return files from bucket sorted by given keys.

Parameters:
  • bucketBucket containing the files.
  • keys – Keys order to be used.
Returns:

Sorted list of bucket items.

Signal handlers.

invenio_records_files.receivers.index_attachments(sender, json=None, record=None, index=None, doc_type=None)[source]

Load and index attached files for given record.

It iterates over _files field in record and checks if _attachment subfiled has been configured with following values:

  • True/False simply enables/disables automatic fulltext indexing for given file instance;
  • Alternativelly, one can provide a dict instance with all configuration options as defined in Elasticsearch guide on https://www.elastic.co/guide/en/elasticsearch/ search for mapper-attachment.

Note

Make sure that mapper-attachment plugin is installed and running in Elasticsearch when using this signal handler.

Models

Define relation between records and buckets.

class invenio_records_files.models.RecordsBuckets(**kwargs)[source]

Relationship between Records and Buckets.

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

bucket

Relationship to the bucket.

bucket_id

Bucket related with the record.

classmethod create(record, bucket)[source]

Create a new RecordsBuckets and adds it to the session.

Parameters:
  • record – Record used to relate with the Bucket.
  • bucket – Bucket used to relate with the Record.
Returns:

The RecordsBuckets object created.

record

It is used by SQLAlchemy for optimistic concurrency control.

record_id

Record related with the bucket.