Skip to content

Commit

Permalink
[IMP] connector_search_engine: check binding model
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard authored and lmignon committed Dec 15, 2023
1 parent d6c553f commit e828c12
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 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:d3f9f139fa17b9a02bf4d6648949fa2e1cb38128197922065b0dee912080c288
!! source digest: sha256:bc20167daf51d9976d3ef6d5d1fd82264bfabd8399525e86f526406d4021337d
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
11 changes: 11 additions & 0 deletions connector_search_engine/models/se_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class SeBinding(models.Model):
),
]

@api.constrains("res_model", "index_id")
def _check_model(self):
for binding in self:
if (
binding.res_model
and binding.res_model != binding.index_id.model_id.model
):
raise ValidationError(
_("Binding model must be equal to the index model")
)

@tools.ormcache()
@api.model
def _get_indexable_model_selection(self):
Expand Down
7 changes: 7 additions & 0 deletions connector_search_engine/readme/newsfragments/177.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Ensure that the record's model is compatible with the index's model before
adding a new record to the index.

Before this change, the index would silently ignore records that were not
compatible with the index's model. This could lead to unexpected behavior and
errors when the record was later used to be serialized to JSON and exported to
a search engine.
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:d3f9f139fa17b9a02bf4d6648949fa2e1cb38128197922065b0dee912080c288
!! source digest: sha256:bc20167daf51d9976d3ef6d5d1fd82264bfabd8399525e86f526406d4021337d
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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
5 changes: 5 additions & 0 deletions connector_search_engine/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ def _get_serializer(self):
class ResPartner(models.Model):
_name = "res.partner"
_inherit = ["res.partner", "se.indexable.record"]


class ResUsers(models.Model):
_name = "res.users"
_inherit = ["res.users", "se.indexable.record"]
12 changes: 11 additions & 1 deletion connector_search_engine/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from odoo_test_helper import FakeModelLoader

from odoo.exceptions import ValidationError
from odoo.tests.common import Form
from odoo.tools import mute_logger

Expand All @@ -25,11 +26,12 @@ def setUpClass(cls):
FakeSeAdapter,
FakeSerializer,
ResPartner,
ResUsers,
SeBackend,
SeIndex,
)

cls.loader.update_registry((ResPartner, SeBackend, SeIndex))
cls.loader.update_registry((ResPartner, ResUsers, SeBackend, SeIndex))
cls.binding_model = cls.env["se.binding"]
cls.se_index_model = cls.env["se.index"]

Expand Down Expand Up @@ -381,3 +383,11 @@ def test_binding_multi_backend_index(self):
self.assertEqual(self.partner_binding.mapped("state"), ["done"])
bindings.delete_record()
self.assertFalse(self.partner_binding.exists())

def test_binding_wrong_model(self):
# Try to add a 'res.users' record to a 'res.partner' index
user = self.env["res.users"].search([], limit=1)
with self.assertRaisesRegex(
ValidationError, "Binding model must be equal to the index model"
):
user._add_to_index(self.se_index)

0 comments on commit e828c12

Please sign in to comment.