Package: oarepo-references
OArepo referencesOArepo module for tracking and updating references in Invenio recordsInstallationTo use this module in your Invenio application, run the following in your virtual environment:pip install oarepo-referencesPrerequisitesoarepo-validateThis module expects acanonical_urlfield present on your Record model. This field
should contain a full canonical url reference to the Record instance, e.g.classRecord(FilesRecord):@propertydefcanonical_url(self):returnurl_for('invenio_records_rest.recid_item',pid_value=self['pid'],_external=True)Types of referenceThis module considers the following two types of reference that can occur
the referencing objects:Reference by linkReference to another object is represented in the referencing
object's metadata as acanonical_urlof the referenced object, e.g:{..."links":{"attachments":"https://example.org/objects/M249/attachments","self":"https://example.org/objects/M249","works":"https://example.org/objects/M249/works"}}Inlined referenceThe actual metadata content of the referenced object are inlined
into the referencing object's metadata, e.g:{..."stylePeriod":{"id":123,"level":1,"links":{…},"path":"/novovek-moderni-doba","slug":"novovek-moderni-doba","startYear":1789,"title":[…],"tooltip":""}}In the example above, the complete metadata of a certain Taxonomic record
are inlined into thestylePeriodfield of the referencing object.UsageTo enable reference tracking on your data model objects, you will need to
do the following:Tell Marshmallow, which fields of your marshmallow schema contain references
by link by inheritingReferenceByLinkFieldMixin:classURLReferenceField(ReferenceByLinkFieldMixin,URL):"""URL reference marshmallow field."""If your Marshmallow Scheme holdsinlinedreferences, you
will need to define a custom nested schema for inlined reference
contents, that implementsref_urlthat returns an URL to be used
as a reference to the object and inherits fromInlineReferenceMixin, like this:classInlinedReferenceSchema(InlineReferenceMixin,Schema):"""Inlined reference schema."""classMeta:unknown=INCLUDEdefref_url(self,data):returndata.get('links').get('self')defref_uuid(self,data):returndata.get('id',None)Use the reference-enabled field in your Marshmallow schema:classExampleReferencesSchema(Schema):"""Reference to other objects schema."""link=URLReferenceField()inlined=Nested(InlinedReferenceSchema)Inherit your Record model from theReferenceEnabledRecordMixinandMarshmallowValidatedRecordMixin.
Doing so, will add support for automatic Record updates whenever some reference contained in Record metadata
changes:classExampleRecord(MarshmallowValidatedRecordMixin,ReferenceEnabledRecordMixin,Record):"""References enabled example record class."""MARSHMALLOW_SCHEMA=ExampleReferencesSchemaVALIDATE_MARSHMALLOW=TrueVALIDATE_PATCH=True@propertydefcanonical_url(self):returnurl_for('invenio_records_rest.recid_item',pid_value=self['pid'],_external=True)SignalsThis module will register the following signal handlers on the Invenio Records signals that handle
managing of reference records whenever a Record changes:Invenio Records signalRegisteredsignal handlerDescriptionafter_record_insertcreate_references_recordFinds all references to other objects in a Record and creates RecordReference entries for each referenceafter_record_updateupdate_references_recordUpdates all RecordReferences that refer to the updated object and reindexes all referring Recordsafter_record_deletedelete_references_recordDeletes all RecordReferences referring to the deleted RecordModule APIYou can access all the API functions this module exposes through thecurrent_referencesproxy.For more info, seeapi.py.TasksAn asynchronous (Celery) tasks could be launched in a group on all objects referring to a certain Record like this:fromoarepo_references.utilsimportrun_task_on_referrersrun_task_on_referrers(referred,task.s(),success_task.s(),error_task.s())Further documentation is available onhttps://oarepo-references.readthedocs.io/..
Copyright (C) 2019 Miroslav Bauer, CESNET.oarepo-references is free software; you can redistribute it and/or
modify it under the terms of the MIT License; see LICENSE file for more
details.ChangesVersion 0.1.0 (released TBD)Initial public release.