From 28075c915bcf74cb1e3c41784801bdd9d77c4e73 Mon Sep 17 00:00:00 2001 From: "Adrien Widart (awt)" Date: Fri, 21 Apr 2023 10:06:05 +0000 Subject: [PATCH] [FIX] core: translate SQL constraints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To reproduce the issue: 1. Install `mrp` 2. Send a RPC to create a new BoM: - `{"product_tmpl_id": 1, "product_qty": -1}` Error: It will generate a traceback Because of the negative product qty, the RPC triggers a SQL constraint that we try to return. However, the context does not have any `lang`, hence the traceback sentry-4088426130 closes odoo/odoo#120614 X-original-commit: badc554b9dd7a299aac8ebca24bec6b90bef779a Signed-off-by: Julien Castiaux (juc) Signed-off-by: Rémy Voet --- odoo/addons/test_rpc/models.py | 5 +++++ odoo/addons/test_rpc/tests/test_error.py | 6 ++++++ odoo/service/model.py | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/odoo/addons/test_rpc/models.py b/odoo/addons/test_rpc/models.py index 2d547a9947e..24b8e536708 100644 --- a/odoo/addons/test_rpc/models.py +++ b/odoo/addons/test_rpc/models.py @@ -18,3 +18,8 @@ class ModelB(models.Model): _description = "Model B" name = fields.Char(required=True) + value = fields.Integer() + + _sql_constraints = [ + ('qty_positive', 'check (value > 0)', 'The value must be positive'), + ] diff --git a/odoo/addons/test_rpc/tests/test_error.py b/odoo/addons/test_rpc/tests/test_error.py index e69a14b1d59..df32173781c 100644 --- a/odoo/addons/test_rpc/tests/test_error.py +++ b/odoo/addons/test_rpc/tests/test_error.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from functools import partial +from xmlrpc.client import Fault from odoo.tests import common, tagged from odoo.tools.misc import mute_logger @@ -65,3 +66,8 @@ class TestError(common.HttpCase): ) self.assertIn("Model: Model A (test_rpc.model_a)", e.faultString) self.assertIn("Constraint: test_rpc_model_a_field_b2_fkey", e.faultString) + + def test_03_sql_constraint(self): + with mute_logger("odoo.sql_db"): + with self.assertRaisesRegex(Fault, r'The operation cannot be completed: The value must be positive'): + self.rpc("test_rpc.model_b", "create", {"name": "B1", "value": -1}) diff --git a/odoo/service/model.py b/odoo/service/model.py index c31a14977d5..a4b46f96016 100644 --- a/odoo/service/model.py +++ b/odoo/service/model.py @@ -109,7 +109,7 @@ def _as_validation_error(env, exc): if exc.diag.constraint_name in env.registry._sql_constraints: return ValidationError(_( "The operation cannot be completed: %s", - translate_sql_constraint(env.cr, exc.diag.constraint_name, env.context['lang']) + translate_sql_constraint(env.cr, exc.diag.constraint_name, env.context.get('lang', 'en_US')) )) return ValidationError(_("The operation cannot be completed: %s", exc.args[0]))