# -*- coding: utf-8 -*- from odoo import models, fields, api, _ from odoo.exceptions import ValidationError, UserError import logging _logger = logging.getLogger(__name__) CONDITION_CODE_TEMP = """# Available locals: # - time, date, datetime, timedelta: Python libraries. # - env: Odoo Environement. # - model: Model of the record on which the action is triggered. # - obj: Record on which the action is triggered if there is one, otherwise None. # - user, Current user object. # - workflow: Workflow engine. # - syslog : syslog(message), function to log debug information to Odoo logging file or console. # - warning: warning(message), Warning Exception to use with raise. result = True""" class SoongWorkflowLink(models.Model): _name = "soong.workflow.link" _description = "Soong Workflow Links" name = fields.Char( string="Name", help="Enter friendly link name that describe the process." ) workflow_id = fields.Many2one( comodel_name="soong.workflow", string="Workflow Ref.", ondelete="cascade", required=True, ) condition_code = fields.Text( string="Operation Code", default=CONDITION_CODE_TEMP, help="Enter condition to pass thru this link.", ) node_from = fields.Many2one( comodel_name="soong.workflow.node", string="Source Node", ondelete="cascade", required=True, ) node_to = fields.Many2one( comodel_name="soong.workflow.node", string="Destination Node", ondelete="cascade", required=True, ) @api.constrains("node_from", "node_to") def check_nodes(self): for rec in self: if rec.node_from == rec.node_to: raise ValidationError( _("Sorry, source & destination nodes can't be the same.") ) @api.onchange("node_from", "node_to") def onchange_nodes(self): for rec in self: if rec.node_from and rec.node_to: rec.name = "%s -> %s" % (rec.node_from.name, rec.node_to.name) def trigger_link(self): # Variables cx = self.env.context model_name = cx.get("active_model") rec_id = cx.get("active_id") model_obj = self.env[model_name].sudo() rec = model_obj.browse(rec_id) # Validation if rec.state == self.node_from.node_name: rec.state = self.node_to.node_name return True