Skip to content

Commit

Permalink
[IMP] partner_pricelist_search: support searching subfields
Browse files Browse the repository at this point in the history
Previously we couldn't make an advanced search to filter pricelist
subfields.

Currently, only possitive operators are working for advanced searches as
negative ones seem to be encapsulating the queries and we can't use them
in the point we're overriding. Further research would need to be done in
order to cover it.

TT47648
  • Loading branch information
chienandalu committed Mar 4, 2024
1 parent a823c54 commit a435b59
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
12 changes: 7 additions & 5 deletions partner_pricelist_search/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Partner pricelist search
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:8d7baaa5390f3b784fc7e4931e37709178dfbb3fc819ff25f5ad37c1fab45b51
!! source digest: sha256:8a03da2b856c7e8372bb9f5cde5c4d35d517ba154d08230490e3fe08e435d126
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -41,10 +41,11 @@ partners with that pricelist.
Known issues / Roadmap
======================

With many partner records the search can be slow because all partners are
computed.

No grouping is allowed.
* With many partner records the search can be slow because all partners are computed.
* No grouping is allowed.
* We can't use negative operators on advanced searches as they won't throw the right
results. This seems to be due tu the subfield negative search where we end up having
an `=` operator and a ``Query`` object that is use to optimize this kind of searches.

Bug Tracker
===========
Expand All @@ -71,6 +72,7 @@ Contributors

* Carlos Dauden
* Víctor Martínez
* David Vidal

Maintainers
~~~~~~~~~~~
Expand Down
16 changes: 14 additions & 2 deletions partner_pricelist_search/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def search(self, args, offset=0, limit=None, order=None, count=False):
# Substitute pricelist tuple
partner_domain = [
(1, "=", 1)
if (isinstance(x, (list, tuple)) and x[0] == "property_product_pricelist")
if (
isinstance(x, (list, tuple))
and (isinstance(x[0], str))
and x[0].startswith("property_product_pricelist")
)
else x
for x in args
]
Expand All @@ -35,4 +39,12 @@ def _search_property_product_pricelist(self, operator, value):
domain = self.env.context.get("search_partner_domain", [])
partners = self.with_context(prefetch_fields=False).search(domain)
key = "property_product_pricelist"
return [("id", "in", partners.filtered_domain([(key, operator, value)]).ids)]
pricelist_search_field = "name" if isinstance(value, str) else "id"
pricelist_ids = (
self.env["product.pricelist"]
.search([(pricelist_search_field, operator, value)])
.ids
)
return [
("id", "in", partners.filtered_domain([(key, "in", pricelist_ids)]).ids)
]
1 change: 1 addition & 0 deletions partner_pricelist_search/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

* Carlos Dauden
* Víctor Martínez
* David Vidal
9 changes: 5 additions & 4 deletions partner_pricelist_search/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
With many partner records the search can be slow because all partners are
computed.

No grouping is allowed.
* With many partner records the search can be slow because all partners are computed.
* No grouping is allowed.
* We can't use negative operators on advanced searches as they won't throw the right
results. This seems to be due tu the subfield negative search where we end up having
an `=` operator and a ``Query`` object that is use to optimize this kind of searches.
13 changes: 9 additions & 4 deletions partner_pricelist_search/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Partner pricelist search</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:8d7baaa5390f3b784fc7e4931e37709178dfbb3fc819ff25f5ad37c1fab45b51
!! source digest: sha256:8a03da2b856c7e8372bb9f5cde5c4d35d517ba154d08230490e3fe08e435d126
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/partner-contact/tree/15.0/partner_pricelist_search"><img alt="OCA/partner-contact" src="https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/partner-contact-15-0/partner-contact-15-0-partner_pricelist_search"><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/partner-contact&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds search by pricelist field option in partners.</p>
Expand All @@ -388,9 +388,13 @@ <h1 class="title">Partner pricelist search</h1>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-1">Known issues / Roadmap</a></h1>
<p>With many partner records the search can be slow because all partners are
computed.</p>
<p>No grouping is allowed.</p>
<ul class="simple">
<li>With many partner records the search can be slow because all partners are computed.</li>
<li>No grouping is allowed.</li>
<li>We can’t use negative operators on advanced searches as they won’t throw the right
results. This seems to be due tu the subfield negative search where we end up having
an <cite>=</cite> operator and a <tt class="docutils literal">Query</tt> object that is use to optimize this kind of searches.</li>
</ul>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
Expand All @@ -414,6 +418,7 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Carlos Dauden</li>
<li>Víctor Martínez</li>
<li>David Vidal</li>
</ul>
</li>
</ul>
Expand Down
59 changes: 59 additions & 0 deletions partner_pricelist_search/tests/test_partner_pricelist_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,32 @@ def setUpClass(cls):
cls.pricelist_2 = cls.env["product.pricelist"].create(
{"name": "Test pricelist 2"}
)
cls.pricelist_3 = cls.env["product.pricelist"].create(
{
"name": "Test pricelist 3",
"item_ids": [
(
0,
0,
{
"base": "pricelist",
"base_pricelist_id": cls.pricelist_2.id,
"compute_price": "percentage",
"percent_price": 15,
},
)
],
}
)
cls.customer_1 = cls.env["res.partner"].create(
{"name": "Test customer 1", "property_product_pricelist": cls.pricelist_1}
)
cls.customer_2 = cls.env["res.partner"].create(
{"name": "Test customer 2", "property_product_pricelist": cls.pricelist_2}
)
cls.customer_3 = cls.env["res.partner"].create(
{"name": "Test customer 3", "property_product_pricelist": cls.pricelist_3}
)
cls.partner_obj = cls.env["res.partner"]

def test_partner_pricelist_search_equal(self):
Expand All @@ -30,6 +50,19 @@ def test_partner_pricelist_search_equal(self):
)
self.assertEqual(partners, self.customer_1)

def test_partner_pricelist_advanced_search_equal(self):
"""Test search '='"""
partners = self.partner_obj.search(
[
(
"property_product_pricelist.item_ids.base_pricelist_id",
"=",
self.pricelist_2.id,
)
]
)
self.assertEqual(partners, self.customer_3)

def test_partner_pricelist_search_in(self):
"""Test search 'in'"""
partners = self.partner_obj.search(
Expand All @@ -44,6 +77,19 @@ def test_partner_pricelist_search_in(self):
self.assertIn(self.customer_1, partners)
self.assertIn(self.customer_2, partners)

def test_partner_pricelist_advanced_search_in(self):
"""Test advanced search 'in'"""
partners = self.partner_obj.search(
[
(
"property_product_pricelist.item_ids.base_pricelist_id",
"in",
(self.pricelist_1 | self.pricelist_2).ids,
)
]
)
self.assertEqual(partners, self.customer_3)

def test_partner_pricelist_search_not_equal(self):
"""Test search 'not equal'"""
partners = self.partner_obj.search(
Expand Down Expand Up @@ -74,6 +120,19 @@ def test_partner_pricelist_search_ilike(self):
self.assertIn(self.customer_1, partners)
self.assertNotIn(self.customer_2, partners)

def test_partner_pricelist_advanced_search_ilike(self):
"""Test advanced search 'ilike'"""
partners = self.partner_obj.search(
[
(
"property_product_pricelist.item_ids.base_pricelist_id",
"ilike",
"Test pricelist 2",
)
]
)
self.assertEqual(partners, self.customer_3)

def test_show_pricelist_partners(self):
res = self.pricelist_1.show_pricelist_partners()
self.assertEqual(self.partner_obj.search(res["domain"]), self.customer_1)
Expand Down

0 comments on commit a435b59

Please sign in to comment.