546 lines
22 KiB
Python
546 lines
22 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api, _
|
|
from odoo.tools.safe_eval import safe_eval
|
|
from odoo.exceptions import UserError
|
|
from datetime import datetime, date, time, timedelta
|
|
from lxml import etree
|
|
import logging
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SoongWorkflowMixin(models.AbstractModel):
|
|
_name = "soong.workflow.mixin"
|
|
_description = "Soong Workflow Mixin"
|
|
|
|
@api.model
|
|
def _compute_default_state(self):
|
|
# Objects
|
|
wkf_obj = self.env["soong.workflow"]
|
|
rec_model = self._name
|
|
# Get Flow Start
|
|
wkf_recs = wkf_obj.search([("model_id", "=", rec_model)])
|
|
for wkf_rec in wkf_recs:
|
|
for node in wkf_rec.node_ids:
|
|
if node.flow_start:
|
|
return node.node_name
|
|
|
|
@api.model
|
|
def _load_state(self):
|
|
# Objects
|
|
wkf_obj = self.env["soong.workflow"]
|
|
# Variables
|
|
rec_model = self._name
|
|
ret = []
|
|
# Get Status
|
|
wkf_recs = wkf_obj.search([("model_id", "=", rec_model)])
|
|
for rec in wkf_recs:
|
|
for node in rec.node_ids:
|
|
ret.append((node.node_name, node.name))
|
|
return ret
|
|
|
|
state = fields.Selection(
|
|
selection="_load_state",
|
|
string="Status",
|
|
default=_compute_default_state,
|
|
track_visibility="onchange",
|
|
)
|
|
workflow_id = fields.Many2one(
|
|
comodel_name="soong.workflow",
|
|
string="Workflow Ref.",
|
|
compute="_compute_workflow_id",
|
|
)
|
|
|
|
approval_users_ids = fields.One2many("soong.approval.users", "res_id", domain=lambda self: [('res_model', '=', self._name)], auto_join=True, string="审批明细")
|
|
|
|
@api.depends("state")
|
|
def _compute_workflow_id(self):
|
|
# Objects
|
|
wkf_obj = self.env["soong.workflow"]
|
|
for rec in self:
|
|
# Variables
|
|
rec_model = rec._name
|
|
# Get Workflow
|
|
rec.workflow_id = wkf_obj.search([("model_id", "=", rec_model)])
|
|
|
|
node_id = fields.Many2one(
|
|
comodel_name="soong.workflow.node",
|
|
string="Workflow Node Ref.",
|
|
compute="_compute_node_id",
|
|
)
|
|
|
|
@api.depends("state")
|
|
def _compute_node_id(self):
|
|
# Objects
|
|
wkf_obj = self.env["soong.workflow"]
|
|
for rec in self:
|
|
# Variables
|
|
rec_model = rec._name
|
|
# Get Workflow Node
|
|
rec.node_id = wkf_obj.search(
|
|
[("model_id", "=", rec_model)]
|
|
).node_ids.filtered(lambda n: n.node_name == rec.state)
|
|
|
|
def btn_exec_action(self):
|
|
"""
|
|
Got invoked when a workflow button is called.
|
|
:return: button action return value.
|
|
"""
|
|
# Variables
|
|
cx = self.env.context.copy() or {}
|
|
cx.update({"active_id": self.id, "active_ids": self.ids})
|
|
wkf_btn_obj = self.env["soong.workflow.node.button"]
|
|
btn_rec = wkf_btn_obj.search([("btn_key", "=", cx.get("btn_key", False))])
|
|
if btn_rec:
|
|
return btn_rec.with_context(cx).run()
|
|
|
|
@api.constrains("state")
|
|
def _update_wkf_records(self):
|
|
for rec in self:
|
|
record_obj = self.env["soong.workflow.record"].search(
|
|
[("res_model", "=", rec._name), ("res_id", "=", rec.id)]
|
|
)
|
|
if not record_obj:
|
|
record_obj = self.env["soong.workflow.record"].create(
|
|
{
|
|
"res_model": rec._name,
|
|
"res_id": rec.id,
|
|
"workflow_id": rec.workflow_id.id,
|
|
"node_id": rec.node_id.id,
|
|
}
|
|
)
|
|
record_obj.update(
|
|
{"workflow_id": rec.workflow_id.id, "node_id": rec.node_id.id}
|
|
)
|
|
|
|
def action_open_diagram(self):
|
|
self.ensure_one()
|
|
current_node_id = self.workflow_id.node_ids.filtered(
|
|
lambda n: n.node_name == self.state
|
|
)
|
|
highlight_color = self.workflow_id.current_stage_highlight_color
|
|
|
|
return {
|
|
"name": _("Workflow: %(name)s")
|
|
% {
|
|
"name": self.display_name,
|
|
},
|
|
"res_model": "soong.workflow",
|
|
"type": "ir.actions.act_window",
|
|
"res_id": self.workflow_id.id,
|
|
"context": {
|
|
"highlight_node_id": current_node_id.id,
|
|
"highlight_node_color": highlight_color,
|
|
"diagram_readonly": True,
|
|
},
|
|
"views": [(False, "diagram_plus")],
|
|
}
|
|
|
|
# @api.model
|
|
# def _get_view(self, view_id=None, view_type='form', **options):
|
|
# """ Set the correct label for `unit_amount`, depending on company UoM """
|
|
# arch, view = super()._get_view(view_id, view_type, **options)
|
|
# for node in arch.xpath("//header"):
|
|
# node.append(etree.XML("""<span>booooooo</span>"""))
|
|
# return arch, view
|
|
|
|
@api.model
|
|
def get_view(self, view_id=None, view_type="form", **options):
|
|
res = super().get_view(view_id, view_type, **options)
|
|
if 'create="false"' not in res['arch']: # 针对一个模型多个form临时做的区分
|
|
if view_type in ["tree", "form"]:
|
|
if "field" in res["arch"]:
|
|
# res = self._remove_default_header(view_type, res)
|
|
res = self._remove_default_modifiers(view_type, res)
|
|
res = self._load_buttons_view(view_type, res)
|
|
res = self._load_button_box_view(view_type, res)
|
|
res = self._load_state_view(view_type, res)
|
|
res = self._load_one2many_views(view_type, res)
|
|
res = self._update_fields_view(view_type, res)
|
|
# 列表视图增加状态字段
|
|
# res = self._add_state_tree_view(view_type, res)
|
|
return res
|
|
|
|
def _remove_default_header(self, view_type, res):
|
|
"""
|
|
Removes default header element from form view.
|
|
:param view_type: Type of view now rendering.
|
|
:param res: View resource data.
|
|
:return: Updated view resource.
|
|
"""
|
|
# Variables
|
|
arch = etree.fromstring(res["arch"])
|
|
# Find Default Header Element & Remove it
|
|
if view_type == "form":
|
|
header_el = arch.xpath("//form/header")
|
|
header_el = header_el[0] if header_el else False
|
|
if header_el is not False:
|
|
header_el.getparent().remove(header_el)
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def _remove_default_modifiers(self, view_type, res):
|
|
"""
|
|
Removed fields default modifiers & attributes in form view.
|
|
:param view_type: Type of view now rendering.
|
|
:param res: View resource data.
|
|
:return: Updated view resource.
|
|
"""
|
|
# Variables
|
|
model = self._name
|
|
wkf_obj = self.env["soong.workflow"]
|
|
wkf_rec = wkf_obj.search([("model_id", "=", model)])
|
|
arch = etree.fromstring(res["arch"])
|
|
|
|
# Validation
|
|
if not wkf_rec.remove_default_attrs_mod:
|
|
return res
|
|
fields_name = wkf_rec.node_ids.field_ids.name.mapped("name")
|
|
# Find Field & Remove Modifiers
|
|
if view_type == "form":
|
|
for field in arch.iter("field"):
|
|
field_name = field.attrib.get("name", False)
|
|
if not field_name or field_name not in fields_name:
|
|
continue
|
|
for attr in field.attrib.keys():
|
|
if attr != "name":
|
|
field.attrib.pop(attr)
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def _load_buttons_view(self, view_type, res):
|
|
"""
|
|
Adds buttons to state bar in form view.
|
|
:param view_type: Type of view now rendering.
|
|
:param res: View resource data.
|
|
:return: Updated view resource.
|
|
"""
|
|
# Variables
|
|
this = self
|
|
model = self._name
|
|
wkf_obj = self.env["soong.workflow"]
|
|
arch = etree.fromstring(res["arch"])
|
|
|
|
# Helper Functions
|
|
def _check_condition_code(btn):
|
|
locals_dict = {
|
|
"env": this.env,
|
|
"model": this.env[model],
|
|
"obj": None,
|
|
"user": this.env["res.users"].browse(this.env.uid),
|
|
"datetime": datetime,
|
|
"time": time,
|
|
"date": date,
|
|
"timedelta": timedelta,
|
|
"workflow": this.env["soong.workflow"],
|
|
"warning": this.warning,
|
|
"syslog": this.syslog,
|
|
}
|
|
try:
|
|
safe_eval(
|
|
btn.reviewer_expression,
|
|
locals_dict=locals_dict,
|
|
mode="exec",
|
|
nocopy=True,
|
|
)
|
|
user = "user" in locals_dict and locals_dict["user"] or False
|
|
if user and self.env.user in user:
|
|
return True
|
|
return False
|
|
except Warning as ex:
|
|
raise ex
|
|
except:
|
|
raise UserError(_("Wrong python condition defined."))
|
|
|
|
# Find header & ADD button
|
|
if view_type == "form":
|
|
# Get Some Elements
|
|
form_el = arch.xpath("//form")
|
|
form_el = form_el[0] if form_el else False
|
|
header_el = arch.xpath("//form/header")
|
|
header_el = header_el[0] if header_el else False
|
|
# Create Header Element If not Exists
|
|
if header_el is False:
|
|
header_el = etree.Element("header")
|
|
form_el.insert(0, header_el)
|
|
# Construct Buttons & Fields Tags
|
|
wkf_rec = wkf_obj.search([("model_id", "=", model)])
|
|
for node in wkf_rec.node_ids:
|
|
for button in node.button_ids:
|
|
# Check Condition Code
|
|
if button.btn_hide:
|
|
if button.review_type == "individual":
|
|
if self.env.user != button.reviewer_id:
|
|
continue
|
|
elif button.review_type == "group":
|
|
if self.env.user not in button.reviewer_group_id.users:
|
|
continue
|
|
elif button.review_type == 'user_role':
|
|
user_role_id = self.env['res.users'].search(
|
|
[('employee_id.user_role_id', '=', button.users_role_id.id)])
|
|
if self.env.user != user_role_id:
|
|
continue
|
|
else:
|
|
if not _check_condition_code(button):
|
|
continue
|
|
# Add Button to View
|
|
btn_exec_action_el = etree.SubElement(header_el, "button")
|
|
btn_exec_action_el.set("name", "btn_exec_action")
|
|
btn_exec_action_el.set("string", button.name)
|
|
btn_exec_action_el.set("type", "object")
|
|
if button.is_highlight:
|
|
btn_exec_action_el.set("class", "oe_highlight")
|
|
if button.has_icon:
|
|
btn_exec_action_el.set("icon", button.icon)
|
|
btn_exec_action_el.set(
|
|
"invisible", "state != '%s'" % node.node_name
|
|
)
|
|
btn_exec_action_el.set(
|
|
"context",
|
|
"{'btn_key':'%s','active_model':'%s'}"
|
|
% (button.btn_key, self._name),
|
|
)
|
|
# setup_modifiers(btn_exec_action_el)
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def _load_button_box_view(self, view_type, res):
|
|
"""
|
|
Adds button_box in form view.
|
|
:param view_type: Type of view now rendering.
|
|
:param res: View resource data.
|
|
:return: Updated view resource.
|
|
"""
|
|
arch = etree.fromstring(res["arch"])
|
|
|
|
# Find header & ADD button
|
|
if view_type == "form":
|
|
# Get Some Elements
|
|
form_sheet_el = arch.xpath("//form/sheet")
|
|
form_sheet_el = form_sheet_el[0] if form_sheet_el else False
|
|
button_box_el = arch.xpath("//form/sheet/div[hasclass('oe_button_box')]")
|
|
button_box_el = button_box_el[0] if button_box_el else False
|
|
# Create button_box Element If not Exists
|
|
if button_box_el is False:
|
|
button_box_el = etree.Element("div")
|
|
button_box_el.set("class", "oe_button_box")
|
|
button_box_el.set("name", "button_box")
|
|
form_sheet_el.insert(0, button_box_el)
|
|
# Add Button to View
|
|
btn_exec_action_el = etree.SubElement(button_box_el, "button")
|
|
btn_exec_action_el.set("name", "action_open_diagram")
|
|
btn_exec_action_el.set("type", "object")
|
|
btn_exec_action_el.set("string", _("View Flow"))
|
|
btn_exec_action_el.set("class", "oe_stat_button")
|
|
btn_exec_action_el.set("icon", "fa-code-fork")
|
|
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def _load_state_view(self, view_type, res):
|
|
"""
|
|
Adds state bar to form view.
|
|
:param view_type: Type of view now rendering.
|
|
:param res: View resource data.
|
|
:return: Updated view resource.
|
|
"""
|
|
# Variables
|
|
wkf_obj = self.env["soong.workflow"]
|
|
model = self._name
|
|
visible_seq = []
|
|
arch = etree.fromstring(res["arch"])
|
|
wkf_rec = wkf_obj.search([("model_id", "=", model)])
|
|
|
|
# Find header & ADD state
|
|
if view_type == "form":
|
|
# Get Header Element
|
|
header_el = arch.xpath("//form/header")
|
|
header_el = header_el[0] if header_el else False
|
|
# Create State Element If not Exists
|
|
if header_el is not False:
|
|
state_el = etree.Element("field")
|
|
state_el.set("name", "state")
|
|
state_el.set("widget", "statusbar")
|
|
# Loop all nodes
|
|
for node in wkf_rec.node_ids:
|
|
# Read Visible States
|
|
if node.is_visible:
|
|
visible_seq.append(node.node_name)
|
|
# Set Attributes & Setup Modifiers for State Field
|
|
if visible_seq:
|
|
state_el.set("statusbar_visible", ",".join(visible_seq))
|
|
header_el.append(state_el)
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def _load_one2many_views(self, view_type, res):
|
|
# Variables
|
|
# wkf_obj = self.env["soong.workflow"]
|
|
# model = self._name
|
|
arch = etree.fromstring(res["arch"])
|
|
# wkf_rec = wkf_obj.search([("model_id", "=", model)])
|
|
|
|
if view_type == "form":
|
|
# Locate the group element
|
|
group_el = arch.xpath("//sheet")
|
|
if group_el:
|
|
group_el = group_el[0]
|
|
else:
|
|
# If no group element exists, create one and add it to the arch
|
|
group_el = etree.Element("sheet")
|
|
# Insert the group element at the beginning or end of the arch
|
|
arch.insert(0, group_el)
|
|
|
|
# Locate or create the notebook element within the group
|
|
notebook_el = group_el.xpath(".//notebook")
|
|
if notebook_el:
|
|
notebook_el = notebook_el[0]
|
|
else:
|
|
# Create a notebook element if it doesn't exist
|
|
notebook_el = etree.Element("notebook")
|
|
group_el.append(notebook_el)
|
|
|
|
# Create a new page element
|
|
page_el = etree.Element("page")
|
|
page_el.set("string", "审批明细") # Set a label for the new page
|
|
|
|
# Create the field element for the one2many field
|
|
field_el = etree.Element("field")
|
|
field_el.set("name", "approval_users_ids")
|
|
field_el_context = {'tree_view_ref': 'soong_workflow.soong_approval_users_search', 'default_res_model_id': self.env['ir.model']._get(self._name).id}
|
|
field_el.set("context", str(field_el_context))
|
|
|
|
# Append the field element to the new page
|
|
page_el.append(field_el)
|
|
|
|
# Append the new page to the notebook
|
|
notebook_el.append(page_el)
|
|
|
|
# Convert the modified XML tree back to a string
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def create_field_element(self, field_name):
|
|
field_el = etree.Element("field")
|
|
field_el.set("name", field_name)
|
|
return field_el
|
|
|
|
def _update_fields_view(self, view_type, res):
|
|
"""
|
|
Updates fields attributes.
|
|
:param view_type: Type of view now rendering.
|
|
:param res: View resource data.
|
|
:return: Updated view resource.
|
|
"""
|
|
# Objects
|
|
wkf_obj = self.env["soong.workflow"]
|
|
user_obj = self.env["res.users"]
|
|
# Variables
|
|
model = self._name
|
|
uid = self._uid
|
|
wkf_rec = wkf_obj.search([("model_id", "=", model)])
|
|
arch = etree.fromstring(res["arch"])
|
|
|
|
# Helper Functions
|
|
def _get_external_id(group):
|
|
arr = []
|
|
ext_ids = group._get_external_ids()
|
|
for ext_id in ext_ids:
|
|
arr.append(ext_ids[ext_id][0])
|
|
return arr
|
|
|
|
if view_type == "form":
|
|
# Read fields of view
|
|
for field in arch.iter("field"):
|
|
field_name = field.attrib.get("name", False)
|
|
if not field_name:
|
|
continue
|
|
# Get Fields Instance
|
|
field_inst = arch.xpath("//field[@name='%s']" % str(field_name))
|
|
field_inst = field_inst[0] if field_inst else False
|
|
# Scope Variables
|
|
readonly_arr = []
|
|
required_arr = []
|
|
invisible_arr = []
|
|
if field_inst is not False:
|
|
# Loop all nodes
|
|
for node in wkf_rec.node_ids:
|
|
# Loop Other Nodes
|
|
for field_attrs in node.field_ids:
|
|
# Record all states for each attribute
|
|
if (
|
|
field_inst is not False
|
|
and field_attrs.name.name == field_inst.attrib["name"]
|
|
):
|
|
flag_show = True
|
|
if field_attrs.readonly:
|
|
readonly_arr.append(node.node_name)
|
|
if field_attrs.required:
|
|
required_arr.append(node.node_name)
|
|
if field_attrs.invisible:
|
|
invisible_arr.append(node.node_name)
|
|
# Check Users & Groups
|
|
if field_attrs.user_ids:
|
|
user_rec = user_obj.browse(uid)
|
|
if user_rec not in field_attrs.user_ids:
|
|
flag_show = False
|
|
if field_attrs.group_ids:
|
|
has_group = False
|
|
user_rec = user_obj.browse(uid)
|
|
ext_ids = _get_external_id(field_attrs.group_ids)
|
|
for ext_id in ext_ids:
|
|
has_group = user_rec.has_group(ext_id)
|
|
if not has_group:
|
|
flag_show = False
|
|
if not flag_show and (
|
|
field_attrs.group_ids or field_attrs.user_ids
|
|
):
|
|
invisible_arr.append(node.node_name)
|
|
# Set Attributes
|
|
if readonly_arr:
|
|
field_inst.set("readonly", "state in %s" % readonly_arr)
|
|
if required_arr:
|
|
field_inst.set("required", "state in %s" % required_arr)
|
|
if invisible_arr:
|
|
field_inst.set("invisible", "state in %s" % invisible_arr)
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def _add_state_tree_view(self, view_type, res):
|
|
"""
|
|
Adds state field to tree view.
|
|
:param view_type: Type of view now rendering.
|
|
:param res: View resource data.
|
|
:return: Updated view resource.
|
|
"""
|
|
# Variables
|
|
model = self._name
|
|
model_obj = self.env[model]
|
|
arch = etree.fromstring(res["arch"])
|
|
|
|
# Add State Field to Tree View if not Exists
|
|
if view_type == "tree":
|
|
# Get Header Element
|
|
tree_el = arch.xpath("//tree")
|
|
tree_el = tree_el[0] if tree_el else False
|
|
# Get State Element
|
|
state_el = arch.xpath("//field[@name='state']")
|
|
state_el = state_el[0] if state_el else False
|
|
if state_el is False:
|
|
state_el = etree.Element("field")
|
|
state_el.set("name", "state")
|
|
tree_el.append(state_el)
|
|
res["arch"] = etree.tostring(arch, encoding="unicode").replace("\t", "")
|
|
return res
|
|
|
|
def warning(self, msg):
|
|
if not isinstance(msg, str):
|
|
msg = str(msg)
|
|
raise UserError(msg)
|
|
|
|
def syslog(self, msg):
|
|
if not isinstance(msg, str):
|
|
msg = str(msg)
|
|
_logger.info(msg)
|