Skip to content

Commit

Permalink
[FIX] connector_search_engine: export and delete on multi bindings
Browse files Browse the repository at this point in the history
Fix error when calling the methods *export_record* or *delete_record* from
the *se.binding* model when called on a recordset with items from different
*se.backend*.

The *export* and *delete* methods involves the use of a *Backend Adapter* to
communicate with the target search engine. We then need to process the bindings
by backend to call the correct adapter and ensure at same time a batch process
of the requested operation for all the records linked to the same backend.
  • Loading branch information
lmignon committed Nov 29, 2023
1 parent a0bb69f commit 3851e0b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion connector_search_engine/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Connector Search Engine
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:d910842e730b94188c461123753f54ef498d2471eb4a7867c3b292195e48df94
!! source digest: sha256:e07468d87b2297ae04803eedb4dbd843aac62e7a66f4c80b61efff18347b4fda
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
2 changes: 1 addition & 1 deletion connector_search_engine/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"website": "https://github.com/OCA/search-engine",
"license": "AGPL-3",
"category": "Generic Modules",
"depends": ["queue_job", "mail", "server_environment"],
"depends": ["queue_job", "mail", "server_environment", "base_partition"],
"external_dependencies": {"python": ["unidecode"]},
"data": [
"wizards/se_binding_state_updater.xml",
Expand Down
10 changes: 6 additions & 4 deletions connector_search_engine/models/se_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,17 @@ def recompute_json(self, force_export: bool = False):
record.error = ""

def export_record(self) -> str:
adapter = self.index_id.se_adapter
adapter.index([record.get_export_data() for record in self])
for _backend, bindings in self.partition("backend_id").items():
adapter = bindings[0].index_id.se_adapter
adapter.index([record.get_export_data() for record in bindings])
self.state = "done"
return "Exported ids : {}".format(self.ids)

def delete_record(self) -> str:
adapter = self.index_id.se_adapter
record_ids = self.mapped("res_id")
adapter.delete(record_ids)
for _backend, bindings in self.partition("backend_id").items():
adapter = bindings[0].index_id.se_adapter
adapter.delete(bindings.mapped("res_id"))
self.unlink()
return "Deleted ids : {}".format(record_ids)

Expand Down
8 changes: 8 additions & 0 deletions connector_search_engine/readme/newsfragments/173.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Fix error when calling the methods *export_record* or *delete_record* from
the *se.binding* model when called on a recordset with items from different
*se.backend*.

The *export* and *delete* methods involves the use of a *Backend Adapter* to
communicate with the target search engine. We then need to process the bindings
by backend to call the correct adapter and ensure at same time a batch process
of the requested operation for all the records linked to the same backend.
2 changes: 1 addition & 1 deletion connector_search_engine/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Connector Search Engine</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:d910842e730b94188c461123753f54ef498d2471eb4a7867c3b292195e48df94
!! source digest: sha256:e07468d87b2297ae04803eedb4dbd843aac62e7a66f4c80b61efff18347b4fda
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/search-engine/tree/16.0/connector_search_engine"><img alt="OCA/search-engine" src="https://img.shields.io/badge/github-OCA%2Fsearch--engine-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/search-engine-16-0/search-engine-16-0-connector_search_engine"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/search-engine&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Base module for connecting Odoo with external search engines. This addon is
Expand Down
20 changes: 20 additions & 0 deletions connector_search_engine/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,23 @@ def test_binding_ids_on_record(self):
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)

def test_binding_multi_backend_index(self):
backend2 = self.backend_model.create(
{"name": "Fake 2", "tech_name": "fake_se_2", "backend_type": "fake"}
)
index2_vals = self._prepare_index_values(backend2)
index2_vals.update(
{"name": "Partner Index 2", "lang_id": self.env.ref("base.lang_fr").id}
)
index2 = self.se_index_model.create(index2_vals)
binding1 = self.partner._add_to_index(self.se_index)
binding2 = self.partner._add_to_index(index2)
bindings = binding1 | binding2
self.assertEqual(self.partner.se_binding_ids, bindings)
bindings.recompute_json()
self.assertEqual(self.partner_binding.mapped("state"), ["to_export"])
bindings.export_record()
self.assertEqual(self.partner_binding.mapped("state"), ["done"])
bindings.delete_record()
self.assertFalse(self.partner_binding.exists())

0 comments on commit 3851e0b

Please sign in to comment.