[REF] core: _name_search() now returns a list of ids

This provides a better API to search for records by name without the
formatting part (`display_name`).

This also simplifies all the overridings of `_name_search` that no
longer need to call `name_get()`.  The call to `name_get()` is done in
method `name_search` in a generic way.
This commit is contained in:
Raphael Collet
2020-08-18 13:02:41 +00:00
parent 89963e0edd
commit 1f48130d2b
25 changed files with 67 additions and 91 deletions
+2 -4
View File
@@ -331,8 +331,7 @@ class AccountAccount(models.Model):
domain = ['|', ('code', '=ilike', name.split(' ')[0] + '%'), ('name', operator, name)]
if operator in expression.NEGATIVE_TERM_OPERATORS:
domain = ['&', '!'] + domain[1:]
account_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(account_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
@api.onchange('user_type_id')
def _onchange_user_type_id(self):
@@ -524,8 +523,7 @@ class AccountGroup(models.Model):
else:
criteria_operator = ['|'] if operator not in expression.NEGATIVE_TERM_OPERATORS else ['&', '!']
domain = criteria_operator + [('code_prefix_start', '=ilike', name + '%'), ('name', operator, name)]
group_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(group_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
@api.constrains('code_prefix_start', 'code_prefix_end')
def _constraint_prefix_overlap(self):
+1 -2
View File
@@ -540,8 +540,7 @@ class AccountJournal(models.Model):
else:
connector = '&' if operator in expression.NEGATIVE_TERM_OPERATORS else '|'
domain = [connector, ('code', operator, name), ('name', operator, name)]
journal_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(journal_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
@api.depends('inbound_payment_method_ids', 'outbound_payment_method_ids')
def _methods_compute(self):
+1 -2
View File
@@ -212,8 +212,7 @@ class AccountTax(models.Model):
else:
connector = '&' if operator in expression.NEGATIVE_TERM_OPERATORS else '|'
domain = [connector, ('description', operator, name), ('name', operator, name)]
tax_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(tax_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
@api.model
def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
+1 -2
View File
@@ -165,8 +165,7 @@ class AccountAnalyticAccount(models.Model):
# we have to cut the search in two searches ... https://github.com/odoo/odoo/issues/25175
partner_ids = self.env['res.partner']._search([('name', operator, name)], limit=limit, access_rights_uid=name_get_uid)
domain = ['|', '|', ('code', operator, name), ('name', operator, name), ('partner_id', 'in', partner_ids)]
analytic_account_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(analytic_account_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
class AccountAnalyticLine(models.Model):
+1 -2
View File
@@ -273,8 +273,7 @@ class FleetVehicle(models.Model):
domain = []
else:
domain = ['|', ('name', operator, name), ('driver_id.name', operator, name)]
rec = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(rec).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
def return_action_to_open(self):
""" This opens the xml view specified in xml_id for the current vehicle """
@@ -50,8 +50,7 @@ class L10nLatamDocumentType(models.Model):
domain = []
else:
domain = ['|', ('name', 'ilike', name), ('code', 'ilike', name)]
ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return self.browse(ids).name_get()
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
def _filter_taxes_included(self, taxes):
""" This method is to be inherited by different localizations and must return filter the given taxes recordset
+1 -1
View File
@@ -107,7 +107,7 @@ class MaintenanceEquipment(models.Model):
equipment_ids = self._search([('name', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid)
if not equipment_ids:
equipment_ids = self._search([('name', operator, name)] + args, limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(equipment_ids).with_user(name_get_uid))
return equipment_ids
name = fields.Char('Equipment Name', required=True, translate=True)
company_id = fields.Many2one('res.company', string='Company',
+6 -6
View File
@@ -507,15 +507,15 @@ class ProductProduct(models.Model):
positive_operators = ['=', 'ilike', '=ilike', 'like', '=like']
product_ids = []
if operator in positive_operators:
product_ids = self._search([('default_code', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid)
product_ids = list(self._search([('default_code', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid))
if not product_ids:
product_ids = self._search([('barcode', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid)
product_ids = list(self._search([('barcode', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid))
if not product_ids and operator not in expression.NEGATIVE_TERM_OPERATORS:
# Do not merge the 2 next lines into one single search, SQL search performance would be abysmal
# on a database with thousands of matching products, due to the huge merge+unique needed for the
# OR operator (and given the fact that the 'name' lookup results come from the ir.translation table
# Performing a quick memory merge of ids in Python will give much better performance
product_ids = self._search(args + [('default_code', operator, name)], limit=limit)
product_ids = list(self._search(args + [('default_code', operator, name)], limit=limit))
if not limit or len(product_ids) < limit:
# we may underrun the limit because of dupes in the results, that's fine
limit2 = (limit - len(product_ids)) if limit else False
@@ -527,12 +527,12 @@ class ProductProduct(models.Model):
['&', ('default_code', '=', False), ('name', operator, name)],
])
domain = expression.AND([args, domain])
product_ids = self._search(domain, limit=limit, access_rights_uid=name_get_uid)
product_ids = list(self._search(domain, limit=limit, access_rights_uid=name_get_uid))
if not product_ids and operator in positive_operators:
ptrn = re.compile('(\[(.*?)\])')
res = ptrn.search(name)
if res:
product_ids = self._search([('default_code', '=', res.group(2))] + args, limit=limit, access_rights_uid=name_get_uid)
product_ids = list(self._search([('default_code', '=', res.group(2))] + args, limit=limit, access_rights_uid=name_get_uid))
# still no results, partner in context: search on supplier info as last hope to find something
if not product_ids and self._context.get('partner_id'):
suppliers_ids = self.env['product.supplierinfo']._search([
@@ -544,7 +544,7 @@ class ProductProduct(models.Model):
product_ids = self._search([('product_tmpl_id.seller_ids', 'in', suppliers_ids)], limit=limit, access_rights_uid=name_get_uid)
else:
product_ids = self._search(args, limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(product_ids).with_user(name_get_uid))
return product_ids
@api.model
def view_header_get(self, view_id, view_type):
+1 -2
View File
@@ -371,8 +371,7 @@ class ProductTemplateAttributeLine(models.Model):
if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'):
args = args or []
domain = ['|', ('attribute_id', operator, name), ('value_ids', operator, name)]
attribute_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(attribute_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return super(ProductTemplateAttributeLine, self)._name_search(name=name, args=args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def _without_no_variant_attributes(self):
+1 -1
View File
@@ -73,7 +73,7 @@ class Pricelist(models.Model):
# regular search() to apply ACLs - may limit results below limit in some cases
pricelist_ids = self._search([('id', 'in', ids)], limit=limit, access_rights_uid=name_get_uid)
if pricelist_ids:
return models.lazy_name_get(self.browse(pricelist_ids).with_user(name_get_uid))
return pricelist_ids
return super(Pricelist, self)._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def _compute_price_rule_multi(self, products_qty_partner, date=False, uom_id=False):
+6 -7
View File
@@ -468,8 +468,8 @@ class ProductTemplate(models.Model):
while True:
domain = templates and [('product_tmpl_id', 'not in', templates.ids)] or []
args = args if args is not None else []
products_ns = Product._name_search(name, args+domain, operator=operator, name_get_uid=name_get_uid)
products = Product.browse([x[0] for x in products_ns])
products_ids = Product._name_search(name, args+domain, operator=operator, name_get_uid=name_get_uid)
products = Product.browse(products_ids)
new_templates = products.mapped('product_tmpl_id')
if new_templates & templates:
"""Product._name_search can bypass the domain we passed (search on supplier info).
@@ -479,8 +479,8 @@ class ProductTemplate(models.Model):
current_round_templates = self.browse([])
if not products:
domain_template = args + domain_no_variant + (templates and [('id', 'not in', templates.ids)] or [])
template_ns = super(ProductTemplate, self)._name_search(name=name, args=domain_template, operator=operator, limit=limit, name_get_uid=name_get_uid)
current_round_templates |= self.browse([ns[0] for ns in template_ns])
template_ids = super(ProductTemplate, self)._name_search(name=name, args=domain_template, operator=operator, limit=limit, name_get_uid=name_get_uid)
current_round_templates |= self.browse(template_ids)
templates |= current_round_templates
if (not products and not current_round_templates) or (limit and (len(templates) > limit)):
break
@@ -491,13 +491,12 @@ class ProductTemplate(models.Model):
# FIXME awa: this is really not performant at all but after discussing with the team
# we don't see another way to do it
if not limit or len(searched_ids) < limit:
searched_ids |= set([template_id[0] for template_id in
super(ProductTemplate, self)._name_search(
searched_ids |= set(super(ProductTemplate, self)._name_search(
name,
args=args,
operator=operator,
limit=limit,
name_get_uid=name_get_uid)])
name_get_uid=name_get_uid))
# re-apply product.template order + name_get
return super(ProductTemplate, self)._name_search(
+1 -2
View File
@@ -163,8 +163,7 @@ class PurchaseOrder(models.Model):
domain = []
if name:
domain = ['|', ('name', operator, name), ('partner_ref', operator, name)]
purchase_order_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(purchase_order_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
@api.depends('date_order', 'currency_id', 'company_id', 'company_id.currency_id')
def _compute_currency_rate(self):
@@ -21,6 +21,6 @@ class TestProductTemplate(TransactionCase):
'seller_ids': [seller.id],
'purchase_ok': True,
})
ns = self.env['product.template'].with_context(partner_id=partner.id)._name_search('VOB2', [['purchase_ok', '=', True]])
self.assertEqual(len(ns), 1, "_name_search should have 1 item")
self.assertEqual(ns[0][1]._value, '[VOB2A] Rubber Duck', "_name_search should return the expected result")
ns = self.env['product.template'].with_context(partner_id=partner.id).name_search('VOB2', [['purchase_ok', '=', True]])
self.assertEqual(len(ns), 1, "name_search should have 1 item")
self.assertEqual(ns[0][1], '[VOB2A] Rubber Duck', "name_search should return the expected result")
+1 -2
View File
@@ -521,8 +521,7 @@ class SaleOrder(models.Model):
args or [],
['|', ('name', operator, name), ('partner_id.name', operator, name)]
])
order_ids = self._search(domain, limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(order_ids).with_user(name_get_uid))
return self._search(domain, limit=limit, access_rights_uid=name_get_uid)
return super(SaleOrder, self)._name_search(name, args=args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def _prepare_invoice(self):
+1 -2
View File
@@ -130,8 +130,7 @@ class Location(models.Model):
domain = [('barcode', operator, name), ('complete_name', operator, name)]
else:
domain = ['|', ('barcode', operator, name), ('complete_name', operator, name)]
location_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(location_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
def _get_putaway_strategy(self, product):
''' Returns the location where the product has to be put, if any compliant putaway strategy is found. Otherwise returns None.'''
+1 -2
View File
@@ -161,8 +161,7 @@ class PickingType(models.Model):
domain = []
if name:
domain = ['|', ('name', operator, name), ('warehouse_id.name', operator, name)]
picking_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(picking_ids).with_user(name_get_uid))
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
@api.onchange('code')
def _onchange_picking_code(self):
+1 -2
View File
@@ -760,8 +760,7 @@ class IrActionsTodo(models.Model):
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if name:
action_ids = self._search(expression.AND([[('action_id', operator, name)], args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(action_ids).with_user(name_get_uid))
return self._search(expression.AND([[('action_id', operator, name)], args]), limit=limit, access_rights_uid=name_get_uid)
return super(IrActionsTodo, self)._name_search(name, args=args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def action_launch(self):
+1 -2
View File
@@ -257,8 +257,7 @@ class IrModel(models.Model):
if args is None:
args = []
domain = args + ['|', ('model', operator, name), ('name', operator, name)]
model_ids = self._search(domain, limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(model_ids).with_user(name_get_uid))
return self._search(domain, limit=limit, access_rights_uid=name_get_uid)
def _drop_table(self):
for model in self:
+1 -2
View File
@@ -136,8 +136,7 @@ class ViewCustom(models.Model):
@api.model
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
if name:
view_ids = self._search([('user_id', operator, name)] + (args or []), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(view_ids).with_user(name_get_uid))
return self._search([('user_id', operator, name)] + (args or []), limit=limit, access_rights_uid=name_get_uid)
return super(ViewCustom, self)._name_search(name, args=args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def _auto_init(self):
+1 -2
View File
@@ -48,8 +48,7 @@ class Bank(models.Model):
domain = ['|', ('bic', '=ilike', name + '%'), ('name', operator, name)]
if operator in expression.NEGATIVE_TERM_OPERATORS:
domain = ['&'] + domain
bank_ids = self._search(domain + args, limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(bank_ids).with_user(name_get_uid))
return self._search(domain + args, limit=limit, access_rights_uid=name_get_uid)
@api.onchange('country')
def _onchange_country_id(self):
+20 -21
View File
@@ -10,24 +10,6 @@ from odoo.tools.translate import _
_logger = logging.getLogger(__name__)
@api.model
def location_name_search(self, name='', args=None, operator='ilike', limit=100):
if args is None:
args = []
records = self.browse()
if len(name) == 2:
records = self.search([('code', 'ilike', name)] + args, limit=limit)
search_domain = [('name', operator, name)]
if records:
search_domain.append(('id', 'not in', records.ids))
records += self.search(search_domain + args, limit=limit)
# the field 'display_name' calls name_get() to get its value
return models.lazy_name_get(records)
class Country(models.Model):
_name = 'res.country'
_description = 'Country'
@@ -77,7 +59,20 @@ class Country(models.Model):
'The code of the country must be unique !')
]
name_search = location_name_search
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
if args is None:
args = []
ids = []
if len(name) == 2:
ids = list(self._search([('code', 'ilike', name)] + args, limit=limit))
search_domain = [('name', operator, name)]
if ids:
search_domain.append(('id', 'not in', ids))
ids += list(self._search(search_domain + args, limit=limit))
return ids
@api.model_create_multi
def create(self, vals_list):
@@ -133,8 +128,12 @@ class CountryState(models.Model):
domain = [('name', operator, name)]
first_state_ids = self._search(expression.AND([first_domain, args]), limit=limit, access_rights_uid=name_get_uid) if first_domain else []
state_ids = first_state_ids + [state_id for state_id in self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) if not state_id in first_state_ids]
return models.lazy_name_get(self.browse(state_ids).with_user(name_get_uid))
return list(first_state_ids) + [
state_id
for state_id in self._search(expression.AND([domain, args]),
limit=limit, access_rights_uid=name_get_uid)
if state_id not in first_state_ids
]
def name_get(self):
result = []
+2 -7
View File
@@ -117,8 +117,7 @@ class PartnerCategory(models.Model):
# Be sure name_search is symetric to name_get
name = name.split(' / ')[-1]
args = [('name', operator, name)] + args
partner_category_ids = self._search(args, limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(partner_category_ids).with_user(name_get_uid))
return self._search(args, limit=limit, access_rights_uid=name_get_uid)
class PartnerTitle(models.Model):
@@ -797,12 +796,8 @@ class Partner(models.Model):
query += ' limit %s'
where_clause_params.append(limit)
self.env.cr.execute(query, where_clause_params)
partner_ids = [row[0] for row in self.env.cr.fetchall()]
return [row[0] for row in self.env.cr.fetchall()]
if partner_ids:
return models.lazy_name_get(self.browse(partner_ids))
else:
return []
return super(Partner, self)._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)
@api.model
+1 -1
View File
@@ -622,7 +622,7 @@ class Users(models.Model):
user_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
if not user_ids:
user_ids = self._search(expression.AND([[('name', operator, name)], args]), limit=limit, access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(user_ids).with_user(name_get_uid))
return user_ids
def copy(self, default=None):
self.ensure_one()
+3 -6
View File
@@ -50,8 +50,7 @@ for name, field in MODELS:
@api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
if isinstance(name, str) and name.split(':')[0] == self._name:
record_ids = self._search([('value', operator, int(name.split(':')[1]))], access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(record_ids).with_user(name_get_uid))
return self._search([('value', operator, int(name.split(':')[1]))], access_rights_uid=name_get_uid)
else:
return []
@@ -72,8 +71,7 @@ class One2ManyChild(models.Model):
@api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
if isinstance(name, str) and name.split(':')[0] == self._name:
record_ids = self._search([('value', operator, int(name.split(':')[1]))], access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(record_ids).with_user(name_get_uid))
return self._search([('value', operator, int(name.split(':')[1]))], access_rights_uid=name_get_uid)
else:
return []
@@ -130,8 +128,7 @@ class Many2ManyChild(models.Model):
@api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
if isinstance(name, str) and name.split(':')[0] == self._name:
record_ids = self._search([('value', operator, int(name.split(':')[1]))], access_rights_uid=name_get_uid)
return models.lazy_name_get(self.browse(record_ids).with_user(name_get_uid))
return self._search([('value', operator, int(name.split(':')[1]))], access_rights_uid=name_get_uid)
else:
return []
+8 -6
View File
@@ -1753,21 +1753,23 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})):
:rtype: list
:return: list of pairs ``(id, text_repr)`` for all matching records.
"""
return self._name_search(name, args, operator, limit=limit)
ids = self._name_search(name, args, operator, limit=limit)
return self.browse(ids).sudo().name_get()
@api.model
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
# private implementation of name_search, allows passing a dedicated user
# for the name_get part to solve some access rights issues
""" _name_search(name='', args=None, operator='ilike', limit=100, name_get_uid=None) -> ids
Private implementation of name_search, allows passing a dedicated user
for the name_get part to solve some access rights issues.
"""
args = list(args or [])
# optimize out the default criterion of ``ilike ''`` that matches everything
if not self._rec_name:
_logger.warning("Cannot execute name_search, no _rec_name defined on %s", self._name)
elif not (name == '' and operator == 'ilike'):
args += [(self._rec_name, operator, name)]
ids = self._search(args, limit=limit, access_rights_uid=name_get_uid)
recs = self.browse(ids)
return lazy_name_get(recs.with_user(name_get_uid))
return self._search(args, limit=limit, access_rights_uid=name_get_uid)
@api.model
def _add_missing_default_values(self, values):