API Docs

Flask extension for Invenio-Records.

class invenio_records.ext.InvenioRecords(app=None, **kwargs)[source]

Invenio-Records extension.

Extension initialization.

init_app(app, entry_point_group='invenio_records.jsonresolver', **kwargs)[source]

Flask application initialization.

init_config(app)[source]

Initialize configuration.

Record API

Record API.

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

Define API for metadata creation and manipulation.

Initialize instance with dictionary data and SQLAlchemy model.

Parameters:
  • data – Dict with record metadata.
  • modelRecordMetadata instance.
commit()[source]

Store changes on current instance in database.

Procedure followed:

  1. The signal invenio_records.signals.before_record_insert is

    called with the record as function parameter.

  2. The record data is validate.

  3. The record is committed to the database.

  4. The signal invenio_records.signals.after_record_insert is

    called with the record as function parameter.

Returns:The Record instance.
classmethod create(data, id_=None, **kwargs)[source]

Create a record instance and store it in database.

Procedure followed:

  1. The signal invenio_records.signals.before_record_insert is

    called with the data as function parameter.

  2. The record data is validate.

  3. The record is added in the database.

  4. The signal invenio_records.signals.after_record_insert is

    called with the data as function parameter.

Parameters:
  • data – Dict with record metadata.
  • id – Force the UUID for the record.
  • **kwargs – See below.
Returns:

A new Record instance.

Keyword Arguments:
 
delete(force=False)[source]

Delete a record.

If force is False, the record is soft-deleted, i.e. the record stays in the database. This ensures e.g. that the same record identifier cannot be used twice, and that you can still retrieve the history of an object. If force is True, the record is completely removed from the database.

Procedure followed:

  1. The signal invenio_records.signals.before_record_insert is

    called with the record as function parameter.

  2. The record is deleted or soft-deleted.

  3. The signal invenio_records.signals.after_record_insert is

    called with the record as function parameter.

Parameters:force – Completely remove record from database.
Returns:The Record instance.
classmethod get_record(id_, with_deleted=False)[source]

Get record instance.

Raises database exception if record does not exists.

Parameters:
  • id – Record ID.
  • with_deleted – If True then it includes deleted records.
Returns:

The Record instance.

classmethod get_records(ids, with_deleted=False)[source]

Get multiple record instances.

Parameters:
  • ids – List of record ID.
  • with_deleted – If True then it includes deleted records.
Returns:

A list od Record instance.

patch(patch)[source]

Patch record metadata.

Params patch:Dictionary of record metadata.
Returns:A new Record instance.
revert(revision_id)[source]

Revert to a specific revision.

Procedure followed:

  1. The signal invenio_records.signals.before_record_insert is

    called with the record as function parameter.

  2. The record is reverted.

  3. The signal invenio_records.signals.after_record_insert is

    called with the reverted record as function parameter.

Parameters:revision_id – Specify with revision the record should be reverted.
Returns:The new Record instance.
revisions

Get revision iterator.

class invenio_records.api.RecordBase(data, model=None)[source]

Base class for Record and RecordBase.

Initialize instance with dictionary data and SQLAlchemy model.

Parameters:
  • data – Dict with record metadata.
  • modelRecordMetadata instance.
created

Get creation timestamp.

dumps(**kwargs)[source]

Return pure Python dictionary with record metadata.

id

Get model identifier.

replace_refs()[source]

Replace the $ref keys within the JSON.

revision_id

Get revision identifier.

updated

Get last updated timestamp.

validate(**kwargs)[source]

Validate record according to schema defined in $schema key.

