Merge PR #2913 into 16.0

Signed-off-by dreispt
pull/2923/head
OCA-git-bot 2024-04-23 14:26:10 +00:00
commit e9d6b73e52
1 changed files with 21 additions and 14 deletions

View File

@ -4,8 +4,8 @@
# Copyright 2020 Hibou Corp. # Copyright 2020 Hibou Corp.
# Copyright 2023 ACSONE SA/NV (http://acsone.eu) # Copyright 2023 ACSONE SA/NV (http://acsone.eu)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import logging import logging
from collections import defaultdict
from odoo import _, api, models from odoo import _, api, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
@ -35,9 +35,12 @@ class BaseExceptionMethod(models.AbstractModel):
""" """
return [("model", "=", self._name), ("active", "=", True)] return [("model", "=", self._name), ("active", "=", True)]
def detect_exceptions(self): def _get_exceptions(self):
"""List all exception_ids applied on self """
Exception ids are also written on records Returns a tuple with:
- All exceptions
- Rules to remove with recordset
- Rules to add with recordset
""" """
rules_info = ( rules_info = (
self.env["exception.rule"] self.env["exception.rule"]
@ -45,25 +48,29 @@ class BaseExceptionMethod(models.AbstractModel):
._get_rules_info_for_domain(self._rule_domain()) ._get_rules_info_for_domain(self._rule_domain())
) )
all_exception_ids = [] all_exception_ids = []
rules_to_remove = {} main_records = self._get_main_records()
rules_to_add = {} rules_to_remove = defaultdict(main_records.browse)
rules_to_add = defaultdict(main_records.browse)
for rule_info in rules_info: for rule_info in rules_info:
main_records = self._get_main_records()
records_with_rule_in_exceptions = main_records.filtered( records_with_rule_in_exceptions = main_records.filtered(
lambda r, rule_id=rule_info.id: rule_id in r.exception_ids.ids lambda r, rule_id=rule_info.id: rule_id in r.exception_ids.ids
) )
records_with_exception = self._detect_exceptions(rule_info) records_with_exception = self._detect_exceptions(rule_info)
to_remove = records_with_rule_in_exceptions - records_with_exception to_remove = records_with_rule_in_exceptions - records_with_exception
to_add = records_with_exception - records_with_rule_in_exceptions to_add = records_with_exception - records_with_rule_in_exceptions
# we expect to always work on the same model type if to_remove:
if rule_info.id not in rules_to_remove: rules_to_remove[rule_info.id] |= to_remove
rules_to_remove[rule_info.id] = main_records.browse() if to_add:
rules_to_remove[rule_info.id] |= to_remove rules_to_add[rule_info.id] |= to_add
if rule_info.id not in rules_to_add:
rules_to_add[rule_info.id] = main_records.browse()
rules_to_add[rule_info.id] |= to_add
if records_with_exception: if records_with_exception:
all_exception_ids.append(rule_info.id) all_exception_ids.append(rule_info.id)
return all_exception_ids, rules_to_remove, rules_to_add
def detect_exceptions(self):
"""List all exception_ids applied on self
Exception ids are also written on records
"""
all_exception_ids, rules_to_remove, rules_to_add = self._get_exceptions()
# Cumulate all the records to attach to the rule # Cumulate all the records to attach to the rule
# before linking. We don't want to call "rule.write()" # before linking. We don't want to call "rule.write()"
# which would: # which would: