Files
gzth/soong_workflow/models/soong_workflow.py
T
2024-07-25 21:05:30 +08:00

84 lines
3.3 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api, _, modules
from odoo.exceptions import ValidationError, UserError
import logging
_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",)
@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