Anup (Open ERP) has proposed merging 
lp:~openerp-commiter/openobject-client/trunk-ach-branch into 
lp:openobject-client.

Requested reviews:
  OpenERP sa GTK client R&D (openerp-dev-gtk)
Related bugs:
  #491817 support for 'or' in attrs keyword
  https://bugs.launchpad.net/bugs/491817


Hello,

    I have made the suggested changes. OPERATORS dict is replaced by operator 
module functions. Would you please check and give your views.

Thanks.
-- 
https://code.launchpad.net/~openerp-commiter/openobject-client/trunk-ach-branch/+merge/25063
Your team OpenERP sa GTK client R&D is requested to review the proposed merge 
of lp:~openerp-commiter/openobject-client/trunk-ach-branch into 
lp:openobject-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 13:42:27 +0000
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
@@ -15,13 +15,14 @@
 #    GNU Affero General Public License for more details.
 #
 #    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
 import time
 import datetime
 import os
+import operator
 if os.name == 'nt':
     import win32
 
@@ -69,38 +70,57 @@
     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):
+    def get_result(self,x,op,y,model):
+        if op=='=':
+            return operator.eq(model[x].get(model),y)
+        elif op=='!=' or op=='<>':
+            return operator.ne(model[x].get(model),y)
+        elif op=='<':
+            return operator.lt(model[x].get(model),y)
+        elif op=='>':
+            return operator.gt(model[x].get(model),y)
+        elif op=='<=':
+            return operator.le(model[x].get(model),y)
+        elif op=='>=':
+            return operator.lt(model[x].get(model),y)
+        elif op=='in':
+            return operator.contains(model[x].get(model),y)
+        elif op=='not in':
+            return operator.not_(operator.contains(model[x].get(model),y))
+
+    def __init__(self, condition):
+        self.cond = condition
+
+    def eval(self, context):
+        def evaluate(cond):
+            if isinstance(cond,bool):
+                return cond
+            left, opr, right = cond
+            return self.get_result(left,opr,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 13:42:27 +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 13:42:27 +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 13:42:27 +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