Skip to content

Commit

Permalink
[FIX] base: allow finding states by display_name
Browse files Browse the repository at this point in the history
That is how states are (might be?) exported, so it should be possible
to find them the same way.

closes odoo#158984

Task-id: 3644762
X-original-commit: 26df8e2
Signed-off-by: Xavier Morel (xmo) <[email protected]>
  • Loading branch information
xmo-odoo committed Mar 25, 2024
1 parent c8456e2 commit 429a37b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
15 changes: 14 additions & 1 deletion odoo/addons/base/models/res_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,25 @@ def _name_search(self, name, domain=None, operator='ilike', limit=None, order=No
first_state_ids = list(self._search(
expression.AND([domain1, domain]), limit=limit, order=order,
))
fallback_domain = None
if name:
m = re.fullmatch(r"(?P<name>.+)\((?P<country>.+)\)", name)
if m:
fallback_domain = [
('name', operator, m['name'].strip()),
'|', ('country_id.name', 'ilike', m['country'].strip()),
('country_id.code', '=', m['country'].strip()),
]
return first_state_ids + [
state_id
for state_id in self._search(expression.AND([domain2, domain]),
limit=limit, order=order)
if state_id not in first_state_ids
]
] or (
list(self._search(expression.AND([fallback_domain, domain]), limit=limit))
if fallback_domain
else []
)

@api.depends('country_id')
def _compute_display_name(self):
Expand Down
1 change: 1 addition & 0 deletions odoo/addons/base/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from . import test_xmlrpc
from . import test_res_company
from . import test_res_currency
from . import test_res_country
from . import test_res_partner
from . import test_res_partner_bank
from . import test_res_users
Expand Down
53 changes: 53 additions & 0 deletions odoo/addons/base/tests/test_res_country.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from odoo.tests import TransactionCase, tagged


@tagged('-at_install', 'post_install')
class TestResCountryState(TransactionCase):
def test_find_by_name(self):
"""It should be possible to find a state by its display name
"""
glorious_arstotzka = self.env['res.country'].create({
'name': 'Arstotzka',
'code': 'AA',
})
altan = self.env['res.country.state'].create({
'country_id': glorious_arstotzka.id,
'code': 'AL',
'name': 'Altan',
})

for name in [
altan.name,
altan.display_name,
'Altan(AA)',
'Altan ( AA )',
'Altan (Arstotzka)',
'Altan (Arst)', # dubious
]:
with self.subTest(name):
self.assertEqual(
self.env['res.country.state'].name_search(name, operator='='),
[(altan.id, altan.display_name)]
)

# imitates basque provinces
vescillo = self.env['res.country.state'].create({
'country_id': glorious_arstotzka.id,
'code': 'VE',
'name': "Vescillo (Vesilo)",
})
for name in [
vescillo.name,
vescillo.display_name,
"vescillo",
"vesilo",
"vescillo (AA)",
"vesilo (AA)",
"vesilo (Arstotzka)",
]:
with self.subTest(name):
# note operator for more flexible state name matching
self.assertEqual(
self.env['res.country.state'].name_search(name, operator='ilike'),
[(vescillo.id, vescillo.display_name)]
)

0 comments on commit 429a37b

Please sign in to comment.