From 53db71bc65dbb84d8562343fffe155237f7a1fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20D=2E=20D=C3=ADaz?= Date: Tue, 5 Apr 2022 19:24:19 +0200 Subject: [PATCH] [IMP] web_widget_one2many_product_picker: more detailed message error (cherry picked from commit 8c45c822971f091f9e9055de2c85cafb72489f9c) --- .../models/base.py | 102 +++++++----------- .../widgets/field_one2many_product_picker.js | 4 - 2 files changed, 41 insertions(+), 65 deletions(-) diff --git a/web_widget_one2many_product_picker/models/base.py b/web_widget_one2many_product_picker/models/base.py index 90c4049cf..686c1a358 100644 --- a/web_widget_one2many_product_picker/models/base.py +++ b/web_widget_one2many_product_picker/models/base.py @@ -7,70 +7,50 @@ from odoo.exceptions import ValidationError class BaseModel(models.BaseModel): _inherit = "base" + @api.model + def _check_product_picker_duplicated_products(self, vals_list): + relation = self.env.context.get("product_picker_relation") + if relation != self._name or not len(vals_list): + return + product_field = self.env.context.get("product_picker_product_field") + product_ids = [ + values[product_field] for values in vals_list if product_field in values + ] + num_products = len(product_ids) + if not num_products: + return + elif num_products != len(set(product_ids)): + raise ValidationError( + _("Can't create the %s: Duplicated product! (Inside query)") % relation + ) + relation_field = self.env.context.get("product_picker_relation_field") + # All records have the same 'relation id' when created with the product picker + relation_id = vals_list[0][relation_field] + # When write maybe need get the value from the record + if not relation_id: + field_obj = self[relation_field] + if field_obj: + relation_id = relation_id.id + db_sol = self.search( + [ + (relation_field, "=", relation_id), + (product_field, "in", product_ids), + ], + limit=1, + ) + if db_sol: + raise ValidationError( + _("Can't create the %s: Duplicated product (%s)! (Already in database)") + % (relation, db_sol[product_field].display_name) + ) + @api.model_create_multi def create(self, vals_list): - """Avoid create lines that have a product currently used when use the product - picker""" - relation = self.env.context.get("product_picker_relation") - if relation == self._name and len(vals_list): - product_field = self.env.context.get("product_picker_product_field") - product_ids = [ - values[product_field] for values in vals_list if product_field in values - ] - if len(product_ids) != len(set(product_ids)): - raise ValidationError( - _("Can't create the %s: Duplicated product! (Inside query)") - % relation - ) - relation_field = self.env.context.get("product_picker_relation_field") - # All records have the same 'relation id' when created with the product - # picker - relation_id = vals_list[0][relation_field] - has_product = ( - self.search( - [ - (relation_field, "=", relation_id), - (product_field, "in", product_ids), - ], - count=True, - limit=1, - ) - != 0 - ) - if has_product: - raise ValidationError( - _("Can't create the %s: Duplicated product! (Already in database)") - % relation - ) + """Avoid create lines that have a product currently used when use the product picker""" + self._check_product_picker_duplicated_products(vals_list) return super().create(vals_list) def write(self, values): - """Avoid write lines that have a product currently used when use the product - picker""" - relation = self.env.context.get("product_picker_relation") - product_field = self.env.context.get("product_picker_product_field") - if relation == self._name and product_field in values: - relation_field = self.env.context.get("product_picker_relation_field") - relation_id = ( - values[relation_field] - if relation_field in values - else self.get(relation_field) - ) - product_id = values[product_field] - has_product = ( - self.search( - [ - (relation_field, "=", relation_id), - (product_field, "=", product_id), - ], - count=True, - limit=1, - ) - != 0 - ) - if has_product: - raise ValidationError( - _("Can't write the %s: Duplicated product! (Already in database)") - % relation - ) + """Avoid write lines that have a product currently used when use the product picker""" + self._check_product_picker_duplicated_products([values]) return super().write(values) diff --git a/web_widget_one2many_product_picker/static/src/js/widgets/field_one2many_product_picker.js b/web_widget_one2many_product_picker/static/src/js/widgets/field_one2many_product_picker.js index a5025009a..779979b97 100644 --- a/web_widget_one2many_product_picker/static/src/js/widgets/field_one2many_product_picker.js +++ b/web_widget_one2many_product_picker/static/src/js/widgets/field_one2many_product_picker.js @@ -1140,16 +1140,12 @@ odoo.define("web_widget_one2many_product_picker.FieldOne2ManyProductPicker", fun _onResumeAutoSave: function() { // Check if can resume -<<<<<<< HEAD - if (this.$('.oe_product_picker_quick_modif_price').is(':visible') || this.$('.oe_search_input').is(':focus') || this.$('.oe_flip_card.active').length) { -======= if ( !this._is_auto_save_paused || this.$(".oe_product_picker_quick_modif_price").is(":visible") || this.$(".oe_search_input").is(":focus") || this.$(".oe_flip_card.active").length ) { ->>>>>>> aca8ae42 ([FIX] web_widget_one2many_product_picker: Don't save in 'refresh' time) return; }