Skip to content

Commit

Permalink
Merge pull request OCA#104 from cyrilgdn/fix-search-e_nr
Browse files Browse the repository at this point in the history
Allow product search by default_code and e_nr
  • Loading branch information
yvaucher committed Apr 12, 2016
2 parents f7ddad9 + e4b81bb commit df59f6d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
42 changes: 27 additions & 15 deletions specific-parts/specific-addons/specific_product/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,34 @@ def _get_transit_qty(self):

@api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
""" Allow to search by E-Nr """
""" Allow to search by E-Nr or internal ref """
args = args or []

result = super(ProductProduct, self).name_search(
name, args=args, operator=operator, limit=limit)

if limit is not None:
limit -= len(result)

if limit is None or limit > 1:
domain = [('e_nr', '=ilike', name + '%')]
if operator in expression.NEGATIVE_TERM_OPERATORS:
domain = ['&'] + domain
products = self.search(domain + args, limit=limit)
result.extend(products.name_get())
return result
filter_known = []

if name:
products = self.browse()
for search_field in ['e_nr', 'default_code']:
if limit is None or limit > 1:
domain = [(search_field, '=ilike', name + '%')]
if operator in expression.NEGATIVE_TERM_OPERATORS:
domain = ['&'] + domain
domain = filter_known + domain
recs = self.search(domain + args, limit=limit)
if limit is not None:
limit -= len(recs)
products |= recs
if products:
filter_known = [('id', 'not in', products.ids)]

name_result = super(ProductProduct, self).name_search(
name, args=filter_known + args, operator=operator, limit=limit)

if products:
result = products.name_get()
name_result = name_result
result.extend(name_result)
return result
return name_result

@api.multi
def action_transit_move(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ def setUp(self):

def test_name_search(self):

self.product_model.create({'name': 'Unittest P1',
'e_nr': 'fake_enr1'})
self.product_model.create({'name': 'P2', 'e_nr': 'fake_enr2'})

self.product_model.create({
'name': 'Unittest P1',
'e_nr': 'fake_enr1_unittest',
'default_code': 'default_code_1'})
self.product_model.create({
'name': 'P2',
'e_nr': 'fake_enr2',
'default_code': 'default_code_2'})
self.assertEqual(1, len(self.product_model.name_search('unittest')))
self.assertEqual(2, len(self.product_model.name_search('fake')))

self.assertEqual(1,
len(self.product_model.name_search('default_code_1')))
self.assertEqual(2,
len(self.product_model.name_search('default_code')))
# Only one result should show up by record
self.assertEqual(1, len(self.product_model.name_search('unittest')))
# Test with limit=None to highlight name_search little bug
self.product_model.name_search('unittest_enr', limit=None)

Expand Down
16 changes: 16 additions & 0 deletions specific-parts/specific-addons/specific_product/views/product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@
</field>
</record>

<!-- Add E-nr in search view -->
<record id="product_template_search_view" model="ir.ui.view">
<field name="name">product.template.search</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_search_view"/>
<field name="arch" type="xml">
<field name="name" position="attributes">
<attribute name="filter_domain">
['|', '|',('default_code','ilike',self),('name','ilike',self), ('e_nr','ilike',self)]
</attribute>
</field>
</field>
</record>



</odoo>

0 comments on commit df59f6d

Please sign in to comment.