senaite / senaite.core

Enterprise Open Source Laboratory System (LIMS)
https://senaite.com
GNU General Public License v2.0
254 stars 145 forks source link

Functions for workflow update #2514

Closed xispa closed 7 months ago

xispa commented 7 months ago

Description of the issue/feature this PR addresses

This Pull Request adds necessary functions to allow the update of workflows in an easy way, just by using dicts. For instance, one could add a new status and transition in the SAMPLE_WORKFLOW easily as follows:

settings = {
    "states": {
        "published": {
            # list preserves transitions, tuple replaces
            "transitions": ["store"],
        },
        "stored": {
            "title": "Stored",
            "description": "Sample is stored",
            "transitions": ("recover", "detach", "dispatch", ),
            # Copy permissions from sample_received first
            "permissions_copy_from": "sample_received",
            # Overwrite permissions
            "permissions": {
                # Note here we are passing tuples, so these permissions are
                # set with acquire=False
                permissions.AddAnalysis: (),
                permissions.AddAttachment: (),
                permissions.TransitionCancelAnalysisRequest: (),
                permissions.TransitionReinstateAnalysisRequest: (),
                permissions.EditFieldResults: (),
                permissions.EditResults: (),
                permissions.TransitionPreserveSample: (),
                permissions.TransitionPublishResults: (),
                permissions.TransitionScheduleSampling: (),
                ModifyPortalContent: (),
            }
        },
    },
    "transitions": {
        "store": {
            "title": "Store",
            "new_state": "stored",
            "action": "Store sample",
            "guard": {
                "guard_permissions": "",
                "guard_roles": "",
                "guard_expr": "python:here.guard_handler('store')",
            }
        },
    }

}

update_workflow(SAMPLE_WORKFLOW, **settings)

Current behavior before PR

Functions to update workflows easily are not present

Desired behavior after PR is merged

Functions to update workflows easily are present

-- I confirm I have tested this PR thoroughly and coded it according to PEP8 and Plone's Python styleguide standards.

xispa commented 7 months ago

Hi Jordi, many thanks for adding these helpers. There are only some minor things I would like to have to make it more understandable without looking into the structure of the whole workflow dict. I guess the changes would also allow us to add a better docstring, e.g.:

Appreciated. I agree with your suggestions, let me do these changes!