From 55a6f2e99668cc0f685680a4ec890e25f61592b3 Mon Sep 17 00:00:00 2001 From: Thibault Libioulle Date: Tue, 22 Mar 2022 14:59:14 +0000 Subject: [PATCH] [FIX] web_editor: catch errors during save and allow to fix it Prior to this commit, when an exception occured during the save of an ir.ui.view, the traceback was shown to the user and an alert directly asks the user to reload or cancel the page. While reloading, the changes that produced the traceback were not saved and the other modifications were saved. If the user choose to cancel the reload, the web editor UI was freezed and there couldn't be any edition anymore. This commit adds correctly the popover to target the invalid element and shows the error rather than reloading the page. The element which caused the error can still be edited. The other are no more editable. task-2700198 closes odoo/odoo#87350 X-original-commit: 10b97f37ab5d78b3a2b0f63fa019ee95bb78ea7d Signed-off-by: Yannick Tivisse (yti) Signed-off-by: Thibault Libioulle (tle) --- addons/web_editor/models/ir_qweb_fields.py | 17 ++++++++++++----- .../web_editor/static/src/js/wysiwyg/wysiwyg.js | 7 +++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/addons/web_editor/models/ir_qweb_fields.py b/addons/web_editor/models/ir_qweb_fields.py index dbf3749abff..d5a6b295c76 100644 --- a/addons/web_editor/models/ir_qweb_fields.py +++ b/addons/web_editor/models/ir_qweb_fields.py @@ -28,7 +28,8 @@ from werkzeug import urls import odoo.modules -from odoo import api, models, fields +from odoo import _, api, models, fields +from odoo.exceptions import ValidationError from odoo.tools import ustr, posix_to_ldml, pycompat from odoo.tools import html_escape as escape from odoo.tools.misc import get_lang, babel_locale_parse @@ -181,8 +182,11 @@ class Float(models.AbstractModel): def from_html(self, model, field, element): lang = self.user_lang() value = element.text_content().strip() - return float(value.replace(lang.thousands_sep, '') - .replace(lang.decimal_point, '.')) + try: + return float(value.replace(lang.thousands_sep, '') + .replace(lang.decimal_point, '.')) + except: + raise ValidationError(_('You entered an invalid value, please try again.')) class ManyToOne(models.AbstractModel): @@ -478,8 +482,11 @@ class Monetary(models.AbstractModel): value = element.find('span').text.strip() - return float(value.replace(lang.thousands_sep, '') - .replace(lang.decimal_point, '.')) + try: + return float(value.replace(lang.thousands_sep, '') + .replace(lang.decimal_point, '.')) + except: + raise ValidationError(_('You entered an invalid value, please try again.')) class Duration(models.AbstractModel): diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js index 47db234477f..862c1b279f9 100644 --- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js +++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js @@ -1844,13 +1844,13 @@ const Wysiwyg = Widget.extend({ // the DOM regeneration. Add markings instead, and returns a // new rejection with all relevant info var id = _.uniqueId('carlos_danger_'); - $el.addClass('o_dirty oe_carlos_danger ' + id); + $el.addClass('o_dirty o_editable oe_carlos_danger ' + id); $('.o_editable.' + id) .removeClass(id) .popover({ trigger: 'hover', content: response.message.data.message || '', - placement: 'auto top', + placement: 'auto', }) .popover('show'); }); @@ -1860,8 +1860,7 @@ const Wysiwyg = Widget.extend({ window.onbeforeunload = null; }).guardedCatch((failed) => { // If there were errors, re-enable edition - this.cancel(); - this.start(); + this.cancel(false); }); }, // TODO unused => remove or reuse as it should be