From cee32aa16e405c617180b452967587539f6b0630 Mon Sep 17 00:00:00 2001 From: "Damhaut Florian (flda)" Date: Thu, 27 Apr 2023 13:56:48 +0000 Subject: [PATCH] [FIX] product: Improved error message for pav deletion What are the steps to reproduce your issue? - Create a product with more than one attribute. - Let say color White, black and purple - Create a 'draft' invoice for the purple product variant - Remove the 'purple' attribute value from the product - It will archive that variant (because the account.move linked to it) - Try to delete the attribute value from menu Sales > Cofinfiguration > Attribute What is the current behavior that you observe? - technical error message What would be your expected behavior in this case? - non-technical message for end-users Solution : - Change both message to tell user he cannot delete the value if the value has been referenced somewhere else. opw-2623583 missing forward-port of db1e52f0234f73e1bda22a411baba7b1f0994d5a closes odoo/odoo#120495 X-original-commit: 0c8c43d50a574d73d9d5ac925eb16faf730a76ba Signed-off-by: Morgane Demesmaeker --- addons/product/i18n/product.pot | 18 +++++++++++++----- .../product/models/product_attribute_value.py | 13 ++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/addons/product/i18n/product.pot b/addons/product/i18n/product.pot index c8c6136fcd8..6ca59acee1c 100644 --- a/addons/product/i18n/product.pot +++ b/addons/product/i18n/product.pot @@ -2815,17 +2815,18 @@ msgstr "" #: code:addons/product/models/product_attribute.py:0 #, python-format msgid "" -"You cannot delete the attribute %s because it is used on the following products:\n" -"%s" +"You cannot delete the attribute %(attribute)s because it is used on the following products:\n" +"%(products)s" msgstr "" #. module: product #. odoo-python -#: code:addons/product/models/product_attribute.py:0 +#: code:addons/product/models/product_attribute_value.py:0 #, python-format msgid "" -"You cannot delete the value %s because it is used on the following products:\n" -"%s" +"You cannot delete the value %(value)s because it is used on the following products:\n" +"%(products)s\n" +" If the value has been associated to a product in the past, you will not be able to delete it." msgstr "" #. module: product @@ -2847,6 +2848,13 @@ msgid "" "%s" msgstr "" +#. module: product +#. odoo-python +#: code:addons/product/models/product_attribute.py:0 +#, python-format +msgid "You cannot delete value %s because it was used in some products." +msgstr "" + #. module: product #. odoo-python #: code:addons/product/models/product_attribute.py:0 diff --git a/addons/product/models/product_attribute_value.py b/addons/product/models/product_attribute_value.py index e0b41f7ea97..b0a050a7ce9 100644 --- a/addons/product/models/product_attribute_value.py +++ b/addons/product/models/product_attribute_value.py @@ -93,10 +93,21 @@ class ProductAttributeValue(models.Model): for pav in self: if pav.is_used_on_products: raise UserError(_( - "You cannot delete the value %(value)s because it is used on the following products:\n%(products)s", + "You cannot delete the value %(value)s because it is used on the following " + "products:\n%(products)s\n If the value has been associated to a product in the" + " past, you will not be able to delete it.", value=pav.display_name, products=", ".join(pav.pav_attribute_line_ids.product_tmpl_id.mapped('display_name')), )) + linked_products = pav.env['product.template.attribute.value'].search( + [('product_attribute_value_id', '=', pav.id)] + ).with_context(active_test=False).ptav_product_variant_ids + unlinkable_products = linked_products._filter_to_unlink() + if linked_products != unlinkable_products: + raise UserError(_( + "You cannot delete value %s because it was used in some products.", + pav.display_name + )) def _without_no_variant_attributes(self): return self.filtered(lambda pav: pav.attribute_id.create_variant != 'no_variant')