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/product | |
| 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/product')
| -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 |
4 files changed, 14 insertions, 16 deletions
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( |
