[FIX][IMP] sale_quotation_builder: convert to computes

Part-of: odoo/odoo#81818
This commit is contained in:
Victor Feyens
2022-01-14 12:11:48 +00:00
parent 9624577a0f
commit e1e3c4e6e0
2 changed files with 47 additions and 42 deletions
@@ -1,24 +1,32 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api
from odoo import fields, models
from odoo.tools.translate import html_translate
class ProductTemplate(models.Model):
_inherit = "product.template"
quotation_only_description = fields.Html('Quotation Only Description', sanitize_attributes=False,
translate=html_translate, help="The quotation description (not used on eCommerce)")
quotation_only_description = fields.Html(
string="Quotation Only Description",
translate=html_translate,
sanitize_attributes=False,
help="The quotation description (not used on eCommerce)")
quotation_description = fields.Html('Quotation Description', compute='_compute_quotation_description',
sanitize_attributes=False, help="This field uses the Quotation Only Description if it is defined, otherwise it will try to read the eCommerce Description.")
quotation_description = fields.Html(
string="Quotation Description",
compute='_compute_quotation_description',
sanitize_attributes=False,
help="This field uses the Quotation Only Description if it is defined, "
"otherwise it will try to read the eCommerce Description.")
def _compute_quotation_description(self):
for record in self:
if record.quotation_only_description:
record.quotation_description = record.quotation_only_description
elif hasattr(record, 'website_description') and record.website_description:
record.quotation_description = record.website_description
for template in self:
if template.quotation_only_description:
template.quotation_description = template.quotation_only_description
elif hasattr(template, 'website_description') and template.website_description:
# Defined in website_sale
template.quotation_description = template.website_description
else:
record.quotation_description = ''
template.quotation_description = ''
@@ -8,7 +8,10 @@ from odoo.tools.translate import html_translate
class SaleOrderTemplate(models.Model):
_inherit = "sale.order.template"
website_description = fields.Html('Website Description', translate=html_translate, sanitize_attributes=False, sanitize_form=False)
website_description = fields.Html(
string="Website Description",
translate=html_translate,
sanitize_attributes=False, sanitize_form=False)
def open_template(self):
self.ensure_one()
@@ -22,40 +25,34 @@ class SaleOrderTemplate(models.Model):
class SaleOrderTemplateLine(models.Model):
_inherit = "sale.order.template.line"
website_description = fields.Html('Website Description', related='product_id.product_tmpl_id.quotation_only_description', translate=html_translate, readonly=False, sanitize_form=False)
# FIXME ANVFE why are the sanitize_* attributes different between this field
# and the one on option lines, doesn't make any sense ???
website_description = fields.Html(
string="Website Description",
compute='_compute_website_description',
store=True, readonly=False,
translate=html_translate,
sanitize_form=False)
@api.onchange('product_id')
def _onchange_product_id(self):
ret = super(SaleOrderTemplateLine, self)._onchange_product_id()
if self.product_id:
self.website_description = self.product_id.quotation_description
return ret
@api.model_create_multi
def create(self, vals_list):
vals_list = [self._inject_quotation_description(vals) for vals in vals_list]
return super().create(vals_list)
def write(self, values):
values = self._inject_quotation_description(values)
return super(SaleOrderTemplateLine, self).write(values)
def _inject_quotation_description(self, values):
values = dict(values or {})
if not values.get('website_description') and values.get('product_id'):
product = self.env['product.product'].browse(values['product_id'])
values['website_description'] = product.quotation_description
return values
def _compute_website_description(self):
for line in self:
if not line.product_id:
continue
line.website_description = line.product_id.quotation_description
class SaleOrderTemplateOption(models.Model):
_inherit = "sale.order.template.option"
website_description = fields.Html('Website Description', translate=html_translate, sanitize_attributes=False)
website_description = fields.Html(
string="Website Description",
compute="_compute_website_description",
store=True, readonly=False,
translate=html_translate,
sanitize_attributes=False)
@api.onchange('product_id')
def _onchange_product_id(self):
ret = super(SaleOrderTemplateOption, self)._onchange_product_id()
if self.product_id:
self.website_description = self.product_id.quotation_description
return ret
def _compute_website_description(self):
for option in self:
if not option.product_id:
continue
option.website_description = option.product_id.quotation_description