changeset f8832d073851 in modules/product_kit:6.0
details: 
https://hg.tryton.org/modules/product_kit?cmd=changeset&node=f8832d073851
description:
        Prevent DomainValidationError when coping product with components

        issue10701
        review350521002
        (grafted from 88a24c1e5a5d4245572694b568ecdfd30693cbef)
diffstat:

 product.py |  57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diffs (74 lines):

diff -r 41cc422d5940 -r f8832d073851 product.py
--- a/product.py        Mon Sep 06 00:33:37 2021 +0200
+++ b/product.py        Mon Sep 06 09:59:13 2021 +0200
@@ -27,6 +27,32 @@
         if self.type == 'kit':
             self.cost_price_method = 'fixed'
 
+    @classmethod
+    def copy(cls, templates, default=None):
+        pool = Pool()
+        Component = pool.get('product.component')
+        if default is None:
+            default = {}
+        else:
+            default = default.copy()
+
+        copy_components = 'components' not in default
+        default.setdefault('components', None)
+        new_templates = super().copy(templates, default)
+        if copy_components:
+            old2new = {}
+            to_copy = []
+            for template, new_template in zip(templates, new_templates):
+                to_copy.extend(
+                    c for c in template.components if not c.product)
+                old2new[template.id] = new_template.id
+            if to_copy:
+                Component.copy(to_copy, {
+                        'parent_template': (lambda d:
+                            old2new[d['parent_template']]),
+                        })
+        return new_templates
+
 
 class Product(metaclass=PoolMeta):
     __name__ = "product.product"
@@ -72,6 +98,37 @@
             quantities[kit.id] = kit.default_uom.floor(min(qties, default=0))
         return quantities
 
+    @classmethod
+    def copy(cls, products, default=None):
+        pool = Pool()
+        Component = pool.get('product.component')
+        if default is None:
+            default = {}
+        else:
+            default = default.copy()
+
+        copy_components = 'components' not in default
+        if 'template' in default:
+            default.setdefault('components', None)
+        new_products = super().copy(products, default)
+        if 'template' in default and copy_components:
+            template2new = {}
+            product2new = {}
+            to_copy = []
+            for product, new_product in zip(products, new_products):
+                if product.components:
+                    to_copy.extend(product.components)
+                    template2new[product.template.id] = new_product.template.id
+                    product2new[product.id] = new_product.id
+            if to_copy:
+                Component.copy(to_copy, {
+                        'parent_product': (lambda d:
+                            product2new[d['parent_product']]),
+                        'parent_template': (lambda d:
+                            template2new[d['parent_template']]),
+                        })
+        return new_products
+
 
 class ComponentMixin(sequence_ordered(), ModelStorage):
 

Reply via email to