product_detailed_type: improve compatibility with demo/test data

It's still not perfect yet, in particular when you install the "stock"
module and "product_detailed_type_stock" is not installed yet.
This commit is contained in:
Alexis de Lattre
2024-09-05 18:21:59 +02:00
parent c7c5d9172b
commit 0bbda6f265
3 changed files with 51 additions and 1 deletions

View File

@@ -13,7 +13,9 @@ class ProductTemplate(models.Model):
('consu', 'Consumable'),
('service', 'Service'),
], string='Product Type', default='consu', required=True, tracking=True)
type = fields.Selection(compute='_compute_type', store=True, string="Type")
type = fields.Selection(
compute='_compute_type', store=True, string="Type", # native string = "Product Type"
default=False, required=False)
def _detailed_type_mapping(self):
return {}
@@ -23,3 +25,16 @@ class ProductTemplate(models.Model):
type_mapping = self._detailed_type_mapping()
for record in self:
record.type = type_mapping.get(record.detailed_type, record.detailed_type)
# to ensure compat with test and demo data
# It's not perfect, we still have a problem when installing the "stock"
# module while product_detailed_type_stock is not installed yet: it creates
# products with type = 'product' and with the inherit below, it sets detailed_type = 'product'
# but this value is only possible once product_detailed_type_stock is installed. Odoo says:
# ValueError: Wrong value for product.template.detailed_type: 'product'
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get('type') and vals['type'] != vals.get('detailed_type'):
vals['detailed_type'] = vals['type']
return super().create(vals_list)

View File

@@ -0,0 +1 @@
from . import test_detailed_type

View File

@@ -0,0 +1,34 @@
# Copyright 2024 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import SavepointCase
class TestProductDetailedType(SavepointCase):
def test_product_detailed_type(self):
p1 = self.env['product.product'].create({
'name': 'Test 1',
'detailed_type': 'service',
})
self.assertEqual(p1.type, 'service')
p2 = self.env['product.product'].create({
'name': 'Test 2',
'detailed_type': 'consu',
})
self.assertEqual(p2.type, 'consu')
def test_product_type_compat(self):
p1 = self.env['product.product'].create({
'name': 'Test 1',
'type': 'service',
})
self.assertEqual(p1.type, 'service')
self.assertEqual(p1.detailed_type, 'service')
p2 = self.env['product.product'].create({
'name': 'Test 1',
'type': 'consu',
})
self.assertEqual(p2.type, 'consu')
self.assertEqual(p2.detailed_type, 'consu')