diff --git a/CHANGELOG b/CHANGELOG index 5cd36616fd..ce9aadf6b4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add identifier_get to product + Version 6.6.0 - 2022-10-31 -------------------------- * Bug fixes (see mercurial logs for details) diff --git a/product.py b/product.py index db0e183003..eb1956300f 100644 --- a/product.py +++ b/product.py @@ -603,6 +603,14 @@ class Product( def get_code_readonly(self, name): return self.default_code_readonly() + def identifier_get(self, types=None): + "Return the first identifier for the given types" + if isinstance(types, str) or types is None: + types = {types} + for identifier in self.identifiers: + if identifier.type in types: + return identifier + @classmethod def _new_suffix_code(cls): pool = Pool() diff --git a/tests/test_module.py b/tests/test_module.py index 8d2c796605..ca3e38b64f 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -498,5 +498,92 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase): with self.subTest(value=value): self.assertEqual(round_price(value), result) + @with_transaction() + def test_product_identifier_get_single_type(self): + "Test identifier get with a single type" + pool = Pool() + Identifier = pool.get('product.identifier') + Product = pool.get('product.product') + Template = pool.get('product.template') + Uom = pool.get('product.uom') + + uom, = Uom.search([], limit=1) + template = Template(name="Product", default_uom=uom) + template.save() + product = Product(template=template) + product.identifiers = [ + Identifier(code='FOO'), + Identifier(type='ean', code='978-0-471-11709-4'), + ] + product.save() + + self.assertEqual( + product.identifier_get('ean').code, + '978-0-471-11709-4') + + @with_transaction() + def test_product_identifier_get_many_types(self): + "Test identifier get with many types" + pool = Pool() + Identifier = pool.get('product.identifier') + Product = pool.get('product.product') + Template = pool.get('product.template') + Uom = pool.get('product.uom') + + uom, = Uom.search([], limit=1) + template = Template(name="Product", default_uom=uom) + template.save() + product = Product(template=template) + product.identifiers = [ + Identifier(code='FOO'), + Identifier(type='isbn', code='0-6332-4980-7'), + Identifier(type='ean', code='978-0-471-11709-4'), + ] + product.save() + + self.assertEqual( + product.identifier_get({'ean', 'isbn'}).code, + '0-6332-4980-7') + + @with_transaction() + def test_product_identifier_get_any(self): + "Test identifier get for any type" + pool = Pool() + Identifier = pool.get('product.identifier') + Product = pool.get('product.product') + Template = pool.get('product.template') + Uom = pool.get('product.uom') + + uom, = Uom.search([], limit=1) + template = Template(name="Product", default_uom=uom) + template.save() + product = Product(template=template) + product.identifiers = [ + Identifier(code='FOO'), + ] + product.save() + + self.assertEqual(product.identifier_get(None).code, 'FOO') + + @with_transaction() + def test_product_identifier_get_unknown_type(self): + "Test identifier get with a unknown type" + pool = Pool() + Identifier = pool.get('product.identifier') + Product = pool.get('product.product') + Template = pool.get('product.template') + Uom = pool.get('product.uom') + + uom, = Uom.search([], limit=1) + template = Template(name="Product", default_uom=uom) + template.save() + product = Product(template=template) + product.identifiers = [ + Identifier(code='FOO'), + ] + product.save() + + self.assertEqual(product.identifier_get('ean'), None) + del ModuleTestCase