From db98fc0c291a1ebc3497d24e1f2d8810127512e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A0n=20Todorovich?= Date: Wed, 27 Oct 2021 11:32:53 -0300 Subject: [PATCH] [IMP] base_exception: Allow to skip exception checks through context --- base_exception/models/base_exception.py | 27 +++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/base_exception/models/base_exception.py b/base_exception/models/base_exception.py index a7785c0ba..c331d37b9 100644 --- a/base_exception/models/base_exception.py +++ b/base_exception/models/base_exception.py @@ -244,17 +244,28 @@ class BaseExceptionModel(models.AbstractModel): return self.env.ref("base_exception.action_exception_rule_confirm") def _check_exception(self): - """ + """Check exceptions + This method must be used in a constraint that must be created in the object that inherits for base.exception. - for sale : - @api.constrains('ignore_exception',) - def sale_check_exception(self): - ... - ... - self._check_exception + + .. code-block:: python + + @api.constrains("ignore_exception") + def sale_check_exception(self): + # ... + self._check_exception() + + For convenience, this check can be skipped by setting check_exception=False + in context. + + Exceptions will be raised as ValidationError, but this can be disabled + by setting raise_exception=False in context. They will still be detected + and updated on the related record, though. """ + if not self.env.context.get("check_exception", True): # pragma: no cover + return True exception_ids = self.detect_exceptions() - if exception_ids: + if exception_ids and self.env.context.get("raise_exception", True): exceptions = self.env["exception.rule"].browse(exception_ids) raise ValidationError("\n".join(exceptions.mapped("name")))