hda (OpenERP) has proposed merging 
lp:~openerp-dev/openobject-client/improve_attrs_for_oring_operation into 
lp:~openerp-dev/openobject-client/trunk-dev-client.

Requested reviews:
  OpenERP sa GTK client R&D (openerp-dev-gtk)


https://bugs.launchpad.net/openobject-client/+bug/491817

Added functionality for oring operation in attrs tag domain.
-- 
https://code.launchpad.net/~openerp-dev/openobject-client/improve_attrs_for_oring_operation/+merge/25057
Your team OpenERP sa GTK client R&D is requested to review the proposed merge 
of lp:~openerp-dev/openobject-client/improve_attrs_for_oring_operation into 
lp:~openerp-dev/openobject-client/trunk-dev-client.
=== modified file 'bin/tools/__init__.py'
--- bin/tools/__init__.py	2010-04-22 15:36:19 +0000
+++ bin/tools/__init__.py	2010-05-11 12:42:26 +0000
@@ -68,39 +68,51 @@
             result[attrs.item(i).localName] = eval(attrs.item(i).nodeValue)
     return result
 
-#FIXME use spaces
-def calc_condition(self,model,con):
-    if model and (con[0] in model.mgroup.fields):
-        val = model[con[0]].get(model)
-        if con[1]=="=" or con[1]=="==":
-            if val==con[2]:
-				return True
-        elif con[1]=="!=" or con[1]=="<>":
-			if val!=con[2]:
-				return True
-        elif con[1]=="<":
-            if val<con[2]:
-				return True
-        elif con[1]==">":
-			if val>con[2]:
-				return True
-        elif con[1]=="<=":
-			if val<=con[2]:
-				return True
-        elif con[1]==">=":
-			if val>=con[2]:
-				return True
-        elif con[1].lower()=="in":
-			for cond in con[2]:
-				if val == cond:
-					return True
-        elif con[1].lower()=="not in":
-			for cond in con[2]:
-				if val == cond:
-					return False
-			return True
-        return False
-    
+def calc_condition(self, model, cond):
+    cond_main = cond[:]
+    try:
+        return ConditionExpr(cond).eval(model)
+    except:
+        import common
+        common.error('Wrong attrs Implementation!','You have wrongly specified conditions in attrs %s' %(cond_main,))
+
+class ConditionExpr(object):
+    OPERATORS = {'=': lambda x, y, model: model[x].get(model) == y,
+                 '!=': lambda x, y, model: model[x].get(model) != y,
+                 '<': lambda x, y, model: model[x].get(model) < y,
+                 '>': lambda x, y, model: model[x].get(model) > y,
+                 '<=': lambda x, y, model: model[x].get(model) <= y,
+                 '>=': lambda x, y, model: model[x].get(model) >= y,
+                 'in': lambda x, y, model: model[x].get(model) in y,
+                 'not in': lambda x, y, model: model[x].get(model) not in y}
+
+    OPERAND_MAPPER = {'<>': '!=', '==': '='}
+
+    def __init__(self, condition):
+        self.cond = condition
+
+    def eval(self, context):
+        def evaluate(cond):
+            if isinstance(cond,bool):
+                return cond
+            left, operand, right = cond
+            real_op = self.OPERAND_MAPPER.get(operand.lower(), operand)
+            return self.OPERATORS[real_op](left, right, context)
+
+        def find_index(con):
+            index=-1
+            for a in range(len(con)):
+                if con[a] == '|':
+                    index = a
+            return index
+        ind = find_index(self.cond)
+        while(ind!= -1):
+            result = any((evaluate(self.cond[ind+1]),evaluate(self.cond[ind+2])))
+            self.cond.__delslice__(ind,ind+3)
+            self.cond.__setslice__(ind,ind,[result])
+            ind = find_index(self.cond)
+        return all(evaluate(expr) for expr in self.cond)
+
 def call_log(fun):
     """Debug decorator
        TODO: Add optionnal execution time

=== modified file 'bin/widget/model/field.py'
--- bin/widget/model/field.py	2010-05-05 13:18:56 +0000
+++ bin/widget/model/field.py	2010-05-11 12:42:26 +0000
@@ -137,8 +137,7 @@
                             attrs_changes[k][i]=cond
         for k,v in attrs_changes.items():
             result = True
-            for condition in v:
-                result = result and tools.calc_condition(self,model,condition)
+            result = tools.calc_condition(self, model, v)
             if result:
                self.get_state_attrs(model)[k]=True
 

=== modified file 'bin/widget/view/form.py'
--- bin/widget/view/form.py	2010-04-13 11:34:18 +0000
+++ bin/widget/view/form.py	2010-05-11 12:42:26 +0000
@@ -433,9 +433,7 @@
                             cond=v[i][0],v[i][1],v[i][2][0]
                             attrs_changes[k][i]=cond
         for k,v in attrs_changes.items():
-            result = True
-            for condition in v:
-                result = result and tools.calc_condition(self,model,condition)
+            result = tools.calc_condition(self, model, v)
             if result:
                 if k=='invisible':
                     obj.hide()

=== modified file 'bin/widget/view/form_gtk/parser.py'
--- bin/widget/view/form_gtk/parser.py	2010-05-05 13:18:56 +0000
+++ bin/widget/view/form_gtk/parser.py	2010-05-11 12:42:26 +0000
@@ -150,8 +150,7 @@
             self.widget.grab_focus()
         for k,v in attrs_changes.items():
             result = True
-            for condition in v:
-                result = result and tools.calc_condition(self,model,condition)
+            result = result and tools.calc_condition(self, model, v)
             if result:
                 if k=='invisible':
                     self.widget.hide()

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to