changeset 036e51122f92 in modules/product:default
details: https://hg.tryton.org/modules/product?cmd=changeset&node=036e51122f92
description:
Add identifier_get to product
issue11811
review445321003
diffstat:
CHANGELOG | 2 +
product.py | 8 ++++
tests/test_module.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 97 insertions(+), 0 deletions(-)
diffs (123 lines):
diff -r a93c305f948c -r 036e51122f92 CHANGELOG
--- a/CHANGELOG Mon Oct 31 16:50:50 2022 +0100
+++ b/CHANGELOG Sat Nov 05 10:06:27 2022 +0100
@@ -1,3 +1,5 @@
+* Add identifier_get to product
+
Version 6.6.0 - 2022-10-31
--------------------------
* Bug fixes (see mercurial logs for details)
diff -r a93c305f948c -r 036e51122f92 product.py
--- a/product.py Mon Oct 31 16:50:50 2022 +0100
+++ b/product.py Sat Nov 05 10:06:27 2022 +0100
@@ -603,6 +603,14 @@
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 -r a93c305f948c -r 036e51122f92 tests/test_module.py
--- a/tests/test_module.py Mon Oct 31 16:50:50 2022 +0100
+++ b/tests/test_module.py Sat Nov 05 10:06:27 2022 +0100
@@ -498,5 +498,92 @@
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