Skip to content

Commit

Permalink
[FIX] connector_search_engine: Invalidate se_binding_ids on binding CRUD
Browse files Browse the repository at this point in the history
When bindings are created / updated / unlinked, we must invalidate the 'se_binding_ids' field of related records. In the case of an update, the invalidation is only done if the reference to the record changes
  • Loading branch information
lmignon committed Oct 13, 2023
1 parent bb948a4 commit 75a850a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions connector_search_engine/models/se_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import json
import logging
from collections import defaultdict
from typing import Any, Dict, Iterator

from typing_extensions import Self

from odoo import _, api, fields, models, tools
from odoo.exceptions import ValidationError

Expand Down Expand Up @@ -201,3 +204,35 @@ def recompute_from_owner(self):
)
bindings.write({"state": "recomputing"})
bindings.jobify_recompute_json()

@api.model_create_multi
def create(self, vals_list) -> Self:
"""Create a binding and compute its JSON."""
records = super().create(vals_list)
records._invalidate_bindings_in_references()
return records

def write(self, vals) -> bool:
"""Write a binding and compute its JSON."""
if "res_model" in vals or "res_id" in vals:
# invalidate bindings in current references
self._invalidate_bindings_in_references()
res = super().write(vals)
if "res_model" in vals or "res_id" in vals:
# invalidate bindings in new references
self._invalidate_bindings_in_references()
return res

def unlink(self):
self._invalidate_bindings_in_references()
return super().unlink()

def _invalidate_bindings_in_references(self):
"""Invalidate the binding_ids field on the records referenced by the
bindings.
"""
ids_by_model = defaultdict(list)
for binding in self:
ids_by_model[binding.res_model].append(binding.res_id)
for model, ids in ids_by_model.items():
self.env[model].browse(ids).invalidate_recordset(["se_binding_ids"])
7 changes: 7 additions & 0 deletions connector_search_engine/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,10 @@ def test_life_cycle(self):
kwargs={},
)
self.assertFalse(self.partner_binding.exists())

def test_binding_ids_on_record(self):
self.assertEqual(self.partner.se_binding_ids, self.partner_binding)
self.partner_binding.unlink()
self.assertFalse(self.partner.se_binding_ids)
partner_binding = self.partner._add_to_index(self.se_index)
self.assertEqual(self.partner.se_binding_ids, partner_binding)

0 comments on commit 75a850a

Please sign in to comment.