diff --git a/base_exception/models/base_exception_method.py b/base_exception/models/base_exception_method.py index 61923098aaf..8f8c0e98596 100644 --- a/base_exception/models/base_exception_method.py +++ b/base_exception/models/base_exception_method.py @@ -125,14 +125,17 @@ def _detect_exceptions(self, rule_info): elif rule_info.exception_type == "by_method": return self._detect_exceptions_by_method(rule_info) - def _get_base_domain(self): - return [("ignore_exception", "=", False)] + def _get_base_domain(self, rule_info=None): + domain = [("ignore_exception", "=", False)] + if rule_info is not None and rule_info.filter_domain: + domain = expression.AND([domain, safe_eval(rule_info.filter_domain)]) + return domain def _detect_exceptions_by_py_code(self, rule_info): """ Find exceptions found on self. """ - domain = self._get_base_domain() + domain = self._get_base_domain(rule_info=rule_info) records = self.filtered_domain(domain) records_with_exception = self.env[self._name] for record in records: @@ -144,7 +147,7 @@ def _detect_exceptions_by_domain(self, rule_info): """ Find exceptions found on self. """ - base_domain = self._get_base_domain() + base_domain = self._get_base_domain(rule_info=rule_info) rule_domain = rule_info.domain domain = expression.AND([base_domain, rule_domain]) return self.filtered_domain(domain) @@ -153,6 +156,6 @@ def _detect_exceptions_by_method(self, rule_info): """ Find exceptions found on self. """ - base_domain = self._get_base_domain() + base_domain = self._get_base_domain(rule_info=rule_info) records = self.filtered_domain(base_domain) return getattr(records, rule_info.method)() diff --git a/base_exception/models/exception_rule.py b/base_exception/models/exception_rule.py index 1a51ea6e1ab..d03dda7fd20 100644 --- a/base_exception/models/exception_rule.py +++ b/base_exception/models/exception_rule.py @@ -22,7 +22,12 @@ class ExceptionRule(models.Model): name = fields.Char("Exception Name", required=True, translate=True) description = fields.Text(translate=True) sequence = fields.Integer(help="Gives the sequence order when applying the test") - model = fields.Selection(selection=[], string="Apply on", required=True) + model = fields.Selection(selection=[], string="Apply on model", required=True) + filter_domain = fields.Char( + string="Apply on records", + default="[]", + help="If present, this condition must be satisfied to trigger the exception.", + ) exception_type = fields.Selection( selection=[ @@ -119,6 +124,7 @@ def _to_cache_entry(self): "method": self.method, "code": self.code, "is_blocking": self.is_blocking, + "filter_domain": self.filter_domain, } @api.model_create_multi diff --git a/base_exception/tests/test_base_exception.py b/base_exception/tests/test_base_exception.py index 9c191178407..e0d4559d34a 100644 --- a/base_exception/tests/test_base_exception.py +++ b/base_exception/tests/test_base_exception.py @@ -147,3 +147,8 @@ def test_blocking_exception(self): self.po.with_context(raise_exception=False).button_confirm() self.assertTrue(self.po.exception_ids) self.assertTrue(self.po.exceptions_summary) + + def test_filtered_domain(self): + self.exception_rule.filter_domain = "['|', ('partner_id.country_id', '=', False), ('partner_id.country_id.zip_required', '=', True)]" # noqa + self.partner.country_id = self.env.ref("base.bz") + self.po.button_confirm() diff --git a/base_exception/views/base_exception_view.xml b/base_exception/views/base_exception_view.xml index 79c23300a55..0ba66ad8101 100644 --- a/base_exception/views/base_exception_view.xml +++ b/base_exception/views/base_exception_view.xml @@ -42,6 +42,11 @@ +