From fb1130166386b851fac7d672e2b0f2a7e81bd774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 5 Dec 2023 10:28:35 +0100 Subject: [PATCH] Strip generated code of product The template code or product suffix code allow to have trailing and leading spaces but not the code of the product. So we must compare the code with a stripped version of the generated code to avoid infinite loop. Closes #12801 (grafted from 79e4677f878eaedbaa882af75f450cfc9958624b) --HG-- branch : 7.0 --- modules/product/product.py | 7 ++++++ .../tests/scenario_product_variant.rst | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/modules/product/product.py b/modules/product/product.py index b952ae9662..d8ff8151d1 100644 --- a/modules/product/product.py +++ b/modules/product/product.py @@ -649,6 +649,13 @@ class Product( for product in products: code = ''.join(filter(None, [ product.prefix_code, product.suffix_code])) + if cls.code.strip: + if cls.code.strip == 'leading': + code = code.lstrip() + elif cls.code.strip == 'trailing': + code = code.rstrip() + else: + code = code.strip() if not code: code = None if code != product.code: diff --git a/modules/product/tests/scenario_product_variant.rst b/modules/product/tests/scenario_product_variant.rst index 91ea8cf72a..87df934299 100644 --- a/modules/product/tests/scenario_product_variant.rst +++ b/modules/product/tests/scenario_product_variant.rst @@ -60,3 +60,26 @@ Change template code:: >>> template.save() >>> sorted([p.code for p in template.products]) ['PRD001', 'PRD002'] + +Create template with trailing space in code:: + + >>> template = ProductTemplate() + >>> template.name = "Product" + >>> template.code = "TRAILING " + >>> template.default_uom = unit + >>> template.save() + >>> product, = template.products + >>> product.code + 'TRAILING' + +Create product with leading space in code:: + + >>> template = ProductTemplate() + >>> template.name = "Product" + >>> template.default_uom = unit + >>> product, = template.products + >>> product.suffix_code = " LEADING" + >>> template.save() + >>> product, = template.products + >>> product.code + 'LEADING'