[Erp5-report] r10316 - in /erp5/trunk/products/ERP5Type: Constraint/ tests/

nobody at svn.erp5.org nobody at svn.erp5.org
Tue Sep 26 10:55:12 CEST 2006


Author: alex
Date: Tue Sep 26 10:55:09 2006
New Revision: 10316

URL: http://svn.erp5.org?rev=10316&view=rev
Log:
Added the ability to add a TALES expression as a condition to Constraints
(except PropertyTypeValidity, as it is implicitly defined for all Document
Types).

Currently, it is only tested in PropertyExistence test (but the logic is the
same in each constraint).

Modified:
    erp5/trunk/products/ERP5Type/Constraint/AttributeEquality.py
    erp5/trunk/products/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py
    erp5/trunk/products/ERP5Type/Constraint/CategoryExistence.py
    erp5/trunk/products/ERP5Type/Constraint/CategoryMembershipArity.py
    erp5/trunk/products/ERP5Type/Constraint/CategoryRelatedMembershipArity.py
    erp5/trunk/products/ERP5Type/Constraint/Constraint.py
    erp5/trunk/products/ERP5Type/Constraint/PortalTypeClass.py
    erp5/trunk/products/ERP5Type/Constraint/PropertyExistence.py
    erp5/trunk/products/ERP5Type/tests/testConstraint.py

Modified: erp5/trunk/products/ERP5Type/Constraint/AttributeEquality.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/AttributeEquality.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/AttributeEquality.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/AttributeEquality.py Tue Sep 26 10:55:09 2006
@@ -39,6 +39,7 @@
       'description'   : 'Title must be "ObjectTitle"',
       'type'          : 'AttributeEquality',
       'title'         : 'ObjectTitle',
+      'condition'     : 'python: object.getPortalType() == 'Foo',
     },
   """
 
@@ -49,6 +50,8 @@
       We will make sure that each non None constraint_definition is 
       satisfied (equality)
     """
+    if not self._checkConstraintCondition(obj):
+      return []
     errors = PropertyExistence.checkConsistency(self, obj, fixit=fixit)
     for attribute_name, attribute_value in self.constraint_definition.items():
       error_message = None

Modified: erp5/trunk/products/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py Tue Sep 26 10:55:09 2006
@@ -42,6 +42,7 @@
       'max_arity'     : '1',
       'portal_type'   : ('Organisation', ),
       'base_category' : ('source',)
+      'condition'     : 'python: object.getPortalType() == 'Foo',
     },
   """
 
@@ -53,6 +54,8 @@
       are defined the minimum and the maximum arity, and the
       list of objects we wants to check the arity.
     """
+    if not self._checkConstraintCondition(obj):
+      return []
     errors = []
     # Retrieve values inside de PropertySheet (_constraints)
     base_category = self.constraint_definition['base_category']

Modified: erp5/trunk/products/ERP5Type/Constraint/CategoryExistence.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/CategoryExistence.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/CategoryExistence.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/CategoryExistence.py Tue Sep 26 10:55:09 2006
@@ -39,6 +39,7 @@
       'type'          : 'CategoryExistence',
       'portal_type'   : ('Person', 'Organisation')
       'causality'     : None,
+      'condition'     : 'python: object.getPortalType() == 'Foo',
     },
   """
 
@@ -47,6 +48,8 @@
       This is the check method, we return a list of string,
       each string corresponds to an error.
     """
+    if not self._checkConstraintCondition(obj):
+      return []
     errors = []
     # For each attribute name, we check if defined
     for base_category in self.constraint_definition.keys():

Modified: erp5/trunk/products/ERP5Type/Constraint/CategoryMembershipArity.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/CategoryMembershipArity.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/CategoryMembershipArity.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/CategoryMembershipArity.py Tue Sep 26 10:55:09 2006
@@ -42,6 +42,7 @@
       'max_arity'     : '1',
       'portal_type'   : ('Organisation', ),
       'base_category' : ('source',)
+      'condition'     : 'python: object.getPortalType() == 'Foo',
     },
   """
 
@@ -53,6 +54,8 @@
       are defined the minimum and the maximum arity, and the
       list of objects we wants to check the arity.
     """
+    if not self._checkConstraintCondition(obj):
+      return []
     errors = []
     # Retrieve values inside de PropertySheet (_constraints)
     base_category = self.constraint_definition['base_category']

Modified: erp5/trunk/products/ERP5Type/Constraint/CategoryRelatedMembershipArity.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/CategoryRelatedMembershipArity.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/CategoryRelatedMembershipArity.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/CategoryRelatedMembershipArity.py Tue Sep 26 10:55:09 2006
@@ -43,6 +43,7 @@
       'max_arity'     : '1',
       'portal_type'   : ('Applied Rule', ),
       'base_category' : ('causality',)
+      'condition'     : 'python: object.getPortalType() == 'Foo',
     },
     """
 
@@ -54,6 +55,8 @@
         are defined the minimum and the maximum arity, and the
         list of objects we wants to check the arity.
       """
+      if not self._checkConstraintCondition(obj):
+        return []
       errors = []
       # Retrieve values inside de PropertySheet (_constraints)
       base_category = self.constraint_definition['base_category']

Modified: erp5/trunk/products/ERP5Type/Constraint/Constraint.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/Constraint.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/Constraint.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/Constraint.py Tue Sep 26 10:55:09 2006
@@ -28,13 +28,15 @@
 #
 ##############################################################################
 
