Package: oarepo-fsm
OArepo FSM  library for record state transitions built on top of thehttps://pypi.org/project/sqlalchemy-fsm/library.QuickstartRun the following commands to bootstrap your environmentgit clone https://github.com/oarepo/oarepo-fsm
cd oarepo-fsm
pip install -e .[devel]ConfigurationCheck that correct record_class is being used on the RECORDS_REST_ENDPOINT’s item_routeitem_route='/records/<pid(recid,record_class="yourapp.models:RecordModelFSM"):pid_value>',To automatically add a link to the FSM endpoint to your record links, use the followinglinks_factory_impin
yourRECORDS_REST_ENDPOINTSconfiglinks_factory_imp='oarepo_fsm.links:record_fsm_links_factory',If you wish to activate FSM on a certain Record enpoints only, put in your configOAREPO_FSM_ENABLED_REST_ENDPOINTS = ['recid']Whererecidis the prefix key into yourRECORDS_REST_ENDPOINTSconfiguration.
This library activates FSM on all endpoints usingrecord_classinherited fromFSMMixinotherwise.UsageIn order to use this library, you need to define a Record
model in your app, that inherits from aFSMMixincolumnfrom invenio_records import Record
from oarepo_fsm.mixins import FSMMixin

class RecordModelFSM(FSMMixin, Record):
...To define FSM transitions on this class, create methods decorated with@transition(..)e.g.@transition(
    src=['open', 'archived'],
    dest='published',
    required=['id'],
    permissions=[editor_permission],
    commit_record=True)
def publish(self, **kwargs):
    print('record published')Where decorator parameters mean:src: record must be in one of the source states before transition could happendest: target state of the transitionrequired: a list of required**kwargsthat must be passed to the@transitiondecorated functionpermissions: currently logged user must have at least one of the permissions to execute the transitioncommit_record: should the changes made in a record be commited after the function returns?A transition-decorated function can optionally return a custom flask Response or a JSON-serializable
dict to be provided to user in a JSON response.“state” field nameTo change state field name, setclass MyRecord(FSMMixin, ...):
    STATE_FIELD = 'administrative_props.state'Note that the field name might be nested. If the container element does not exist,
it will be created on the first transition.Note: if you use your own state field, do not use marshamllow mixin, json schema
or included mappings. Write/use your own.REST API UsageTo get current record state and possible transitions (only transitions that you have permission to invoke will be returned)GET <record_rest_item_endpoint>
>>>
{
    metadata: {
        state: <current state of the record>
        ... other record metadata
    }
    links: {
        self: ...,
        "transitions": {
            <fsm_transition1_name>: <transition_url>,
            <fsm_transition2_name>: <transition_url>,
        },
        ...
    }
}To invoke a specific transition transition, doPOST <record_rest_endpoint>/<fsm_transition_name>Further documentation is available onhttps://oarepo-fsm.readthedocs.io/Permission factoriesSometimes access to records should be governed by the state of the record. For example,
if the record is instate=editing, any editor can make changes. If it isstate=approving,
only the curator can modify the record.On REST level, modification permissions are governed by permission factoriesfrom invenio_records_rest.utils import allow_all, deny_all
RECORDS_REST_ENDPOINTS = dict(
    recid=dict(
       create_permission_factory_imp=deny_all,
       delete_permission_factory_imp=deny_all,
       update_permission_factory_imp=deny_all,
       read_permission_factory_imp=allow_all,
   )
)This library provides the following factories and helpers:transition_required(*transitions)allows user if
he is entitled to perform any of the transitions (
method names) on the current recordstates_required(*states,state_field="state"allows
anyone if the record is in any of the states mentionedrequire_all(*perms_or_factories)allows user only if all
permissions allow. Use it with states_required as followsrequire_all(
    states_required('editing'),
    editing_user_permission_factory
)where editing_user_permission_factory is a permission factory allowing only
editing users.require_any(*perms_or_factories)allows user if any of
the permissions allow. Examplerequire_any(
    require_all(
        states_required('editing'),
        editing_user_permission_factory
    ),
    require_all(
        states_required('editing', 'approving),
        curator_user_permission_factory
    ),
)ChangesVersion 0.1.0 (released TBD)Initial public release.