diff options
| author | Raphael Collet <rco@odoo.com> | 2020-06-17 12:18:27 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2020-08-18 13:02:41 +0000 |
| commit | 1f48130d2bd055835ee428a21922331d126285a3 (patch) | |
| tree | 594a6dd2999a290712839dfe92df47d0f7ed42b1 /addons | |
| parent | 89963e0eddbf36d687799acb6ed1e8486cc178e7 (diff) | |
[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.
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/account/models/account_account.py | 6 | ||||
| -rw-r--r-- | addons/account/models/account_journal.py | 3 | ||||
| -rw-r--r-- | addons/account/models/account_tax.py | 3 | ||||
| -rw-r--r-- | addons/analytic/models/analytic_account.py | 3 | ||||
| -rw-r--r-- | addons/fleet/models/fleet_vehicle.py | 3 | ||||
| -rw-r--r-- | addons/l10n_latam_invoice_document/models/l10n_latam_document_type.py | 3 | ||||
| -rw-r--r-- | addons/maintenance/models/maintenance.py | 2 | ||||
| -rw-r--r-- | addons/product/models/product.py | 12 | ||||
| -rw-r--r-- | addons/product/models/product_attribute.py | 3 | ||||
| -rw-r--r-- | addons/product/models/product_pricelist.py | 2 | ||||
| -rw-r--r-- | addons/product/models/product_template.py | 13 | ||||
| -rw-r--r-- | addons/purchase/models/purchase.py | 3 | ||||
| -rw-r--r-- | addons/purchase_stock/tests/test_product_template.py | 6 | ||||
| -rw-r--r-- | addons/sale/models/sale.py | 3 | ||||
| -rw-r--r-- | addons/stock/models/stock_location.py | 3 | ||||
| -rw-r--r-- | addons/stock/models/stock_picking.py | 3 |
16 files changed, 29 insertions, 42 deletions
diff --git a/addons/account/models/account_account.py b/addons/account/models/account_account.py index 6dc128dc316..d9b5fa936b1 100644 --- a/addons/account/models/account_account.py +++ b/addons/account/models/account_account.py @@ -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): diff --git a/addons/account/models/account_journal.py b/addons/account/models/account_journal.py index 9be7d9c68b5..f9e5ef905fa 100644 --- a/addons/account/models/account_journal.py +++ b/addons/account/models/account_journal.py @@ -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): diff --git a/addons/account/models/account_tax.py b/addons/account/models/account_tax.py index 6d04db8b3c6..56b65e5d8ad 100644 --- a/addons/account/models/account_tax.py +++ b/addons/account/models/account_tax.py @@ -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): diff --git a/addons/analytic/models/analytic_account.py b/addons/analytic/models/analytic_account.py index 5c99a9dfcdf..9bbfe4a254c 100644 --- a/addons/analytic/models/analytic_account.py +++ b/addons/analytic/models/analytic_account.py @@ -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): diff --git a/addons/fleet/models/fleet_vehicle.py b/addons/fleet/models/fleet_vehicle.py index 87519bdafd0..a74add5e386 100644 --- a/addons/fleet/models/fleet_vehicle.py +++ b/addons/fleet/models/fleet_vehicle.py @@ -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 """ diff --git a/addons/l10n_latam_invoice_document/models/l10n_latam_document_type.py b/addons/l10n_latam_invoice_document/models/l10n_latam_document_type.py index 40b9f55ee98..3eb6bd04e0b 100644 --- a/addons/l10n_latam_invoice_document/models/l10n_latam_document_type.py +++ b/addons/l10n_latam_invoice_document/models/l10n_latam_document_type.py @@ -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 diff --git a/addons/maintenance/models/maintenance.py b/addons/maintenance/models/maintenance.py index 8224cc52d93..fcb87179010 100644 --- a/addons/maintenance/models/maintenance.py +++ b/addons/maintenance/models/maintenance.py @@ -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', diff --git a/addons/product/models/product.py b/addons/product/models/product.py index 2349ac40194..7e192b84ee8 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -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): diff --git a/addons/product/models/product_attribute.py b/addons/product/models/product_attribute.py index 1f879fbca7b..8c252c22448 100644 --- a/addons/product/models/product_attribute.py +++ b/addons/product/models/product_attribute.py @@ -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): diff --git a/addons/product/models/product_pricelist.py b/addons/product/models/product_pricelist.py index d1ef0ec714c..42378e4e00c 100644 --- a/addons/product/models/product_pricelist.py +++ b/addons/product/models/product_pricelist.py @@ -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): diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 2039468696a..50685ef014a 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -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( diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 09c2697cd2e..d4f23bbc860 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -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): diff --git a/addons/purchase_stock/tests/test_product_template.py b/addons/purchase_stock/tests/test_product_template.py index 2cb82886c0d..0692ac02033 100644 --- a/addons/purchase_stock/tests/test_product_template.py +++ b/addons/purchase_stock/tests/test_product_template.py @@ -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") diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index b9ad7c8b796..1bdc685b134 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -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): diff --git a/addons/stock/models/stock_location.py b/addons/stock/models/stock_location.py index 2d82f3e84d5..cd2b7643aa2 100644 --- a/addons/stock/models/stock_location.py +++ b/addons/stock/models/stock_location.py @@ -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.''' diff --git a/addons/stock/models/stock_picking.py b/addons/stock/models/stock_picking.py index 95b77f2997c..9c266ce95c3 100644 --- a/addons/stock/models/stock_picking.py +++ b/addons/stock/models/stock_picking.py @@ -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): |
