Files
gzth/soong_workflow/models/soong_workflow.py
T
2024-08-26 09:41:39 +08:00

146 lines
5.8 KiB
Python

# -*- coding: utf-8 -*-
import logging
import datetime
from odoo import models, fields, api, _, modules
from odoo.exceptions import ValidationError, UserError
_logger = logging.getLogger(__name__)
MODEL_DOMAIN = """[
('is_workflow', '=', True),
('state', '=', 'base'),
('transient', '=', False),
'!',
'|',
'|',
'|',
'|',
'|',
'|',
'|',
('model', '=ilike', 'res.%'),
('model', '=ilike', 'ir.%'),
('model', '=ilike', 'soong.workflow%'),
('model', '=ilike', 'bus.%'),
('model', '=ilike', 'base.%'),
('model', '=ilike', 'base_%'),
('model', '=', 'base'),
('model', '=', '_unknown'),
]"""
class SoongWorkflow(models.Model):
_name = "soong.workflow"
_description = "Soong Workflow"
_sql_constraints = [
("uniq_name", "unique(name)", _("Workflow name must be unique.")),
# ("uniq_model", "unique(model_id)", _("Model must be unique.")),
]
name = fields.Char(string="Name", help="Give workflow a name.")
model_id = fields.Many2one("ir.model", string="Model", domain=MODEL_DOMAIN, help="Enter business model you would like to modify its workflow.")
node_ids = fields.One2many(comodel_name="soong.workflow.node", inverse_name="workflow_id", string="Nodes")
link_ids = fields.One2many(comodel_name="soong.workflow.link", inverse_name="workflow_id", string="Links")
remove_default_attrs_mod = fields.Boolean(string="Remove Default Attributes & Modifiers", default=True,
help="This option will remove default attributes set on fields & buttons of current model view in order to customized all attributes depending on your needs\nAttributes like: [required, readonly, invisible].")
current_stage_highlight_color = fields.Char(default="rgb(0, 255, 255)", help="The color to highlight the current stage on flow",)
start_time = fields.Datetime(string="Start Time", copy=False)
end_time = fields.Datetime(string="End Time", copy=False)
is_enable = fields.Boolean(string="Enable", default="False", compute="compute_is_enable", store=True)
def copy(self, default=None):
workflow_id = self.search([('model_id', '=', self.model_id.id)])
workflow_version = len(workflow_id) + 1
default = dict(default or {})
default.update({
'name': self.name + '-v' + str(workflow_version),
'model_id': False,
'remove_default_attrs_mod': self.remove_default_attrs_mod,
'current_stage_highlight_color': self.current_stage_highlight_color,
})
res = super(SoongWorkflow, self).copy(default)
node_dict = dict()
button_dict = dict()
# 复制节点
for node_id in self.node_ids:
new_noode_id = node_id.copy({
'workflow_id': res.id
})
node_dict[node_id.id] = new_noode_id.id
# 复制节点-按钮
for button_id in node_id.button_ids:
new_button_id = button_id.copy({
'node_id': new_noode_id.id,
'link_id': False
})
if button_id.link_id:
if button_id.link_id.id in button_dict:
button_dict[button_id.link_id.id] |= new_button_id
else:
button_dict[button_id.link_id.id] = new_button_id
# 复制节点-字段
for field_id in node_id.field_ids:
field_id.copy({
'node_id': new_noode_id.id,
})
# 复制链接
for link_id in self.link_ids:
new_link_id = link_id.copy({
'workflow_id': res.id,
'node_from': node_dict[link_id.node_from.id],
'node_to': node_dict[link_id.node_to.id]
})
if link_id.id in button_dict:
for new_button_id in button_dict[link_id.id]:
new_button_id.link_id = new_link_id.id
return res
@api.depends('start_time', 'end_time')
def compute_is_enable(self):
for rec in self:
if rec.start_time and rec.end_time:
if rec.start_time < datetime.datetime.now() and rec.end_time > datetime.datetime.now():
rec.is_enable = True
else:
rec.is_enable = False
else:
rec.is_enable = False
@api.constrains("node_ids")
def validate_nodes(self):
# Objects
wkf_node_obj = self.env["soong.workflow.node"]
for rec in self:
# Must have one flow start node
res = rec.node_ids.search_count(
[
("workflow_id", "=", rec.id),
("flow_start", "=", True),
]
)
if res > 1:
raise ValidationError(_("Workflow must have only one start node."))
# Nodes' sequence must be unique per workflow
for node in rec.node_ids:
res = wkf_node_obj.search_count(
[
("id", "!=", node.id),
("workflow_id", "=", rec.id),
("sequence", "=", node.sequence),
]
)
if res:
raise ValidationError(
_("Nodes' sequence must be unique per workflow.")
)
def action_workflow_diagram(self):
self.ensure_one()
action = self.env.ref("soong_workflow.action_soong_workflow").read()[0]
action["name"] = _("Workflow: %(type_name)s") % {"type_name": self.display_name}
action["context"] = {"default_workflow_id": self.id}
action["res_id"] = self.id
action["views"] = [(False, "diagram_plus"), (False, "form")]
return action