← Back to team overview

openerp-community-reviewer team mailing list archive

lp:~yann-papouin/ocb-addons/6.1-bug-1267845-product-and-category-search-improved-usability into lp:ocb-addons/6.1

 

Yann Papouin has proposed merging lp:~yann-papouin/ocb-addons/6.1-bug-1267845-product-and-category-search-improved-usability into lp:ocb-addons/6.1.

Requested reviews:
  OpenERP Community Backports Team (ocb)
Related bugs:
  Bug #1267845 in OpenERP Community Backports (Addons): "[6.1][7.0][trunk] Search for product and product category is not made for a daily use"
  https://bugs.launchpad.net/ocb-addons/+bug/1267845

For more details, see:
https://code.launchpad.net/~yann-papouin/ocb-addons/6.1-bug-1267845-product-and-category-search-improved-usability/+merge/201192
-- 
https://code.launchpad.net/~yann-papouin/ocb-addons/6.1-bug-1267845-product-and-category-search-improved-usability/+merge/201192
Your team OpenERP Community Backports Team is requested to review the proposed merge of lp:~yann-papouin/ocb-addons/6.1-bug-1267845-product-and-category-search-improved-usability into lp:ocb-addons/6.1.
=== modified file 'product/product.py'
--- product/product.py	2013-11-20 01:17:39 +0000
+++ product/product.py	2014-01-10 14:42:28 +0000
@@ -193,6 +193,8 @@
 #----------------------------------------------------------
 class product_category(osv.osv):
 
+    product_category_separator = ' / '
+    
     def name_get(self, cr, uid, ids, context=None):
         if not len(ids):
             return []
@@ -201,9 +203,44 @@
         for record in reads:
             name = record['name']
             if record['parent_id']:
-                name = record['parent_id'][1]+' / '+name
+                name = record['parent_id'][1]+self.product_category_separator+name
             res.append((record['id'], name))
         return res
+    
+
+    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
+        if not args:
+            args = []
+        if name:
+            clean_name = False
+            category_args = []
+            if self.product_category_separator in name:
+                # Get a list of each category names and remove extra spaces 
+                category_names = [x for x in name.split(self.product_category_separator) if x.strip()]
+                # Use only latest value and remove interferences
+                clean_name = category_names[-1].rstrip(self.product_category_separator.strip()).strip()
+                # Extract parent name for better accuracy
+                if len(category_names) > 1:
+                    parent_name = category_names[-2].rstrip(self.product_category_separator.strip()).strip() 
+                    category_args = [('parent_id.name', '=', parent_name)]  
+                            
+            ids = self.search(cr, user, [('name', operator, clean_name or name)]+category_args+args, limit=limit, context=context)
+            # Get remaining results (using clean name if exists) 
+            if not limit or len(ids) < limit:
+                classic_ids = self.search(cr, user, [('name', operator, clean_name or name)], limit=(limit-len(ids) if limit else limit), context=context)    
+                # Add classic results and avoid duplicates
+                if classic_ids:
+                    ids = ids + list(set(classic_ids)-set(ids))
+             
+        else:
+            ids = self.search(cr, user, args, limit=limit, context=context)
+            
+        # Get named values
+        result = self.name_get(cr, user, ids, context=context)
+        # Re-order data by search priority since read lost our data
+        if name:
+            result = sorted(result, lambda x,y: cmp(ids.index(x[0]), ids.index(y[0])))
+        return result
 
     def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
         res = self.name_get(cr, uid, ids, context=context)
@@ -620,6 +657,8 @@
                 res = ptrn.search(name)
                 if res:
                     ids = self.search(cr, user, [('default_code','=', res.group(2))] + args, limit=limit, context=context)
+                elif '[' in name or ']' in name: 
+                    ids = self.search(cr, user, [('default_code', operator, name.lstrip('[').rstrip(']').strip())] + args, limit=limit, context=context)
         else:
             ids = self.search(cr, user, args, limit=limit, context=context)
         result = self.name_get(cr, user, ids, context=context)


Follow ups