Keyword Arguments:
 
  • format_checker – A format_checker is an instance of class jsonschema.FormatChecker containing business logic to validate arbitrary formats. For example:

    >>> from jsonschema import FormatChecker
    >>> from jsonschema.validators import validate
    >>> checker = FormatChecker()
    >>> checker.checks('foo')(lambda el: el.startswith('foo'))
    <function <lambda> at ...>
    >>> validate('foo', {'format': 'foo'}, format_checker=checker)
    

    returns None, which means that the validation was successful, while

    >>> validate('bar', {'format': 'foo'}, format_checker=checker)
    Traceback (most recent call last):
    ...
    ValidationError: 'bar' is not a 'foo'
    ...
    

    raises a jsonschema.exceptions.ValidationError.

  • validator – A jsonschema.IValidator class used for record validation. It will be used as cls argument when calling jsonschema.validate(). For example

    >>> from jsonschema.validators import extend, Draft4Validator
    >>> NoRequiredValidator = extend(
    ... Draft4Validator,
    ... validators={'required': lambda v, r, i, s: None})
    >>> schema = {
    ... 'type': 'object',
    ... 'properties': {
    ...     'name': { 'type': 'string' },
    ...     'email': { 'type': 'string' },
    ...     'address': {'type': 'string' },
    ...     'telephone': { 'type': 'string' }
    ... },
    ... 'required': ['name', 'email']
    ... }
    >>> from jsonschema.validators import validate
    >>> validate({}, schema, NoRequiredValidator)
    

    returns None, which means that the validation was successful, while

    >>> validate({}, schema)
    Traceback (most recent call last):
    ...
    ValidationError: 'name' is a required property
    ...
    

    raises a jsonschema.exceptions.ValidationError.

class invenio_records.api.RecordRevision(model)[source]

API for record revisions.

Initialize revision.

class invenio_records.api.RevisionsIterator(model)[source]

Iterator for record revisions.

Initialize iterator.

next()[source]

Python 2.7 compatibility.

Errors

Errors for records module.

exception invenio_records.errors.MissingModelError[source]

Error raised when record has no model.

exception invenio_records.errors.RecordsError[source]

Base class for errors in records module.

Models

Record models.

class invenio_records.models.RecordMetadata(**kwargs)[source]

Represent a record metadata inside the SQL database.

Additionally it contains two columns created and updated with automatically managed timestamps.

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.

id

Record identifier.

json

Store metadata in JSON format.

When you create new Record the json field value should never be NULL. Default value is an empty dict. NULL value means that the record metadata has been deleted.

version_id

It is used by SQLAlchemy for optimistic concurrency control.

Signals

Record module signals.

invenio_records.signals.after_record_delete = <blinker.base.NamedSignal object at 0x7ffc05516110; 'after-record-delete'>

Signal sent after a record is delete.

invenio_records.signals.after_record_insert = <blinker.base.NamedSignal object at 0x7ffc0556afd0; 'after-record-insert'>

Signal sent after a record is inserted.

Note

No modification are allowed on record object.

invenio_records.signals.after_record_revert = <blinker.base.NamedSignal object at 0x7ffc05516190; 'after-record-revert'>

Signal sent after a record is revert.

invenio_records.signals.after_record_update = <blinker.base.NamedSignal object at 0x7ffc05516090; 'after-record-update'>

Signal sent after a record is updated.

invenio_records.signals.before_record_delete = <blinker.base.NamedSignal object at 0x7ffc055160d0; 'before-record-delete'>

Signal is sent before a record is delete.

invenio_records.signals.before_record_insert = <blinker.base.NamedSignal object at 0x7ffc0556af90; 'before-record-insert'>

Signal is sent before a record is inserted.

Example subscriber

def listener(sender, *args, **kwargs):
    sender['key'] = sum(args)

from invenio_records.signals import before_record_insert
before_record_insert.connect(listener)
invenio_records.signals.before_record_revert = <blinker.base.NamedSignal object at 0x7ffc05516150; 'before-record-revert'>

Signal is sent before a record is revert.

invenio_records.signals.before_record_update = <blinker.base.NamedSignal object at 0x7ffc05516050; 'before-record-update'>

Signal is sent before a record is update.

Configuration

Package configuration.

invenio_records.config.RECORDS_VALIDATION_TYPES = {}

Pass additional types when validating record against schema.