From f6e2d74a783b9523ff81e20ef3db57d01b41dbbf Mon Sep 17 00:00:00 2001 From: "Nasreddin (bon)" Date: Thu, 21 Jan 2021 10:40:54 +0000 Subject: [PATCH] [FIX] website: Warning message on website if dynamic snippet category deleted Issue - Install "Ecommerce" app - Go to 'Shop' page and edit it - Add a "Dynamic Products" block - Click on it to edit it : - Select any template - Select `Desks` as product category - Save - Go to "Website -> Configuration -> eCommerce Categories" - Delete the "Desks" category - Go back to Shop page Error : "Record does not exist or has been deleted". Cause The search domain is looking for child categories of dataset.productCategoryId (who does not exist anymore in the above case). `child_of` do a browse() to fetch the records, and therefore generate an error if category not found. Solution Log warning message if unexptected domain and return empty records set instead. This commit also reverts commit 0de069b8ca9fb005ba5b076984f5677de25889ee that generated the issue. opw-2440917 closes odoo/odoo#64947 X-original-commit: e452d30f739db39a088248a3109c8c49cff9d49f Signed-off-by: LTU-Odoo Signed-off-by: bon-odoo --- .../website/models/website_snippet_filter.py | 38 ++++++++++++------- .../src/snippets/s_dynamic_snippet/000.js | 6 +-- .../s_dynamic_snippet_products/000.js | 24 ++---------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/addons/website/models/website_snippet_filter.py b/addons/website/models/website_snippet_filter.py index f9f524c0851..a2a8c82e86c 100644 --- a/addons/website/models/website_snippet_filter.py +++ b/addons/website/models/website_snippet_filter.py @@ -3,10 +3,13 @@ from ast import literal_eval from collections import OrderedDict from odoo import models, fields, api, _ -from odoo.exceptions import ValidationError +from odoo.exceptions import ValidationError, MissingError from odoo.osv import expression from odoo.tools import html_escape as escape from lxml import etree as ET +import logging + +_logger = logging.getLogger(__name__) class WebsiteSnippetFilter(models.Model): @@ -70,20 +73,27 @@ class WebsiteSnippetFilter(models.Model): domain = expression.AND([domain, [('is_published', '=', True)]]) if search_domain: domain = expression.AND([domain, search_domain]) - - records = self.env[filter_sudo.model_id].search( - domain, - order=','.join(literal_eval(filter_sudo.sort)) or None, - limit=limit - ) - return self._filter_records_to_dict_values(records) + try: + records = self.env[filter_sudo.model_id].search( + domain, + order=','.join(literal_eval(filter_sudo.sort)) or None, + limit=limit + ) + return self._filter_records_to_dict_values(records) + except MissingError: + _logger.warning("The provided domain %s in 'ir.filters' generated a MissingError in '%s'", domain, self._name) + return [] elif self.action_server_id: - return self.action_server_id.with_context( - dynamic_filter=self, - limit=limit, - search_domain=search_domain, - get_rendering_data_structure=self._get_rendering_data_structure, - ).sudo().run() + try: + return self.action_server_id.with_context( + dynamic_filter=self, + limit=limit, + search_domain=search_domain, + get_rendering_data_structure=self._get_rendering_data_structure, + ).sudo().run() + except MissingError: + _logger.warning("The provided domain %s in 'ir.actions.server' generated a MissingError in '%s'", search_domain, self._name) + return [] @api.model def _get_rendering_data_structure(self): diff --git a/addons/website/static/src/snippets/s_dynamic_snippet/000.js b/addons/website/static/src/snippets/s_dynamic_snippet/000.js index 46b10ea0dc9..71b551738b5 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet/000.js +++ b/addons/website/static/src/snippets/s_dynamic_snippet/000.js @@ -94,14 +94,14 @@ const DynamicSnippet = publicWidget.Widget.extend({ * domain if needed. * @private */ - _getSearchDomain: async function () { + _getSearchDomain: function () { return []; }, /** * Fetches the data. * @private */ - _fetchData: async function () { + _fetchData: function () { if (this._isConfigComplete()) { return this._rpc( { @@ -110,7 +110,7 @@ const DynamicSnippet = publicWidget.Widget.extend({ 'filter_id': parseInt(this.$el.get(0).dataset.filterId), 'template_key': this.$el.get(0).dataset.templateKey, 'limit': parseInt(this.$el.get(0).dataset.numberOfRecords), - 'search_domain': await this._getSearchDomain() + 'search_domain': this._getSearchDomain() }, }) .then( diff --git a/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js b/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js index 44225f83928..3763593f98f 100644 --- a/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js +++ b/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js @@ -13,18 +13,6 @@ const DynamicSnippetProducts = DynamicSnippetCarousel.extend({ // Private //-------------------------------------------------------------------------- - /** - * Check product category exist. - * @private - * @returns {Promise} - */ - _checkCategoryExist: async function (productCategoryId) { - return this._rpc({ - model: 'product.public.category', - method: 'search_count', - args: [[['id', '=', productCategoryId]]], - }).then(nb => nb > 0); - }, /** * Method to be overridden in child components if additional configuration elements * are required in order to fetch data. @@ -40,15 +28,9 @@ const DynamicSnippetProducts = DynamicSnippetCarousel.extend({ * @override * @private */ - _getSearchDomain: async function () { - const searchDomain = await this._super.apply(this, arguments); - const productCategoryId = parseInt(this.$el.get(0).dataset.productCategoryId); - const categoryExist = await this._checkCategoryExist(productCategoryId); - if (categoryExist) { - searchDomain.push(['public_categ_ids', 'child_of', productCategoryId]); - } else { - searchDomain.push([0, '=', 1]); - } + _getSearchDomain: function () { + const searchDomain = this._super.apply(this, arguments); + searchDomain.push(['public_categ_ids', 'child_of', parseInt(this.$el.get(0).dataset.productCategoryId)]); return searchDomain; },