[Erp5-report] r40847 arnaud.fontaine - in /erp5/trunk/products/ERP5Type: Core/ PropertyShee...

nobody at svn.erp5.org nobody at svn.erp5.org
Mon Nov 29 10:55:54 CET 2010


Author: arnaud.fontaine
Date: Mon Nov 29 10:55:52 2010
New Revision: 40847

URL: http://svn.erp5.org?rev=40847&view=rev
Log:
Add TALES Constraint for ZODB Property Sheets and its test

Added:
    erp5/trunk/products/ERP5Type/Core/TALESConstraint.py
      - copied, changed from r40829, erp5/trunk/products/ERP5Type/Constraint/TALESConstraint.py
    erp5/trunk/products/ERP5Type/PropertySheet/TALESConstraint.py
Modified:
    erp5/trunk/products/ERP5Type/PropertySheet/__init__.py
    erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py

Copied: erp5/trunk/products/ERP5Type/Core/TALESConstraint.py (from r40829, erp5/trunk/products/ERP5Type/Constraint/TALESConstraint.py)
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Core/TALESConstraint.py?p2=erp5/trunk/products/ERP5Type/Core/TALESConstraint.py&p1=erp5/trunk/products/ERP5Type/Constraint/TALESConstraint.py&r1=40829&r2=40847&rev=40847&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/TALESConstraint.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/Core/TALESConstraint.py [utf8] Mon Nov 29 10:55:52 2010
@@ -1,7 +1,8 @@
 ##############################################################################
 #
-# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
-#                    Jerome Perrin <jerome at nexedi.com>
+# Copyright (c) 2006-2010 Nexedi SARL and Contributors. All Rights Reserved.
+#                         Jerome Perrin <jerome at nexedi.com>
+#                         Arnaud Fontaine <arnaud.fontaine at nexedi.com>
 #
 # WARNING: This program as such is intended to be used by professional
 # programmers who take the whole responsability of assessing all potential
@@ -28,63 +29,65 @@
 
 import sys
 
-from Products.CMFCore.Expression import Expression
+from Products.ERP5Type.mixin.constraint import ConstraintMixin
+from Products.ERP5Type import PropertySheet
+from zLOG import LOG, PROBLEM
 from ZODB.POSException import ConflictError
 
 import Products.PageTemplates.Expressions
-# this gets the CompilerError class wherever it is defined (which is different
-# depending on the Zope version
+# this gets the CompilerError class wherever it is defined (which is
+# different depending on the Zope version
 CompilerError = Products.PageTemplates.Expressions.getEngine().getCompilerError()
 
-from zLOG import LOG, PROBLEM
-
-from Constraint import Constraint
-
-class TALESConstraint(Constraint):
-  """This constraint uses an arbitrary TALES expression on the context of the
+class TALESConstraint(ConstraintMixin):
+  """
+  This constraint uses an arbitrary TALES expression on the context of the
   object; if this expression is evaluated as False, the object will be
   considered in an inconsistent state.
-    
-    Configuration example:
-    { 'id'            : 'tales_constraint',
-      'description'   : 'Title should not be equals to foo',
-      'type'          : 'TALESConstraint',
-      'expression'    : 'python: object.getTitle() != 'foo',
-    },
+
+  This is only relevant for ZODB Property Sheets (filesystem Property
+  Sheets rely on Products.ERP5Type.Constraint.TALESConstraint
+  instead).
+
+  For example, if we would like to check whether the expression
+  'python: object.getTitle() != 'foo'' is False, then we would create
+  a 'TALES Constraint' within that Property Sheet and set 'Expression'
+  to 'python: object.getTitle() != 'foo'', then set the 'Predicate' if
+  necessary (known as 'condition' for filesystem Property Sheets).
 
   For readability, please don't abuse this constraint to evaluate complex
   things. If necessary, write your own constraint class.
   """
-  
-  _message_id_list = [ 'message_expression_false',
-                       'message_expression_error' ]
-
-  message_expression_false = "Expression was false"
-  message_expression_error = \
-      "Error while evaluating expression: ${error_text}"
+  meta_type = 'ERP5 TALES Constraint'
+  portal_type = 'TALES Constraint'
+
+  property_sheets = (PropertySheet.SimpleItem,
+                     PropertySheet.Predicate,
+                     PropertySheet.Reference,
+                     PropertySheet.TALESConstraint)
 
   def checkConsistency(self, obj, fixit=0):
-    """Check the object's consistency.
     """
-    # import this later to prevent circular import
-    from Products.ERP5Type.Utils import createExpressionContext
-    if not self._checkConstraintCondition(obj):
+    Check that the Expression does not contain an error and is not
+    evaluated to False
+    """
+    if not self.test(obj):
       return []
