3434from opengever .task import TASK_STATE_IN_PROGRESS
3535from opengever .task import TASK_STATE_OPEN
3636from opengever .task import TASK_STATE_PLANNED
37+ from opengever .task import TASK_STATE_REJECTED
38+ from opengever .task import TASK_STATE_RESOLVED
3739from opengever .task import TASK_STATE_SKIPPED
3840from opengever .task import util
3941from opengever .task .interfaces import ITaskSettings
4749from persistent .dict import PersistentDict
4850from persistent .list import PersistentList
4951from plone import api
52+ from plone .api .exc import InvalidParameterError
5053from plone .app .textfield import RichText
5154from plone .autoform import directives as form
5255from plone .dexterity .content import Container
@@ -907,14 +910,6 @@ def _set_review_state(self, review_state):
907910 self .sync ()
908911 self .reindexObject ()
909912
910- def get_available_transitions (self ):
911- wftool = api .portal .get_tool ("portal_workflow" )
912- actions = wftool .listActionInfos (object = self )
913- return [
914- action ['id' ] for action in actions
915- if action ['category' ] == 'workflow'
916- ]
917-
918913 def force_finish_task (self ):
919914 """This method is used to forcefully close a task, including all its subtasks,
920915 when a dossier is being closed and active tasks still exist.
@@ -924,32 +919,32 @@ def force_finish_task(self):
924919 Rather than preventing the dossier from closing, we force each task into a
925920 valid terminal state based on its current state.
926921
927- # This method is recursive, ensuring that all subtasks are also finished in the same way,
922+ This method is recursive, ensuring that all subtasks are also finished in the same way,
928923 """
929- state = api .content .get_state (self )
930- transitions = self .get_available_transitions ()
924+ if api .content .get_state (self ) == TASK_STATE_REJECTED :
925+ api .content .transition (obj = self , transition = 'task-transition-rejected-open' )
926+
927+ if api .content .get_state (self ) == TASK_STATE_OPEN :
928+ api .content .transition (obj = self , transition = 'task-transition-open-cancelled' )
929+ # Cancel a task will automatically cancel all subtasks.
930+ return
931931
932932 for subtask in self .objectValues ():
933933 if ITask .providedBy (subtask ):
934934 subtask .force_finish_task ()
935935
936- if state == TASK_STATE_OPEN :
937- api .content .transition (obj = self , transition = 'task-transition-open-cancelled' )
938-
939- elif state == TASK_STATE_IN_PROGRESS :
940- if 'task-transition-in-progress-resolved' in transitions :
941- api .content .transition (obj = self , transition = 'task-transition-in-progress-resolved' )
942- # refresh transitions after state change
943- transitions = self .get_available_transitions ()
944-
945- if 'task-transition-resolved-tested-and-closed' in transitions :
936+ if api .content .get_state (self ) == TASK_STATE_IN_PROGRESS :
937+ # Depending on the task type, some transitions are not possible. We
938+ # do not try to understand the businesslogic here and naivily do
939+ # the expected transitions.
940+ try :
941+ # We first try to directly close it.
946942 api .content .transition (obj = self , transition = 'task-transition-resolved-tested-and-closed' )
943+ except InvalidParameterError :
944+ # If it does not work, we try to resolve it first.
945+ api .content .transition (obj = self , transition = 'task-transition-in-progress-resolved' )
947946
948- if 'task-transition-in-progress-cancelled' in transitions :
949- api .content .transition (obj = self , transition = 'task-transition-in-progress-cancelled' )
950- transitions = self .get_available_transitions ()
951-
952- elif state == 'task-state-resolved' :
947+ if api .content .get_state (self ) == TASK_STATE_RESOLVED :
953948 api .content .transition (obj = self , transition = 'task-transition-resolved-tested-and-closed' )
954949
955950
0 commit comments