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(*args, **kwargs)[source]

Record class with associated bucket.

The record class implements a one-to-one relationship between a bucket and a record. A bucket is automatically created and associated with the record when the record is created with Record.create() (unless with_bucket is set to False).

The bucket id is stored in the record metadata (by default in the _bucket key). You can implement your dump/load behavior for storing the bucket id in the record (or possibly somewhere else). You do this by creating a subclass of this class, and overriding the two classmethods Record.dump_bucket() and Record.load_bucket().

Initialize the record.

bucket

Get bucket instance.

bucket_id

Get bucket id from record metadata.

classmethod create(data, id_=None, with_bucket=True, **kwargs)[source]

Create a record and the associated bucket.

Parameters:with_bucket – Create a bucket automatically on record creation.
classmethod create_bucket(data)[source]

Create a bucket for this record.

Override this method to provide more advanced bucket creation capabilities. This method may return a new or existing bucket, or may return None, in case no bucket should be created.

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.
classmethod dump_bucket(data, bucket)[source]

Dump the bucket id into the record metadata.

Override this method to provide custom behavior for storing the bucket id in the record metadata. By default the bucket id is stored in the _bucket key. If you override this method, make sure you also override Record.load_bucket().

This method is called after the bucket is created, but before the record is created in the database.

Parameters:
  • data – A dictionary of the record metadata.
  • bucket – The created bucket for the record.
classmethod load_bucket(record)[source]

Load the bucket id from the record metadata.

Override this method to provide custom behavior for retrieving the bucket id from the record metadata. By default the bucket id is retrieved from the _bucket key. If you override this method, make sure you also override Record.dump_bucket().

Parameters:record – A record instance.

Utilities

Implementation 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',
    )
)

If download is passed as a querystring argument, the file is sent as an attachment.

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.

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.