[MIG] base/workflow

This commit is contained in:
Raphael Collet
2016-05-11 11:40:25 +02:00
parent d465346f1e
commit 47c44578ba
+134 -134
View File
@@ -1,183 +1,183 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from openerp.exceptions import UserError
from openerp.osv import fields, osv
from openerp.tools.translate import _
import openerp.workflow
from odoo import api, fields, models, _
from odoo.exceptions import UserError
import odoo.workflow
class workflow(osv.osv):
class Workflow(models.Model):
_name = "workflow"
_table = "wkf"
_order = "name"
_columns = {
'name': fields.char('Name', required=True),
'osv': fields.char('Resource Object', required=True,select=True),
'on_create': fields.boolean('On Create', select=True),
'activities': fields.one2many('workflow.activity', 'wkf_id', 'Activities'),
}
_defaults = {
'on_create': lambda *a: True
}
def copy(self, cr, uid, id, values, context=None):
name = fields.Char('Name', required=True)
osv = fields.Char('Resource Object', required=True, index=True)
on_create = fields.Boolean('On Create', default=True, index=True)
activities = fields.One2many('workflow.activity', 'wkf_id', string='Activities')
@api.multi
def copy(self, values):
raise UserError(_("Duplicating workflows is not possible, please create a new workflow"))
def write(self, cr, user, ids, vals, context=None):
if not context:
context={}
openerp.workflow.clear_cache(cr, user)
return super(workflow, self).write(cr, user, ids, vals, context=context)
@api.multi
def write(self, vals):
odoo.workflow.clear_cache(self._cr, self._uid)
return super(Workflow, self).write(vals)
def get_active_workitems(self, cr, uid, res, res_id, context=None):
cr.execute('select * from wkf where osv=%s limit 1',(res,))
@api.model
def get_active_workitems(self, res_model, res_id):
cr = self._cr
cr.execute("SELECT * FROM wkf WHERE osv=%s LIMIT 1", (res_model,))
wkfinfo = cr.dictfetchone()
workitems = []
if wkfinfo:
cr.execute('SELECT id FROM wkf_instance \
WHERE res_id=%s AND wkf_id=%s \
ORDER BY state LIMIT 1',
(res_id, wkfinfo['id']))
query = """ SELECT id FROM wkf_instance
WHERE res_id=%s AND wkf_id=%s
ORDER BY state LIMIT 1 """
cr.execute(query, (res_id, wkfinfo['id']))
inst_id = cr.fetchone()
cr.execute('select act_id,count(*) from wkf_workitem where inst_id=%s group by act_id', (inst_id,))
query = """ SELECT act_id, COUNT(*) FROM wkf_workitem
WHERE inst_id=%s GROUP BY act_id """
cr.execute(query, (inst_id,))
workitems = dict(cr.fetchall())
return {'wkf': wkfinfo, 'workitems': workitems}
return {'wkf': wkfinfo, 'workitems': workitems}
def create(self, cr, user, vals, context=None):
if not context:
context={}
openerp.workflow.clear_cache(cr, user)
return super(workflow, self).create(cr, user, vals, context=context)
@api.model
def create(self, vals):
odoo.workflow.clear_cache(self._cr, self._uid)
return super(Workflow, self).create(vals)
workflow()
class wkf_activity(osv.osv):
class WorkflowActivity(models.Model):
_name = "workflow.activity"
_table = "wkf_activity"
_order = "name"
_columns = {
'name': fields.char('Name', required=True),
'wkf_id': fields.many2one('workflow', 'Workflow', required=True, select=True, ondelete='cascade'),
'split_mode': fields.selection([('XOR', 'Xor'), ('OR','Or'), ('AND','And')], 'Split Mode', size=3, required=True),
'join_mode': fields.selection([('XOR', 'Xor'), ('AND', 'And')], 'Join Mode', size=3, required=True),
'kind': fields.selection([('dummy', 'Dummy'), ('function', 'Function'), ('subflow', 'Subflow'), ('stopall', 'Stop All')], 'Kind', required=True),
'action': fields.text('Python Action'),
'action_id': fields.many2one('ir.actions.server', 'Server Action', ondelete='set null'),
'flow_start': fields.boolean('Flow Start'),
'flow_stop': fields.boolean('Flow Stop'),
'subflow_id': fields.many2one('workflow', 'Subflow'),
'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'),
}
_defaults = {
'kind': lambda *a: 'dummy',
'join_mode': lambda *a: 'XOR',
'split_mode': lambda *a: 'XOR',
}
def unlink(self, cr, uid, ids, context=None):
if context is None: context = {}
if not context.get('_force_unlink') and self.pool.get('workflow.workitem').search(cr, uid, [('act_id', 'in', ids)]):
name = fields.Char('Name', required=True)
wkf_id = fields.Many2one('workflow', string='Workflow', ondelete='cascade',
required=True, index=True)
split_mode = fields.Selection([('XOR', 'Xor'), ('OR','Or'), ('AND','And')],
string='Split Mode', size=3, required=True, default='XOR')
join_mode = fields.Selection([('XOR', 'Xor'), ('AND', 'And')],
string='Join Mode', size=3, required=True, default='XOR')
kind = fields.Selection([('dummy', 'Dummy'), ('function', 'Function'),
('subflow', 'Subflow'), ('stopall', 'Stop All')],
string='Kind', required=True, default='dummy')
action = fields.Text('Python Action')
action_id = fields.Many2one('ir.actions.server', string='Server Action', ondelete='set null')
flow_start = fields.Boolean('Flow Start')
flow_stop = fields.Boolean('Flow Stop')
subflow_id = fields.Many2one('workflow', string='Subflow')
signal_send = fields.Char('Signal (subflow.*)')
out_transitions = fields.One2many('workflow.transition', 'act_from', string='Outgoing Transitions')
in_transitions = fields.One2many('workflow.transition', 'act_to', string='Incoming Transitions')
@api.multi
def unlink(self):
if not self._context.get('_force_unlink') and \
self.env['workflow.workitem'].search([('act_id', 'in', self.ids)]):
raise UserError(_('Please make sure no workitems refer to an activity before deleting it!'))
super(wkf_activity, self).unlink(cr, uid, ids, context=context)
super(WorkflowActivity, self).unlink()
wkf_activity()
class wkf_transition(osv.osv):
_table = "wkf_transition"
class WorkflowTransition(models.Model):
_name = "workflow.transition"
_table = "wkf_transition"
_rec_name = 'signal'
_columns = {
'trigger_model': fields.char('Trigger Object'),
'trigger_expr_id': fields.char('Trigger Expression'),
'sequence': fields.integer('Sequence'),
'signal': fields.char('Signal (Button Name)',
help="When the operation of transition comes from a button pressed in the client form, "\
"signal tests the name of the pressed button. If signal is NULL, no button is necessary to validate this transition."),
'group_id': fields.many2one('res.groups', 'Group Required',
help="The group that a user must have to be authorized to validate this transition."),
'condition': fields.char('Condition', required=True,
help="Expression to be satisfied if we want the transition done."),
'act_from': fields.many2one('workflow.activity', 'Source Activity', required=True, select=True, ondelete='cascade',
help="Source activity. When this activity is over, the condition is tested to determine if we can start the ACT_TO activity."),
'act_to': fields.many2one('workflow.activity', 'Destination Activity', required=True, select=True, ondelete='cascade',
help="The destination activity."),
'wkf_id': fields.related('act_from','wkf_id', type='many2one', relation='workflow', string='Workflow', select=True),
}
_defaults = {
'condition': lambda *a: 'True',
'sequence': 10,
}
_order = 'sequence,id'
def name_get(self, cr, uid, ids, context=None):
return [(line.id, (line.act_from.name) + '+' + (line.act_to.name)) if line.signal == False else (line.id, line.signal) for line in self.browse(cr, uid, ids, context=context)]
trigger_model = fields.Char('Trigger Object')
trigger_expr_id = fields.Char('Trigger Expression')
sequence = fields.Integer('Sequence', default=10)
signal = fields.Char('Signal (Button Name)',
help="When the operation of transition comes from a button pressed in the client form, "
"signal tests the name of the pressed button. If signal is NULL, no button is necessary to validate this transition.")
group_id = fields.Many2one('res.groups', string='Group Required',
help="The group that a user must have to be authorized to validate this transition.")
condition = fields.Char('Condition', required=True, default='True',
help="Expression to be satisfied if we want the transition done.")
act_from = fields.Many2one('workflow.activity', string='Source Activity',
ondelete='cascade', required=True, index=True,
help="Source activity. When this activity is over, the condition is tested to determine if we can start the ACT_TO activity.")
act_to = fields.Many2one('workflow.activity', string='Destination Activity',
ondelete='cascade', required=True, index=True,
help="The destination activity.")
wkf_id = fields.Many2one('workflow', related='act_from.wkf_id', string='Workflow')
def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
if args is None:
args = []
@api.multi
def name_get(self):
return [
(line.id, line.signal or "%s+%s" % (line.act_from.name, line.act_to.name))
for line in self
]
@api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
if name:
ids = self.search(cr, user, ['|',('act_from', operator, name),('act_to', operator, name)] + args, limit=limit)
return self.name_get(cr, user, ids, context=context)
return super(wkf_transition, self).name_search(cr, user, name, args=args, operator=operator, context=context, limit=limit)
domain = ['|', ('act_from', operator, name), ('act_to', operator, name)] + (args or [])
return self.search(domain, limit=limit).name_get()
return super(WorkflowTransition, self).name_search(name, args, operator, limit=limit)
wkf_transition()
class wkf_instance(osv.osv):
_table = "wkf_instance"
class WorkflowInstance(models.Model):
_name = "workflow.instance"
_table = "wkf_instance"
_rec_name = 'res_type'
_log_access = False
_columns = {
'uid': fields.integer('User'), # FIXME no constraint??
'wkf_id': fields.many2one('workflow', 'Workflow', ondelete='cascade', select=True),
'res_id': fields.integer('Resource ID'),
'res_type': fields.char('Resource Object'),
'state': fields.char('Status'),
'transition_ids': fields.many2many('workflow.transition', 'wkf_witm_trans', 'inst_id', 'trans_id'),
}
def _auto_init(self, cr, context=None):
super(wkf_instance, self)._auto_init(cr, context)
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_type_res_id_state_index\'')
uid = fields.Integer('User') # FIXME no constraint??
wkf_id = fields.Many2one('workflow', string='Workflow', ondelete='cascade', index=True)
res_id = fields.Integer('Resource ID')
res_type = fields.Char('Resource Object')
state = fields.Char('Status')
transition_ids = fields.Many2many('workflow.transition', 'wkf_witm_trans', 'inst_id', 'trans_id')
@api.model_cr_context
def _auto_init(self):
super(WorkflowInstance, self)._auto_init()
cr = self._cr
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname=%s', ['wkf_instance_res_type_res_id_state_index'])
if not cr.fetchone():
cr.execute('CREATE INDEX wkf_instance_res_type_res_id_state_index ON wkf_instance (res_type, res_id, state)')
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_wkf_id_index\'')
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname=%s', ['wkf_instance_res_id_wkf_id_index'])
if not cr.fetchone():
cr.execute('CREATE INDEX wkf_instance_res_id_wkf_id_index ON wkf_instance (res_id, wkf_id)')
wkf_instance()
class wkf_workitem(osv.osv):
_table = "wkf_workitem"
class WorkflowWorkitem(models.Model):
_name = "workflow.workitem"
_log_access = False
_table = "wkf_workitem"
_rec_name = 'state'
_columns = {
'act_id': fields.many2one('workflow.activity', 'Activity', required=True, ondelete="cascade", select=True),
'wkf_id': fields.related('act_id','wkf_id', type='many2one', relation='workflow', string='Workflow'),
'subflow_id': fields.many2one('workflow.instance', 'Subflow', ondelete="set null", select=True),
'inst_id': fields.many2one('workflow.instance', 'Instance', required=True, ondelete="cascade", select=True),
'state': fields.char('Status', select=True),
}
wkf_workitem()
class wkf_triggers(osv.osv):
_table = "wkf_triggers"
_name = "workflow.triggers"
_log_access = False
_columns = {
'res_id': fields.integer('Resource ID', size=128),
'model': fields.char('Object'),
'instance_id': fields.many2one('workflow.instance', 'Destination Instance', ondelete="cascade"),
'workitem_id': fields.many2one('workflow.workitem', 'Workitem', required=True, ondelete="cascade"),
}
def _auto_init(self, cr, context=None):
super(wkf_triggers, self)._auto_init(cr, context)
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_triggers_res_id_model_index\'')
act_id = fields.Many2one('workflow.activity', string='Activity',
ondelete="cascade", required=True, index=True)
wkf_id = fields.Many2one('workflow', related='act_id.wkf_id', string='Workflow')
subflow_id = fields.Many2one('workflow.instance', string='Subflow',
ondelete="set null", index=True)
inst_id = fields.Many2one('workflow.instance', string='Instance',
ondelete="cascade", required=True, index=True)
state = fields.Char('Status', index=True)
class WorkflowTriggers(models.Model):
_name = "workflow.triggers"
_table = "wkf_triggers"
_log_access = False
res_id = fields.Integer('Resource ID', size=128)
model = fields.Char('Object')
instance_id = fields.Many2one('workflow.instance', string='Destination Instance',
ondelete="cascade")
workitem_id = fields.Many2one('workflow.workitem', string='Workitem',
ondelete="cascade", required=True)
@api.model_cr_context
def _auto_init(self):
super(WorkflowTriggers, self)._auto_init()
cr = self._cr
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname=%s', ['wkf_triggers_res_id_model_index'])
if not cr.fetchone():
cr.execute('CREATE INDEX wkf_triggers_res_id_model_index ON wkf_triggers (res_id, model)')
wkf_triggers()