-    error_list = []
-    expression_text = self.constraint_definition['expression']
-    expression = Expression(expression_text)
-    econtext = createExpressionContext(obj)
+
+    expression_text = self.getExpression()
+
     try:
-      if not expression(econtext):
-        error_list.append(self._generateError(obj,
-                  self._getMessage('message_expression_false')))
+      if not self._getExpressionValue(obj, expression_text):
+        return [self._generateError(obj,
+                                    self._getMessage('message_expression_false'))]
     except (ConflictError, CompilerError):
       raise
     except Exception, e:
       LOG('ERP5Type', PROBLEM, 'TALESConstraint error on "%s" on %s' %
-         (self.constraint_definition['expression'], obj), error=sys.exc_info())
-      error_list.append(self._generateError(obj,
-                  self._getMessage('message_expression_error'),
-                  mapping=dict(error_text=str(e))))
-    return error_list
+          (expression_text, obj), error=sys.exc_info())
+
+      return [self._generateError(obj,
+                                  self._getMessage('message_expression_error'),
+                                  mapping=dict(error=str(e)))]
 
+    return []

Added: erp5/trunk/products/ERP5Type/PropertySheet/TALESConstraint.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/PropertySheet/TALESConstraint.py?rev=40847&view=auto
==============================================================================
--- erp5/trunk/products/ERP5Type/PropertySheet/TALESConstraint.py (added)
+++ erp5/trunk/products/ERP5Type/PropertySheet/TALESConstraint.py [utf8] Mon Nov 29 10:55:52 2010
@@ -0,0 +1,47 @@
+##############################################################################
+#
+# Copyright (c) 2010 Nexedi SARL and Contributors. All Rights Reserved.
+#                    Arnaud Fontaine <arnaud.fontaine at nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsability of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# garantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+##############################################################################
+
+class TALESConstraint:
+    """
+    Define a TALES Constraint for ZODB Property Sheets
+    """
+    _properties = (
+        # TALES Expression
+        {   'id': 'expression',
+            'type': 'string',
+            'description' : 'TALES Expression' },
+        {   'id': 'message_expression_false',
+            'type': 'string',
+            'description' : 'Error message when the Expression is false',
+            'default': 'Expression was false' },
+        {   'id': 'message_expression_error',
+            'type': 'string',
+            'description' : 'Error message when there is an error while '\
+                            'evaluating an Expression',
+            'default': 'Error while evaluating expression: ${error}' },
+        )

Modified: erp5/trunk/products/ERP5Type/PropertySheet/__init__.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/PropertySheet/__init__.py?rev=40847&r1=40846&r2=40847&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/PropertySheet/__init__.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/PropertySheet/__init__.py [utf8] Mon Nov 29 10:55:52 2010
@@ -24,3 +24,4 @@ from ContentExistenceConstraint import C
 from CategoryMembershipArityConstraint import CategoryMembershipArityConstraint
 from CategoryRelatedMembershipArityConstraint import \
      CategoryRelatedMembershipArityConstraint
+from TALESConstraint import TALESConstraint

Modified: erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py?rev=40847&r1=40846&r2=40847&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py [utf8] Mon Nov 29 10:55:52 2010
@@ -450,6 +450,15 @@ class TestZodbPropertySheet(ERP5TypeTest
       constraint_portal_type=('Test Migration',),
       constraint_base_category=('gender',))
 
+  def _newTALESConstraint(self):
+    """
+    Create a new TALES Constraint within test Property Sheet
+    """
+    self.test_property_sheet.newContent(
+      reference='test_tales_constraint',
+      portal_type='TALES Constraint',
+      expression='python: object.getTitle() == "my_tales_constraint_title"')
+
   def afterSetUp(self):
     """
     Create a test Property Sheet (and its properties)
@@ -500,6 +509,9 @@ class TestZodbPropertySheet(ERP5TypeTest
       # test Property Sheet
       self._newCategoryRelatedMembershipArityConstraint()
 
+      # Create a TALES Constraint in the test Property Sheet
+      self._newTALESConstraint()
+
       # Create all the test Properties
       for operation_type in ('change', 'delete', 'assign'):
         self._newStandardProperty(operation_type)
@@ -972,6 +984,16 @@ class TestZodbPropertySheet(ERP5TypeTest
 
     self.assertEquals([], constraint.checkConsistency(self.test_module))
 
+  def testTALESConstraint(self):
+    """
+    Take the test module and check whether the TALES Constraint is
+    there. Until the title of Test Module has been set to the expected
+    value, the constraint should fail
+    """
+    self._checkConstraint('test_tales_constraint',
+                          self.test_module.setTitle,
+                          'my_tales_constraint_title')
+
 TestZodbPropertySheet = skip("ZODB Property Sheets code is not enabled yet")(
   TestZodbPropertySheet)
 




More information about the Erp5-report mailing list