From 3806636d9be9912fd26fc501d6fed4bb9f4a3b27 Mon Sep 17 00:00:00 2001 From: "Simon Genin (ges)" Date: Thu, 25 Jun 2020 09:40:17 +0000 Subject: [PATCH] [FIX] sale_timesheet: prevent deleting service on timesheet This commit prevents a user from deleting or archiving the default "Service on Timesheet" product. Before this commit, the users were receiving a constraint violation error. closes odoo/odoo#53655 Taskid: 2221046 X-original-commit: 761d28bcca1e4f7789d45eb4baa22d08f31717a4 Signed-off-by: Yannick Tivisse (yti) --- addons/sale_timesheet/models/product.py | 35 ++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/addons/sale_timesheet/models/product.py b/addons/sale_timesheet/models/product.py index 3ae044c7c91..90a04b75494 100644 --- a/addons/sale_timesheet/models/product.py +++ b/addons/sale_timesheet/models/product.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models +import threading + +from odoo import api, fields, models, _ +from odoo.exceptions import ValidationError class ProductTemplate(models.Model): @@ -67,6 +70,21 @@ class ProductTemplate(models.Model): self.invoice_policy = 'order' return res + def unlink(self): + time_product = self.env.ref('sale_timesheet.time_product') + if time_product.product_tmpl_id in self: + raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name) + return super(ProductTemplate, self).unlink() + + def write(self, vals): + # timesheet product can't be archived + test_mode = getattr(threading.currentThread(), 'testing', False) or self.env.registry.in_test_mode() + if not test_mode and 'active' in vals and not vals['active']: + time_product = self.env.ref('sale_timesheet.time_product') + if time_product.product_tmpl_id in self: + raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name) + return super(ProductTemplate, self).write(vals) + class ProductProduct(models.Model): _inherit = 'product.product' @@ -75,3 +93,18 @@ class ProductProduct(models.Model): """ Check if the product is a delivered timesheet """ self.ensure_one() return self.type == 'service' and self.service_policy == 'delivered_timesheet' + + def unlink(self): + time_product = self.env.ref('sale_timesheet.time_product') + if time_product in self: + raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name) + return super(ProductProduct, self).unlink() + + def write(self, vals): + # timesheet product can't be archived + test_mode = getattr(threading.currentThread(), 'testing', False) or self.env.registry.in_test_mode() + if not test_mode and 'active' in vals and not vals['active']: + time_product = self.env.ref('sale_timesheet.time_product') + if time_product in self: + raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name) + return super(ProductProduct, self).write(vals)