Skip to content

Commit

Permalink
Merge ca6b571 into 069f1e4
Browse files Browse the repository at this point in the history
  • Loading branch information
guewen committed Aug 14, 2019
2 parents 069f1e4 + ca6b571 commit 23d4b60
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions base_exception/models/base_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,32 @@ def _rule_domain(self):
def detect_exceptions(self):
"""List all exception_ids applied on self
Exception ids are also written on records
If self is empty, check exceptions on all active records.
"""
rules = self.env['exception.rule'].sudo().search(
self._rule_domain())
all_exception_ids = []
rules_to_remove = {}
rules_to_add = {}
for rule in rules:
records_with_exception = self._detect_exceptions(rule)
reverse_field = self._reverse_field()
if self:
main_records = self._get_main_records()
commons = main_records & rule[reverse_field]
to_remove = commons - records_with_exception
to_add = records_with_exception - commons
to_remove_list = [(3, x.id, _) for x in to_remove]
to_add_list = [(4, x.id, _) for x in to_add]
rule.write({reverse_field: to_remove_list + to_add_list})
else:
rule.write({
reverse_field: [(6, 0, records_with_exception.ids)]
})
main_records = self._get_main_records()
commons = main_records & rule[reverse_field]
to_remove = commons - records_with_exception
to_add = records_with_exception - commons
# we expect to always work on the same model type
rules_to_remove.setdefault(
rule.id, main_records.browse()
).update(to_remove)
rules_to_add.setdefault(
rule.id, main_records.browse()
).update(to_add)
if records_with_exception:
all_exception_ids.append(rule.id)
for rule_id, records in rules_to_remove.iteritems():
records.write({'exception_ids': [(3, rule_id,)]})
for rule_id, records in rules_to_add.iteritems():
records.write(({'exception_ids': [(4, rule_id,)]}))
return all_exception_ids

@api.model
Expand Down Expand Up @@ -148,16 +152,12 @@ def _detect_exceptions(self, rule):

@api.multi
def _get_base_domain(self):
domain = [('ignore_exception', '=', False)]
if self:
domain = osv.expression.AND([domain, [('id', 'in', self.ids)]])
return domain
return [('ignore_exception', '=', False), ('id', 'in', self.ids)]

@api.multi
def _detect_exceptions_by_py_code(self, rule):
"""
Find exceptions found on self.
If self is empty, check on all records.
"""
domain = self._get_base_domain()
records = self.search(domain)
Expand All @@ -171,7 +171,6 @@ def _detect_exceptions_by_py_code(self, rule):
def _detect_exceptions_by_domain(self, rule):
"""
Find exceptions found on self.
If self is empty, check on all records.
"""
base_domain = self._get_base_domain()
rule_domain = rule._get_domain()
Expand Down

0 comments on commit 23d4b60

Please sign in to comment.