Skip to content

Commit

Permalink
fixed current_workflows.set_workflow to commit the changed parent record
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Jul 29, 2024
1 parent c32d395 commit 8a04778
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
21 changes: 16 additions & 5 deletions oarepo_workflows/ext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import cached_property

import importlib_metadata
from invenio_drafts_resources.services.records.uow import ParentRecordCommitOp

from oarepo_workflows.errors import InvalidWorkflowError
from oarepo_workflows.proxies import current_oarepo_workflows
Expand Down Expand Up @@ -49,17 +50,27 @@ def set_state(self, identity, record, value, *args, uow=None, **kwargs):
identity, record, previous_value, value, *args, uow=uow, **kwargs
)

def set_workflow(self, identity, record, value, *args, uow=None, **kwargs):
if value not in current_oarepo_workflows.record_workflows:
def set_workflow(
self, identity, record, new_workflow_id, *args, uow=None, commit=True, **kwargs
):
if new_workflow_id not in current_oarepo_workflows.record_workflows:
raise InvalidWorkflowError(
f"Workflow {value} does not exist in the configuration."
f"Workflow {new_workflow_id} does not exist in the configuration."
)
previous_value = record.parent.workflow
record.parent.workflow = value
record.parent.workflow = new_workflow_id
for workflow_changed_notifier in self.workflow_changed_notifiers:
workflow_changed_notifier(
identity, record, previous_value, value, *args, uow=uow, **kwargs
identity,
record,
previous_value,
new_workflow_id,
*args,
uow=uow,
**kwargs,
)
if commit:
uow.register(ParentRecordCommitOp(record.parent))

def get_workflow_from_record(self, record, **kwargs):
if hasattr(record, "parent"):
Expand Down
4 changes: 3 additions & 1 deletion oarepo_workflows/services/components/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ def create(self, identity, data=None, record=None, **kwargs):
workflow_id = data["parent"]["workflow_id"]
except KeyError:
raise MissingWorkflowError("Workflow not defined in input.")
current_oarepo_workflows.set_workflow(identity, record, workflow_id)
current_oarepo_workflows.set_workflow(
identity, record, workflow_id, uow=self.uow, **kwargs
)
16 changes: 12 additions & 4 deletions tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ def test_set_workflow(
):
record = record_service.create(users[0].identity, default_workflow_json)._record
with pytest.raises(InvalidWorkflowError):
workflow_change_function(users[0].identity, record, "egregore")
workflow_change_function(users[0].identity, record, "record_owners_can_read")
workflow_change_function(
users[0].identity, record, "invalid_workflow", commit=False
)
workflow_change_function(
users[0].identity, record, "record_owners_can_read", commit=False
)
assert record.parent.workflow == "record_owners_can_read"


Expand All @@ -153,6 +157,10 @@ def test_set_workflow_entrypoint_hookup(
):
record = record_service.create(users[0].identity, default_workflow_json)._record
with pytest.raises(InvalidWorkflowError):
workflow_change_function(users[0].identity, record, "egregore")
workflow_change_function(users[0].identity, record, "record_owners_can_read")
workflow_change_function(
users[0].identity, record, "invalid_workflow", commit=False
)
workflow_change_function(
users[0].identity, record, "record_owners_can_read", commit=False
)
assert record.parent["workflow-change-notifier-called"]

0 comments on commit 8a04778

Please sign in to comment.