+from Products.CMFCore.Expression import Expression
+
 class Constraint:
     """
       Default Constraint implementation
     """
 
     def __init__(self, id=None, description=None, type=None,
-                 **constraint_definition):
+                 condition=None, **constraint_definition):
       """
         Remove unwanted attributes from constraint definition and keep
         them as instance attributes
@@ -42,6 +44,7 @@
       self.id = id
       self.description = description
       self.type = type
+      self.condition = condition
       self.constraint_definition = constraint_definition
 
     def edit(self, id=None, description=None, type=None,
@@ -66,6 +69,21 @@
                  104, error_message, self.description)
       return error
 
+    def _checkConstraintCondition(self, obj):
+      """
+        method that will check if the TALES condition is true.
+        It should be called by checkConsistency, which should ignore
+        constraints if TALES is False
+      """
+      from Products.ERP5Type.Utils import createExpressionContext
+      condition = getattr(self, 'condition', None)
+      if condition not in (None, ''):
+        expression = Expression(condition)
+        econtext = createExpressionContext(obj)
+        if not expression(econtext):
+          return 0 # a condition was defined and is False
+      return 1 # no condition or a True condition was defined
+
     def checkConsistency(self, obj, fixit=0):
       """
         Default method is to return no error.

Modified: erp5/trunk/products/ERP5Type/Constraint/PortalTypeClass.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/PortalTypeClass.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/PortalTypeClass.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/PortalTypeClass.py Tue Sep 26 10:55:09 2006
@@ -43,6 +43,7 @@
       'description'   : 'The __class__ must be the same as the portal'\
                         ' type definition',
       'type'          : 'PortalTypeClass',
+      'condition'     : 'python: object.getPortalType() == 'Foo',
     },
   """
 
@@ -51,6 +52,8 @@
       This is the check method, we return a list of string,
       each string corresponds to an error.
     """
+    if not self._checkConstraintCondition(obj):
+      return []
     errors = []
     types_tool = getToolByName(obj, 'portal_types')
     type_info = types_tool._getOb(obj.getPortalType(), None)

Modified: erp5/trunk/products/ERP5Type/Constraint/PropertyExistence.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/PropertyExistence.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/PropertyExistence.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/PropertyExistence.py Tue Sep 26 10:55:09 2006
@@ -40,6 +40,7 @@
       'description'   : 'Property price must be defined',
       'type'          : 'PropertyExistence',
       'price'         : None,
+      'condition'     : 'python: object.getPortalType() == 'Foo',
     },
   """
 
@@ -48,6 +49,8 @@
       This is the check method, we return a list of string,
       each string corresponds to an error.
     """
+    if not self._checkConstraintCondition(obj):
+      return []
     errors = []
     # For each attribute name, we check if defined
     for property_id in self.constraint_definition.keys():

Modified: erp5/trunk/products/ERP5Type/tests/testConstraint.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/tests/testConstraint.py?rev=10316&r1=10315&r2=10316&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/tests/testConstraint.py (original)
+++ erp5/trunk/products/ERP5Type/tests/testConstraint.py Tue Sep 26 10:55:09 2006
@@ -324,6 +324,32 @@
                                   description='propertyExistence test',
                                   not_defined_property=None)
 
+  def stepCreatePropertyExistence1TrueCondition(self, sequence=None, 
+                                   sequence_list=None, **kw):
+    """
+      Create a PropertyExistence Constraint with a true condition
+    """
+    self._createGenericConstraint(sequence, 
+                                  klass_name='PropertyExistence',
+                                  id='property_existence',
+                                  description='propertyExistence test',
+                                  not_defined_property=None,
+                                  condition='python: object.getPortalType()' \
+                                      + ' == "%s"' % self.object_portal_type)
+
+  def stepCreatePropertyExistence1FalseCondition(self, sequence=None, 
+                                   sequence_list=None, **kw):
+    """
+      Create a PropertyExistence Constraint with a false condition
+    """
+    self._createGenericConstraint(sequence, 
+                                  klass_name='PropertyExistence',
+                                  id='property_existence',
+                                  description='propertyExistence test',
+                                  not_defined_property=None,
+                                  condition='python: object.getPortalType()' \
+                                      + ' == "False_PortalTypeXXX123"')
+
   def stepCreatePropertyExistence2(self, sequence=None, 
                                    sequence_list=None, **kw):
     """
@@ -355,6 +381,24 @@
               CreatePropertyExistence1 \
               CallCheckConsistency \
               CheckIfConstraintFailed \
+              '
+    sequence_list.addSequenceString(sequence_string)
+    # Test Constraint with property not defined in PropertySheet and true
+    # condition
+    sequence_string = '\
+              CreateObject \
+              CreatePropertyExistence1TrueCondition \
+              CallCheckConsistency \
+              CheckIfConstraintFailed \
+              '
+    sequence_list.addSequenceString(sequence_string)
+    # Test Constraint with property not defined in PropertySheet and false
+    # condition
+    sequence_string = '\
+              CreateObject \
+              CreatePropertyExistence1FalseCondition \
+              CallCheckConsistency \
+              CheckIfConstraintSucceeded \
               '
     sequence_list.addSequenceString(sequence_string)
     # Test Constraint with property defined on object




More information about the Erp5-report mailing list