← Back to team overview

openerp-community team mailing list archive

lp:~camptocamp/openerp-product-attributes/6.1-search-all-prodlot-attributes-lep into lp:openerp-product-attributes/6.1

 

Leonardo Pistone @ camptocamp has proposed merging lp:~camptocamp/openerp-product-attributes/6.1-search-all-prodlot-attributes-lep into lp:openerp-product-attributes/6.1.

Requested reviews:
  Product Core Editors (product-core-editors)

For more details, see:
https://code.launchpad.net/~camptocamp/openerp-product-attributes/6.1-search-all-prodlot-attributes-lep/+merge/199335

This allows to search all attributes of the production lot, both serialized and not.
-- 
https://code.launchpad.net/~camptocamp/openerp-product-attributes/6.1-search-all-prodlot-attributes-lep/+merge/199335
Your team OpenERP Community is subscribed to branch lp:openerp-product-attributes/6.1.
=== modified file 'production_lot_custom_attributes/lot.py'
--- production_lot_custom_attributes/lot.py	2013-12-11 10:53:46 +0000
+++ production_lot_custom_attributes/lot.py	2013-12-17 18:10:09 +0000
@@ -26,6 +26,7 @@
 from tools.translate import _
 from lxml import etree
 import re
+import itertools
 
 
 class stock_production_lot(osv.Model):
@@ -38,17 +39,14 @@
         to search into all attributes. The ORM will take care of security
         afterwards, so it' OK to use SQL here.
 
-        In the future, we could consider storing attributes as native PostgreSQL
-        hstore or JSON instead of strings, and substitute this rough regexp
-        search with native PostgreSQL goodness.
+        In the future, we could consider storing attributes as native
+        PostgreSQL hstore or JSON instead of strings, and substitute this rough
+        regexp search with native PostgreSQL goodness.
 
         """
 
-        def expand_arg(arg):
-            """Takes a single argument of the domain, and when possible expands
-            it to a trivial domain ('in', 'in', list)
-
-            """
+        def expand_serialized(arg):
+            """Expand the args in a trivial domain ('id', 'in', ids)"""
             if isinstance(arg, tuple) and arg[0] == name:
                 if arg[1] == 'like':
                     operator = '~'
@@ -64,18 +62,38 @@
                         select id
                         from {0}
                         where x_custom_json_attrs {1} %s;
-                    """.format(
-                        self._table,
-                        operator
-                    ),
-                    (ur'.*: "[^"]*%s' % re.escape(arg[2]) ,)
+                    """.format(self._table, operator),
+                    (ur'.*: "[^"]*%s' % re.escape(arg[2]),)
                 )
                 sql_ids = [line[0] for line in cr.fetchall()]
-                return ('id', 'in', sql_ids)
+                return [('id', 'in', sql_ids)]
             else:
-                return arg
-
-        return [expand_arg(arg) for arg in args]
+                return [arg]
+
+        def expand_not_serialized(arg):
+            """Expand the args in a domain like
+            ['|', ('real_field_1', operator, string),
+            ('real_field_2', operator, string)"""
+            if arg[1] not in ('like', 'ilike'):
+                raise osv.except_osv(
+                    _('Not Implemented!'),
+                    _('Search not supported for this field'))
+
+            attribute_pool = self.pool.get('attribute.attribute')
+
+            field_ids = attribute_pool.search(cr, uid, [
+                ('model_id.model', '=', self._name),
+                ('serialized', '=', False)
+            ], context=context)
+            fields = attribute_pool.browse(cr, uid, field_ids, context=context)
+            terms = [(f.name, arg[1], arg[2]) for f in fields]
+            return ['|'] * (len(terms) - 1) + terms
+
+        def expand(arg):
+            """Expand each argument in a domain we can pass upstream"""
+            return ['|'] + expand_serialized(arg) + expand_not_serialized(arg)
+
+        return itertools.chain(*[expand(arg) for arg in args])
 
     _columns = {
         'attribute_set_id': fields.many2one('attribute.set', 'Attribute Set'),


Follow ups