From 9b568d75c974904c49d421506be04a4783b14ce6 Mon Sep 17 00:00:00 2001
From: Nicolas Dumazet <nicolas.dumazet@nexedi.com>
Date: Tue, 28 Dec 2010 03:01:59 +0000
Subject: [PATCH] add tales expression to a predicate so that one can define
 simple conditions without having to create dozens of unique Python scripts.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@41813 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5PropertySheetLegacy/PropertySheet/Predicate.py | 7 +++++++
 product/ERP5Type/Core/Predicate.py                         | 7 +++++++
 2 files changed, 14 insertions(+)

diff --git a/product/ERP5PropertySheetLegacy/PropertySheet/Predicate.py b/product/ERP5PropertySheetLegacy/PropertySheet/Predicate.py
index 722d1973d9..4d01e1ac0e 100644
--- a/product/ERP5PropertySheetLegacy/PropertySheet/Predicate.py
+++ b/product/ERP5PropertySheetLegacy/PropertySheet/Predicate.py
@@ -61,6 +61,13 @@ class Predicate:
             'type'        : 'lines',
             'default'     : (),
             'mode'        : 'w' },
+        {   'id'          : 'test_tales_expression',
+            'description' : 'A Tales expression to implement a simple ' \
+                            'condition in Python. Runtime context of this ' \
+                            'expression will be the tested document',
+            'type'        : 'string',
+            'default'     : 'python: True',
+            'mode'        : 'w' },
         {   'id'          : 'test_method_id',
             'description' : 'A python method to implement additional tests',
             'type'        : 'lines', # Only a list of method ids is feasable for lines
diff --git a/product/ERP5Type/Core/Predicate.py b/product/ERP5Type/Core/Predicate.py
index 7c558eeee2..30aaacef51 100644
--- a/product/ERP5Type/Core/Predicate.py
+++ b/product/ERP5Type/Core/Predicate.py
@@ -44,6 +44,7 @@ from Products.ERP5Type.Cache import getReadOnlyTransactionCache, enableReadOnlyT
 from Products.ZSQLCatalog.SQLCatalog import SQLQuery
 from Products.ERP5Type.Globals import PersistentMapping
 from Products.ERP5Type.UnrestrictedMethod import UnrestrictedMethod
+from Products.CMFCore.Expression import Expression
 
 class Predicate(XMLObject):
   """
@@ -207,6 +208,12 @@ class Predicate(XMLObject):
 #              '%s after method %s ' % (result, test_method_id))
           if not result:
             return result
+    test_tales_expression = self.getTestTalesExpression()
+    if test_tales_expression not in (None, '', 'python: True'):
+      expression = Expression(test_tales_expression)
+      from Products.ERP5Type.Utils import createExpressionContext
+      # evaluate a tales expression with the tested value as context
+      result = expression(createExpressionContext(context))
     return result
 
   @UnrestrictedMethod
-- 
2.30.9