diff --git a/CHANGES.rst b/CHANGES.rst index c055d88701..e9bde37a56 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/bika/lims/utils/__init__.py b/src/bika/lims/utils/__init__.py index 1045f5bc1d..d588952764 100644 --- a/src/bika/lims/utils/__init__.py +++ b/src/bika/lims/utils/__init__.py @@ -23,7 +23,6 @@ import re import tempfile from email import Encoders - from email.MIMEBase import MIMEBase from time import time @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/bika/lims/utils/analysisrequest.py b/src/bika/lims/utils/analysisrequest.py index 6cf11f235e..49cb4b6460 100644 --- a/src/bika/lims/utils/analysisrequest.py +++ b/src/bika/lims/utils/analysisrequest.py @@ -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