# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import re
from markupsafe import Markup
from odoo import api, fields, models, _
from odoo.tools import is_html_empty
class ProposeChange(models.TransientModel):
_name = 'propose.change'
_description = 'Propose a change in the production'
IMG_REGEX = re.compile(r'()')
workorder_id = fields.Many2one(
'mrp.workorder', 'Workorder', required=True, ondelete='cascade')
title = fields.Char('Title')
step_id = fields.Many2one('quality.check', 'Step to change')
note = fields.Html('New Instruction')
comment = fields.Char('Comment')
picture = fields.Binary('Picture')
change_type = fields.Selection([
('update_step', 'Update Current Step'),
('remove_step', 'Remove Current Step'),
('set_picture', 'Set Picture')], 'Type of Change')
@api.model
def default_get(self, fields):
defaults = super().default_get(fields)
if 'step_id' in defaults:
step = self.env['quality.check'].browse(defaults.get('step_id'))
defaults['title'] = step.title
return defaults
def process(self):
for wizard in self:
if wizard.change_type == 'update_step':
wizard._do_update_step()
elif wizard.change_type == 'remove_step':
wizard._do_remove_step()
elif wizard.change_type == 'set_picture':
wizard._do_set_picture()
def _workorder_name(self):
if self.workorder_id.employee_id:
return self.workorder_id.employee_id.name
return self.env.user.name
def _get_update_step_note(self, original_title=False):
tl_text = _("New Instruction suggested by %(user_name)s", user_name=self._workorder_name())
body = Markup("%s") % tl_text
if self.note and not is_html_empty(self.note):
body += Markup("
%s%s") % (_("Instruction:"), self.note)
if self.comment:
body += Markup("
%s %s") % (_("Reason:"), self.comment)
if self.title and self.title != original_title:
body += Markup("
%s %s") % (_("New Title suggested:"), self.title)
return body
def _do_update_step(self, notify_bom=True):
self.ensure_one()
existing_imgs = []
is_blank = is_html_empty(self.note)
if not is_html_empty(self.step_id.note) and (is_blank or re.sub(self.IMG_REGEX, '', self.note) == self.note):
# blank suggestion or only text => keep existing images if they exist
existing_imgs = re.findall(self.IMG_REGEX, self.step_id.note)
if existing_imgs:
self.step_id.note = Markup('
%s
') % Markup('