senaite / senaite.core

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

Fix AttributeError for transitions created with core's workflow api #2613

Closed xispa closed 2 months ago

xispa commented 2 months ago

Description of the issue/feature this PR addresses

This Pull Request fixes an AttributeError that arises when triggering transitions that were created or updated by using senaite.core.api.workflow because the transition property script_after_name (Script (after)) was wrongly set to 'None':

Captura de 2024-08-28 17-30-13

The value of script_after_name for Transition instances is initialized as None by default, although the rest of attributes (except guard) are initialized with empty string '': https://github.com/zopefoundation/Products.DCWorkflow/blob/master/src/Products/DCWorkflow/Transitions.py#L45-L56

However, on setProperties, the system stringifies the values and therefore, the value of the attribute becomes 'None' instead:

>>> from senaite.core.api import workflow as wapi
>>> wf = wapi.get_workflow(SAMPLE_WORKFLOW)
>>> # Add a new 'dumb' transition using DCWorkflow
>>> wf.transitions.addTransition("dumb")
>>> dumb = wf.transitions.get("dumb")
>>> getattr(dumb, "after_script_name") is None
True
>>> dumb.setProperties("Dumb", "", after_script_name=None)
>>> getattr(dumb, "after_script_name") is None
False
>>> getattr(dumb, "after_script_name")
'None'

This Pull Request makes senaite.core.api.workflow to be aware of this inconsistent behavior when updating/creating transitions.

Current behavior before PR

The value for attribute after_script_name of transitions updated or created with core's workflow API have a 'None' value (as string)

Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 176, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 385, in publish_module
  Module ZPublisher.WSGIPublisher, line 288, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZPublisher.WSGIPublisher, line 63, in call_object
  Module bika.lims.browser.workflow, line 153, in __call__
  Module bika.lims.browser.workflow, line 164, in __call__
  Module bika.lims.browser.workflow, line 182, in do_action
  Module bika.lims.workflow, line 99, in doActionFor
  Module Products.CMFCore.WorkflowTool, line 252, in doActionFor
  Module Products.CMFCore.WorkflowTool, line 537, in _invokeWithNotification
AttributeError: 'exceptions.KeyError' object has no attribute 'with_traceback'

Desired behavior after PR is merged

The value for attribute after_script_name of transitions updated or created with core's workflow API have a None value

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