Skip to content

Commit

Permalink
Fix event subscribers are not triggered on analysis initialization (#…
Browse files Browse the repository at this point in the history
…2649)

* Fix event subscribers are not triggered on analysis initialization

* Changelog

* Trigger before/after transition events

* Added comment

* comments

* Trigger event with transition definition

---------

Co-authored-by: Ramon Bartl <[email protected]>
  • Loading branch information
xispa and ramonski authored Dec 4, 2024
1 parent dd5aab4 commit 7dece0a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2.6.0 (unreleased)
------------------

- #2649 Fix event subscribers are not triggered on analysis initialization
- #2650 Fix AttributeError on analysis update with interim values via jsonapi
- #2648 Increase the default width for field labels to min 150px
- #2647 Fix analysis instrument is not auto-assigned on change in worksheet
Expand Down
36 changes: 25 additions & 11 deletions src/bika/lims/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import re
import tempfile
from email import Encoders

from email.MIMEBase import MIMEBase
from time import time

Expand All @@ -48,6 +47,7 @@
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.CMFPlone.utils import safe_unicode
from Products.DCWorkflow.events import AfterTransitionEvent
from Products.DCWorkflow.events import BeforeTransitionEvent
from senaite.core.p3compat import cmp
from six.moves.urllib.request import urlopen
from weasyprint import CSS
Expand Down Expand Up @@ -275,11 +275,13 @@ def sortable_title(portal, title):
break
return sortabletitle


# TODO Remove this function
def logged_in_client(context, member=None):
return api.get_current_client()

def changeWorkflowState(content, wf_id, state_id, **kw):

def changeWorkflowState(content, wf_id, state_id, trigger_events=False, **kw):
"""Change the workflow state of an object
@param content: Content obj which state will be changed
@param state_id: name of the state to put on content
Expand All @@ -292,12 +294,17 @@ def changeWorkflowState(content, wf_id, state_id, **kw):
logger.error("%s: Cannot find workflow id %s" % (content, wf_id))
return False

action = kw.get("action")
tdef = workflow.transitions.get(action)
actor = kw.get("actor", api.user.get_user_id())
now = DateTime()

wf_state = {
'action': kw.get("action", None),
'actor': kw.get("actor", api.get_current_user().id),
'comments': "Setting state to %s" % state_id,
'review_state': state_id,
'time': DateTime()
"action": action,
"actor": actor,
"comments": "Setting state to %s" % state_id,
"review_state": state_id,
"time": now,
}

# Get old and new state info
Expand All @@ -307,17 +314,24 @@ def changeWorkflowState(content, wf_id, state_id, **kw):
raise WorkflowException("Destination state undefined: {}"
.format(state_id))

if trigger_events:
# Notify the *before* transition event
notify(BeforeTransitionEvent(
content, workflow, old_state, new_state, tdef, wf_state, None))

# Change status and update permissions
portal_workflow.setStatusOf(wf_id, content, wf_state)
workflow.updateRoleMappingsFor(content)

# Notify the object has been transitioned
notify(AfterTransitionEvent(content, workflow, old_state, new_state, None,
wf_state, None))

# Map changes to catalog
indexes = ["allowedRolesAndUsers", "review_state", "is_active"]
content.reindexObject(idxs=indexes)

if trigger_events:
# Notify the *after* transition event
notify(AfterTransitionEvent(
content, workflow, old_state, new_state, tdef, wf_state, None))

return True


Expand Down
5 changes: 4 additions & 1 deletion src/bika/lims/utils/analysisrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,11 @@ def receive_sample(sample, check_permission=False, date_received=None):
for obj in sample.objectValues():
if obj.portal_type != "Analysis":
continue
# https://github.com/senaite/senaite.core/pull/2650
# NOTE: We trigger event handlers for analyses to keep it consistent
# with the behavior when manually received.
changeWorkflowState(obj, ANALYSIS_WORKFLOW, "unassigned",
action="initialize")
action="initialize", trigger_events=True)

return True

Expand Down

0 comments on commit 7dece0a

Please sign in to comment.