[FIX] product.template: name_search compatibility with product.product

In the rare places with a m2o field to product.template
(e.g BoMs), it is necessary to be able to search on product
codes, EAN, etc, the way it works on product.product.
This is done by delegating the name_search to product.product
and then returning the corresponding templates.
This has a small penalty of executing name_get() twice,
but is simpler and more robust for future changes.

An alternative would have been to extract the name_search()
method into a mixin and mix it on both product.product and
product.template. However this would be more brittle and
only work as long as the name_search implementation strictly
uses fields that are present in both tables.

OPW 626662
This commit is contained in:
Olivier Dony 2015-02-25 17:46:38 +01:00
parent e700805a43
commit bf31ab6718
1 changed files with 12 additions and 3 deletions

View File

@ -22,8 +22,10 @@
import math import math
import re import re
import time import time
from collections import OrderedDict
from _common import ceiling from _common import ceiling
from openerp import SUPERUSER_ID from openerp import SUPERUSER_ID
from openerp import tools from openerp import tools
from openerp.osv import osv, fields, expression from openerp.osv import osv, fields, expression
@ -797,9 +799,16 @@ class product_template(osv.osv):
pass pass
return super(product_template, self).name_get(cr, user, ids, context) return super(product_template, self).name_get(cr, user, ids, context)
def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
product_product = self.pool['product.product']
results = product_product.name_search(
cr, user, name, args, operator=operator, context=context, limit=limit)
product_ids = [p[0] for p in results]
template_ids = [p.product_tmpl_id.id
for p in product_product.browse(
cr, user, product_ids, context=context)]
uniq_ids = OrderedDict.fromkeys(template_ids).keys()
return self.name_get(cr, user, uniq_ids, context=context)
class product_product(osv.osv): class product_product(osv.osv):
_name = "product.product" _name = "product.product"