Files
tryton/trytond/workflow/workflow.py
T

365 lines
14 KiB
Python
Raw Normal View History

2008-06-23 20:18:54 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms.
2007-12-10 21:17:22 +01:00
"Workflow"
2008-02-07 01:03:05 +01:00
import os
2008-07-17 19:35:03 +02:00
from trytond.osv import fields, OSV
2007-12-10 21:17:22 +01:00
from trytond.netsvc import LocalService
2008-02-07 01:03:05 +01:00
from trytond.report import Report
from trytond.tools import exec_command_pipe
2008-04-08 13:41:27 +02:00
import base64
2007-12-10 21:17:22 +01:00
class Workflow(OSV):
"Workflow"
_name = "workflow"
_table = "wkf"
_description = __doc__
name = fields.Char('Name', required=True, translate=True)
osv = fields.Char('Resource Model', required=True, select=1)
on_create = fields.Boolean('On Create', select=2)
activities = fields.One2Many('workflow.activity', 'workflow',
'Activities')
def __init__(self):
super(Workflow, self).__init__()
self._error_messages.update({
'no_workflow_defined': 'No workflow defined!',
})
def default_on_create(self, cursor, user, context=None):
return 1
2007-12-10 21:17:22 +01:00
def write(self, cursor, user, ids, vals, context=None):
wf_service = LocalService("workflow")
2008-04-08 13:17:22 +02:00
wf_service.clear_cache(cursor)
2007-12-10 21:17:22 +01:00
return super(Workflow, self).write(cursor, user, ids, vals,
context=context)
def create(self, cursor, user, vals, context=None):
wf_service = LocalService("workflow")
2008-04-08 13:17:22 +02:00
wf_service.clear_cache(cursor)
2007-12-10 21:17:22 +01:00
return super(Workflow, self).create(cursor, user, vals,
context=context)
Workflow()
class WorkflowActivity(OSV):
"Workflow activity"
_name = "workflow.activity"
_table = "wkf_activity"
_description = __doc__
name = fields.Char('Name', required=True, translate=True)
workflow = fields.Many2One('workflow', 'Workflow', required=True,
select=1, ondelete='cascade')
split_mode = fields.Selection([
('XOR', 'Xor'),
('OR', 'Or'),
('AND', 'And'),
], 'Split Mode', required=True)
join_mode = fields.Selection([
('XOR', 'Xor'),
('AND', 'And'),
], 'Join Mode', required=True)
kind = fields.Selection([
('dummy', 'Dummy'),
('function', 'Function'),
('subflow', 'Subflow'),
('stopall', 'Stop All'),
], 'Kind', required=True)
2008-10-23 10:54:28 +02:00
action = fields.Text('Action', states={
'readonly': "kind == 'dummy'",
'required': "kind == 'function'",
})
flow_start = fields.Boolean('Flow Start')
flow_stop = fields.Boolean('Flow Stop')
2008-10-23 10:54:28 +02:00
subflow = fields.Many2One('workflow', 'Subflow', states={
'readonly': "kind != 'subflow'",
'required': "kind == 'subflow'",
})
2008-07-29 11:01:20 +02:00
signal_send = fields.Char('Signal (subflow.*)')
out_transitions = fields.One2Many('workflow.transition', 'act_from',
'Outgoing transitions')
in_transitions = fields.One2Many('workflow.transition', 'act_to',
'Incoming transitions')
def default_kind(self, cursor, user, context=None):
return 'dummy'
def default_join_mode(self, cursor, user, context=None):
return 'XOR'
def default_split_mode(self, cursor, user, context=None):
return 'XOR'
2007-12-10 21:17:22 +01:00
2008-07-30 12:26:32 +02:00
def default_flow_start(self, cursor, user, context=None):
return False
def default_flow_stop(self, cursor, user, context=None):
return False
2007-12-10 21:17:22 +01:00
WorkflowActivity()
class WorkflowTransition(OSV):
"Workflow transition"
_table = "wkf_transition"
_name = "workflow.transition"
_rec_name = 'signal'
_description = __doc__
2008-07-29 11:01:20 +02:00
trigger_model = fields.Char('Trigger Type')
trigger_expr_id = fields.Char('Trigger Expr ID')
signal = fields.Char('Signal (button Name)')
group = fields.Many2One('res.group', 'Group Required')
2008-07-29 11:01:20 +02:00
condition = fields.Char('Condition', required=True)
act_from = fields.Many2One('workflow.activity', 'Source Activity',
required=True, select=1, ondelete='cascade')
act_to = fields.Many2One('workflow.activity', 'Destination Activity',
required=True, select=1, ondelete='cascade')
def default_condition(self, cursor, user, context=None):
return 'True'
2007-12-10 21:17:22 +01:00
WorkflowTransition()
class WorkflowInstance(OSV):
"Workflow instance"
_table = "wkf_instance"
_name = "workflow.instance"
_rec_name = 'res_type'
_description = __doc__
workflow = fields.Many2One('workflow', 'Workflow', ondelete="restrict",
select=1)
uid = fields.Integer('User ID')
res_id = fields.Integer('Resource ID', required=True, select=1)
res_type = fields.Char('Resource Model', required=True, select=1)
state = fields.Char('State', required=True, select=1)
2007-12-10 21:17:22 +01:00
def __init__(self):
super(WorkflowInstance, self).__init__()
self._error_messages.update({
'no_instance_defined': 'No workflow instance defined!',
})
2008-02-11 14:01:47 +01:00
def _auto_init(self, cursor, module_name):
super(WorkflowInstance, self)._auto_init(cursor, module_name)
2007-12-10 21:17:22 +01:00
cursor.execute('SELECT indexname FROM pg_indexes ' \
'WHERE indexname = ' \
'\'wkf_instance_res_id_res_type_state_index\'')
if not cursor.fetchone():
cursor.execute('CREATE INDEX ' \
'wkf_instance_res_id_res_type_state_index ' \
'ON wkf_instance (res_id, res_type, state)')
cursor.execute('SELECT indexname FROM pg_indexes ' \
'WHERE indexname = ' \
'\'wkf_instance_res_id_workflow_index\'')
if not cursor.fetchone():
cursor.execute('CREATE INDEX ' \
'wkf_instance_res_id_workflow_index ' \
'ON wkf_instance (res_id, workflow)')
2007-12-10 21:17:22 +01:00
WorkflowInstance()
class WorkflowWorkitem(OSV):
"Workflow workitem"
_table = "wkf_workitem"
_name = "workflow.workitem"
_rec_name = 'state'
_description = __doc__
activity = fields.Many2One('workflow.activity', 'Activity',
required=True, ondelete="cascade", select=1)
subflow = fields.Many2One('workflow.instance', 'Subflow',
ondelete="cascade", select=1)
instance = fields.Many2One('workflow.instance', 'Instance',
required=True, ondelete="cascade", select=1)
state = fields.Char('State', select=1)
2007-12-10 21:17:22 +01:00
WorkflowWorkitem()
class WorkflowTrigger(OSV):
"Workflow trigger"
2008-02-21 17:00:05 +01:00
_table = "wkf_trigger"
_name = "workflow.trigger"
2007-12-10 21:17:22 +01:00
_description = __doc__
res_id = fields.Integer('Resource ID')
model = fields.Char('Model')
instance = fields.Many2One('workflow.instance',
'Destination Instance', ondelete="cascade")
workitem = fields.Many2One('workflow.workitem', 'Workitem',
required=True, ondelete="cascade")
2007-12-10 21:17:22 +01:00
2008-02-11 14:01:47 +01:00
def _auto_init(self, cursor, module_name):
super(WorkflowTrigger, self)._auto_init(cursor, module_name)
2007-12-10 21:17:22 +01:00
cursor.execute('SELECT indexname FROM pg_indexes ' \
2008-02-21 17:00:05 +01:00
'WHERE indexname = \'wkf_trigger_res_id_model_index\'')
2007-12-10 21:17:22 +01:00
if not cursor.fetchone():
2008-02-21 17:00:05 +01:00
cursor.execute('CREATE INDEX wkf_trigger_res_id_model_index ' \
'ON wkf_trigger (res_id, model)')
2007-12-10 21:17:22 +01:00
cursor.commit()
WorkflowTrigger()
2008-02-07 01:03:05 +01:00
class InstanceGraph(Report):
_name = 'workflow.instance.graph'
def execute(self, cursor, user, ids, datas, context=None):
import pydot
lang_obj = self.pool.get('ir.lang')
2008-02-07 01:03:05 +01:00
workflow_obj = self.pool.get('workflow')
instance_obj = self.pool.get('workflow.instance')
if context is None:
context = {}
lang_id = lang_obj.search(cursor, user, [
('code', '=', context.get('language', 'en_US')),
], limit=1, context=context)[0]
lang = lang_obj.browse(cursor, user, lang_id, context=context)
2008-02-07 01:03:05 +01:00
workflow_id = workflow_obj.search(cursor, user, [
('osv', '=', datas['model']),
], limit=1, context=context)
if not workflow_id:
workflow_obj.raise_user_error(cursor, 'no_workflow_defined',
context=context)
2008-02-07 01:03:05 +01:00
workflow_id = workflow_id[0]
2008-04-08 13:41:27 +02:00
workflow = workflow_obj.browse(cursor, user, workflow_id,
2008-02-07 01:03:05 +01:00
context=context)
instance_id = instance_obj.search(cursor, user, [
('res_id', '=', datas['id']),
2008-02-17 16:57:34 +01:00
('workflow', '=', workflow.id),
2008-04-28 18:32:24 +02:00
], order=[('id', 'DESC')], limit=1, context=context)
2008-02-07 01:03:05 +01:00
if not instance_id:
instance_obj.raise_user_error(cursor, 'no_instance_defined',
context=context)
2008-02-07 01:03:05 +01:00
instance_id = instance_id[0]
2008-06-10 17:58:09 +02:00
title = "Workflow: %s OSV: %s" % (workflow.name, workflow.osv)
if pydot.__version__ == '1.0.2':
# version 1.0.2 doesn't quote correctly label on Dot object
title = '"' + title + '"'
graph = pydot.Dot(fontsize='16',
label=title)
2008-02-07 01:03:05 +01:00
graph.set('center', '1')
graph.set('ratio', 'auto')
if lang.direction == 'ltr':
2008-06-10 17:58:09 +02:00
if hasattr(graph, 'set_rankdir'):
graph.set_rankdir('LR')
else:
graph.set('rankdir', 'LR')
else:
2008-06-10 17:58:09 +02:00
if hasattr(graph, 'set_rankdir'):
graph.set_rankdir('RL')
else:
graph.set('rankdir', 'RL')
2008-04-08 13:41:27 +02:00
self.graph_instance_get(cursor, user, graph, instance_id,
2008-02-07 01:03:05 +01:00
datas.get('nested', False), context=context)
data = graph.create(prog='dot', format='png')
return ('png', base64.encodestring(data), False)
2008-02-07 01:03:05 +01:00
2008-04-08 13:41:27 +02:00
def graph_instance_get(self, cursor, user, graph, instance_id, nested=False,
2008-02-07 01:03:05 +01:00
context=None):
instance_obj = self.pool.get('workflow.instance')
instance = instance_obj.browse(cursor, user, instance_id,
context=context)
2008-02-17 16:57:34 +01:00
self.graph_get(cursor, user, graph, instance.workflow.id, nested,
2008-02-07 01:03:05 +01:00
self.workitem_get(cursor, user, instance.id, context=context),
context=context)
def workitem_get(self, cursor, user, instance_id, context=None):
res = {}
workitem_obj = self.pool.get('workflow.workitem')
workitem_ids = workitem_obj.search(cursor, user, [
2008-02-17 16:57:34 +01:00
('instance', '=', instance_id),
2008-02-07 01:03:05 +01:00
], context=context)
workitems = workitem_obj.browse(cursor, user, workitem_ids,
context=context)
for workitem in workitems:
2008-02-17 16:57:34 +01:00
res.setdefault(workitem.activity.id, 0)
res[workitem.activity.id] += 1
if workitem.subflow:
2008-02-07 01:03:05 +01:00
res.update(self.workitem_get(cursor, user,
2008-02-17 16:57:34 +01:00
workitem.subflow.id, context=context))
2008-02-07 01:03:05 +01:00
return res
def graph_get(self, cursor, user, graph, workflow_id, nested=False,
workitem=None, context=None):
import pydot
if workitem is None:
workitem = {}
2008-04-08 13:41:27 +02:00
activity_obj = self.pool.get('workflow.activity')
2008-02-07 01:03:05 +01:00
workflow_obj = self.pool.get('workflow')
transition_obj = self.pool.get('workflow.transition')
activity_ids = activity_obj.search(cursor, user, [
2008-02-17 16:57:34 +01:00
('workflow', '=', workflow_id),
2008-02-07 01:03:05 +01:00
], context=context)
id2activities = {}
actfrom = {}
actto = {}
activities = activity_obj.browse(cursor, user, activity_ids,
context=context)
start = 0
stop = {}
for activity in activities:
if activity.flow_start:
start = activity.id
if activity.flow_stop:
stop['subflow.' + activity.name] = activity.id
id2activities[activity.id] = activity
2008-02-17 16:57:34 +01:00
if activity.subflow and nested:
2008-02-07 01:03:05 +01:00
workflow = workflow_obj.browse(cursor, user,
2008-02-17 16:57:34 +01:00
activity.subflow.id, context=context)
2008-02-07 01:03:05 +01:00
subgraph = pydot.Cluster('subflow' + str(workflow.id),
2008-06-10 17:58:09 +02:00
fontsize='12', label="Subflow: " + activity.name + \
2008-02-07 01:03:05 +01:00
'\\nOSV: ' + workflow.osv)
(substart, substop) = self.graph_get(cursor, user,
subgraph, workflow.id, nested, workitem,
context=context)
graph.add_subgraph(subgraph)
actfrom[activity.id] = substart
actto[activity.id] = substop
else:
args = {}
if activity.flow_start or activity.flow_stop:
args['style'] = 'filled'
args['color'] = 'lightgrey'
args['label'] = activity.name
2008-02-17 16:57:34 +01:00
if activity.subflow:
2008-02-07 01:03:05 +01:00
args['shape'] = 'box'
if activity.id in workitem:
args['label'] += '\\nx ' + str(workitem[activity.id])
args['color'] = 'red'
graph.add_node(pydot.Node(activity.id, **args))
actfrom[activity.id] = (activity.id, {})
actto[activity.id] = (activity.id, {})
transition_ids = transition_obj.search(cursor, user, [
('act_from', 'in', [x.id for x in activities]),
], context=context)
transitions = transition_obj.browse(cursor, user, transition_ids,
context=context)
for transition in transitions:
args = {}
args['label'] = str(transition.condition).replace(' or ',
'\\nor ').replace(' and ', '\\nand ')
if transition.signal:
args['label'] += '\\n' + str(transition.signal)
args['style'] = 'bold'
if id2activities[transition.act_from.id].split_mode == 'AND':
args['arrowtail'] = 'box'
elif id2activities[transition.act_from.id].split_mode == 'OR':
args['arrowtail'] = 'inv'
if id2activities[transition.act_to.id].join_mode == 'AND':
args['arrowhead'] = 'crow'
activity_from = actfrom[transition.act_from.id][1].get(
transition.signal, actfrom[transition.act_from.id][0])
activity_to = actto[transition.act_to.id][1].get(
transition.signal, actto[transition.act_to.id][0])
2008-06-10 17:58:09 +02:00
graph.add_edge(pydot.Edge(str(activity_from), str(activity_to),
fontsize='10', **args))
2008-02-07 01:03:05 +01:00
return ((start, {}), (stop.values()[0], stop))
InstanceGraph()