From 882d91a8284d3956e3218f891c403ad5c3ebe6cb Mon Sep 17 00:00:00 2001 From: paso-odoo Date: Thu, 27 Apr 2023 09:16:05 +0000 Subject: [PATCH] [FIX] loyalty: fix missing loyalty product update issue If applied, this commit will solve the issue of missing product issue while installing the following modules: loyalty_delivery, pos_loyalty, sale_loyalty, website_sale_loyalty Steps to produce: - Install loyalty module. - Go to 'Products' or 'Product Variants'. - Delete the product 'Gift Card'. - Now install the 'sale_loyalty' module. This commit will raise an userError while deleting the loyalty products. sentry - 4112536971 closes odoo/odoo#121484 X-original-commit: 3488d292b514bbf4190f19bd8271b3e68b3eb055 Signed-off-by: Parth Solanki (paso) --- addons/loyalty/i18n/loyalty.pot | 10 ++++++++++ addons/loyalty/models/__init__.py | 1 + addons/loyalty/models/product_product.py | 18 ++++++++++++++++-- addons/loyalty/models/product_template.py | 22 ++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 addons/loyalty/models/product_template.py diff --git a/addons/loyalty/i18n/loyalty.pot b/addons/loyalty/i18n/loyalty.pot index 2dca1d68196..ea28342fda0 100644 --- a/addons/loyalty/i18n/loyalty.pot +++ b/addons/loyalty/i18n/loyalty.pot @@ -1770,6 +1770,16 @@ msgstr "" msgid "You can not delete a program in an active state" msgstr "" +#. module: loyalty +#. odoo-python +#: code:addons/loyalty/models/product_product.py:0 +#: code:addons/loyalty/models/product_template.py:0 +#, python-format +msgid "" +"You cannot delete %(name)s as it is used in 'Coupons & Loyalty'. Please " +"archive it instead." +msgstr "" + #. module: loyalty #: model:mail.template,subject:loyalty.mail_template_gift_card msgid "Your Gift Card at {{ object.company_id.name }}" diff --git a/addons/loyalty/models/__init__.py b/addons/loyalty/models/__init__.py index daf8fafc9d6..25fd40743bb 100644 --- a/addons/loyalty/models/__init__.py +++ b/addons/loyalty/models/__init__.py @@ -7,4 +7,5 @@ from . import loyalty_reward from . import loyalty_rule from . import loyalty_program from . import product_product +from . import product_template from . import res_partner diff --git a/addons/loyalty/models/product_product.py b/addons/loyalty/models/product_product.py index 9193f258283..80fbc4ffe69 100644 --- a/addons/loyalty/models/product_product.py +++ b/addons/loyalty/models/product_product.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import _, models -from odoo.exceptions import ValidationError +from odoo import _, api, models +from odoo.exceptions import UserError, ValidationError + class ProductProduct(models.Model): _inherit = 'product.product' @@ -14,3 +15,16 @@ class ProductProduct(models.Model): if rewards: raise ValidationError(_("This product may not be archived. It is being used for an active promotion program.")) return super().write(vals) + + @api.ondelete(at_uninstall=False) + def _unlink_except_loyalty_products(self): + product_data = [ + self.env.ref('loyalty.gift_card_product_50', False), + self.env.ref('loyalty.ewallet_product_50', False), + ] + for product in self.filtered(lambda p: p in product_data): + raise UserError(_( + "You cannot delete %(name)s as it is used in 'Coupons & Loyalty'." + " Please archive it instead.", + name=product.name + )) diff --git a/addons/loyalty/models/product_template.py b/addons/loyalty/models/product_template.py new file mode 100644 index 00000000000..4d7ec6acdbc --- /dev/null +++ b/addons/loyalty/models/product_template.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import _, api, models +from odoo.exceptions import UserError + + +class ProductTemplate(models.Model): + _inherit = 'product.template' + + @api.ondelete(at_uninstall=False) + def _unlink_except_loyalty_products(self): + product_data = [ + self.env.ref('loyalty.gift_card_product_50', False), + self.env.ref('loyalty.ewallet_product_50', False), + ] + for product in self.filtered(lambda p: p.product_variant_id in product_data): + raise UserError(_( + "You cannot delete %(name)s as it is used in 'Coupons & Loyalty'." + " Please archive it instead.", + name=product.name + ))