Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion auditlog/models/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import copy
from collections import defaultdict

from odoo import Command, _, api, fields, models
from odoo.exceptions import UserError
from odoo.tools.misc import OrderedSet

FIELDS_BLACKLIST = [
"id",
Expand Down Expand Up @@ -61,14 +63,20 @@ def __init__(self, env):
self._transaction = env.transaction

def __enter__(self):
"""Replace the cache on all envs and on the transaction.
"""Replace the cache + tocompute on all envs and on the transaction.

It is not enough to replace the cache on the current env, because once
a sudo is executed under the scope of this context manager, another new
or existing env is fetched which will have the original cache if we
don't swap them all out here.
"""
self._original_cache = self._transaction.cache
# Copy the sets of records, which are popped on recompute but do not
# copy the keys because they do not match the original field object
# afterwards.
self._original_tocompute = defaultdict(OrderedSet)
for key, value in self._transaction.tocompute.items():
self._original_tocompute[key] = OrderedSet(value)
temporary_cache = api.Cache()
for env in self._transaction.envs:
env.cache = temporary_cache
Expand All @@ -80,6 +88,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
for env in self._transaction.envs:
env.cache = self._original_cache
self._transaction.cache = self._original_cache
self._transaction.tocompute = self._original_tocompute


class AuditlogRule(models.Model):
Expand Down
14 changes: 14 additions & 0 deletions test_auditlog/tests/test_account_move_reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ def setUp(self):
)
self.rule.subscribe()

def test_in_invoice_stored_related_field(self):
"""Stored related fields are computed properly"""
self.invoice.name = "TEST"
line = self.env["account.move.line"].create(
{
"display_type": "line_note",
"name": __name__,
"move_id": self.invoice.id,
}
)
self.assertEqual(line.move_name, "TEST")
self.invoice.name = "TEST2"
self.assertEqual(line.move_name, "TEST2")

def test_in_invoice_create_refund(self):
"""Test creating a refund from a vendor bill.

Expand Down