API Docs¶
Flask extension for Invenio-Records.
-
class
invenio_records.ext.
InvenioRecords
(app=None, **kwargs)[source]¶ Invenio-Records extension.
Extension initialization.
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.
- model –
RecordMetadata
instance.
-
commit
()[source]¶ Store changes on current instance in database.
Procedure followed:
- The signal
invenio_records.signals.before_record_insert
is called with the record as function parameter.
- The signal
The record data is validate.
The record is committed to the database.
- The signal
invenio_records.signals.after_record_insert
is called with the record as function parameter.
- The signal
Returns: The Record instance.
-
classmethod
create
(data, id_=None, **kwargs)[source]¶ Create a record instance and store it in database.
Procedure followed:
- The signal
invenio_records.signals.before_record_insert
is called with the data as function parameter.
- The signal
The record data is validate.
The record is added in the database.
- The signal
invenio_records.signals.after_record_insert
is called with the data as function parameter.
- The signal
Parameters: - data – Dict with record metadata.
- id – Force the UUID for the record.
- **kwargs – See below.
Returns: A new Record instance.
Keyword Arguments: - format_checker –
An instance of class
jsonschema.FormatChecker
, which contains validation rules for formats. Seevalidate()
for details. - validator –
A
jsonschema.IValidator
class that will be used to validate. Seevalidate()
for details.
-
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:
- The signal
invenio_records.signals.before_record_insert
is called with the record as function parameter.
- The signal
The record is deleted or soft-deleted.
- The signal
invenio_records.signals.after_record_insert
is called with the record as function parameter.
- The signal
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:
- The signal
invenio_records.signals.before_record_insert
is called with the record as function parameter.
- The signal
The record is reverted.
- The signal
invenio_records.signals.after_record_insert
is called with the reverted record as function parameter.
- The signal
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.
- model –
RecordMetadata
instance.
-
created
¶ Get creation timestamp.
-
id
¶ Get model identifier.
-
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 classjsonschema.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 callingjsonschema.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
.
Errors¶
Errors for 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
andupdated
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
thejson
field value should never beNULL
. 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.