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

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Dec 1 04:09:30 CET 2010


Author: arnaud.fontaine
Date: Wed Dec  1 04:09:29 2010
New Revision: 40945

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

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

Copied: erp5/trunk/products/ERP5Type/Core/PropertyTypeValidityConstraint.py (from r40897, erp5/trunk/products/ERP5Type/Constraint/PropertyTypeValidity.py)
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Core/PropertyTypeValidityConstraint.py?p2=erp5/trunk/products/ERP5Type/Core/PropertyTypeValidityConstraint.py&p1=erp5/trunk/products/ERP5Type/Constraint/PropertyTypeValidity.py&r1=40897&r2=40945&rev=40945&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/PropertyTypeValidity.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/Core/PropertyTypeValidityConstraint.py [utf8] Wed Dec  1 04:09:29 2010
@@ -28,24 +28,33 @@
 #
 ##############################################################################
 
-from Constraint import Constraint
+from Products.ERP5Type.mixin.constraint import ConstraintMixin
+from Products.ERP5Type import PropertySheet
 from DateTime import DateTime
 
-boolean_types = (int, bool)
-
-class PropertyTypeValidity(Constraint):
+class PropertyTypeValidityConstraint(ConstraintMixin):
   """
-    This constraint class allows to check / fix type of each
-    attributes defined in the PropertySheets.
-    This Constraint is always created in ERP5Type/Utils.py
+  This constraint class allows to check / fix type of each attributes
+  defined in the PropertySheets.
+
+  This is only relevant for ZODB Property Sheets (filesystem Property
+  Sheets rely on Products.ERP5Type.Constraint.PropertyTypeValidity
+  instead).
   """
+  meta_type = 'ERP5 Property Type Validity Constraint'
+  portal_type = 'Property Type Validity Constraint'
+
+  property_sheets = (PropertySheet.SimpleItem,
+                     PropertySheet.Predicate,
+                     PropertySheet.Reference,
+                     PropertySheet.PropertyTypeValidityConstraint)
 
   # Initialize type dict
   _type_dict = {
     'string':             (str, ),
     'text':               (str, ),
     'int':                (int, ),
-    'boolean':            boolean_types,
+    'boolean':            (int, bool),
     'float':              (float, ),
     'long':               (long, ),
     'tales':              (str, ),
@@ -55,29 +64,17 @@ class PropertyTypeValidity(Constraint):
     'multiple selection': (list, tuple),
     'date':               (DateTime, ),
   }
-  
+
   # Properties of type eg. "object" can hold anything
   _permissive_type_list = ('object', 'data')
 
-  _message_id_list = [ 'message_unknown_type',
-                       'message_incorrect_type',
-                       'message_incorrect_type_fix_failed',
-                       'message_incorrect_type_fixed']
-
-  message_unknown_type = "Attribute ${attribute_name} is defined with"\
-                         " an unknown type ${type_name}"
-  message_incorrect_type = "Attribute ${attribute_name}"\
-    " should be of type ${expected_type} but is of type ${actual_type}"
-  message_incorrect_type_fix_failed = "Attribute ${attribute_name}"\
-    " should be of type ${expected_type} but is of type ${actual_type}"\
-    " (Type cast failed with error ${type_cast_error})"
-  message_incorrect_type_fixed = "Attribute ${attribute_name}"\
-    " should be of type ${expected_type} but is of type ${actual_type} (Fixed)"
-  
-
   def checkConsistency(self, obj, fixit=0):
-    """Check the object's consistency.
     """
+    Check the object's consistency.
+    """
+    if not self.test(obj):
+      return []
+
     error_list = []
     # For each attribute name, we check type
     for prop in obj.propertyMap():
@@ -87,8 +84,8 @@ class PropertyTypeValidity(Constraint):
       else:
         property_type = prop['type']
 
-    # if this property was a local property and has been later added in a
-    # property sheet, we want to remove it from _local_properties
+      # if this property was a local property and has been later added in a
+      # property sheet, we want to remove it from _local_properties
       if fixit and \
          property_id in [x['id'] for x in
              getattr(obj, '_local_properties', ())] and \

Modified: erp5/trunk/products/ERP5Type/PropertySheet/PropertyTypeValidityConstraint.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/PropertySheet/PropertyTypeValidityConstraint.py?rev=40945&r1=40944&r2=40945&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/PropertySheet/PropertyTypeValidityConstraint.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/PropertySheet/PropertyTypeValidityConstraint.py [utf8] Wed Dec  1 04:09:29 2010
@@ -1,6 +1,7 @@
 ##############################################################################
 #
 # Copyright (c) 2010 Nexedi SA 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
@@ -26,12 +27,32 @@
 ##############################################################################
 
 class PropertyTypeValidityConstraint:
-  """Property sheet to enable Property Type Validity Constraint
   """
-
-  _constraints = (
-    { 'type': 'PropertyTypeValidity',
-      'id': 'type_check',
-      'description': "Type Validity Check Error",
-    },
-  )
+  Define an Attribute Equality Constraint for ZODB Property Sheets
+  """
+  _properties = (
+    {'id': 'message_unknown_type',
+     'type': 'string',
+     'description' : "Error message when the attribute's type is unknown",
+     'default': "Attribute ${attribute_name} is defined with an unknown "\
+                "type ${type_name}" },
+    {'id': 'message_incorrect_type',
+     'type': 'string',
+     'description' : "Error message when the type of attribute's value is "\
+                     "incorrect",
+     'default': "Attribute ${attribute_name} should be of type "\
+                "${expected_type} but is of type ${actual_type}" },
+    {'id': 'message_incorrect_type_fix_failed',
+     'type': 'string',
+     'description' : "Error message when the type of attribute's value is "\
+                     "incorrect and it could not be fixed",
+     'default': "Attribute ${attribute_name} should be of type "\
+                "${expected_type} but is of type ${actual_type} (Type cast "\
+                "failed with error ${type_cast_error})" },
+    {'id': 'message_incorrect_type_fixed',
+     'type': 'string',
+     'description' : "Error message when the type of attribute's value is "\
+                     "incorrect but could be fixed",
+     'default': "Attribute ${attribute_name} should be of type "\
+                "${expected_type} but is of type ${actual_type} (Fixed)" },
+    )

Modified: erp5/trunk/products/ERP5Type/PropertySheet/__init__.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/PropertySheet/__init__.py?rev=40945&r1=40944&r2=40945&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/PropertySheet/__init__.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/PropertySheet/__init__.py [utf8] Wed Dec  1 04:09:29 2010
@@ -25,3 +25,4 @@ from CategoryMembershipArityConstraint i
 from CategoryRelatedMembershipArityConstraint import \
      CategoryRelatedMembershipArityConstraint
 from TALESConstraint import TALESConstraint
+from PropertyTypeValidityConstraint import PropertyTypeValidityConstraint

Modified: erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py?rev=40945&r1=40944&r2=40945&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/tests/testDynamicClassGeneration.py [utf8] Wed Dec  1 04:09:29 2010
@@ -459,6 +459,15 @@ class TestZodbPropertySheet(ERP5TypeTest
       portal_type='TALES Constraint',
       expression='python: object.getTitle() == "my_tales_constraint_title"')
 
+  def _newPropertyTypeValidityConstraint(self):
+    """
+    Create a new Property Type Validity Constraint within test
+    Property Sheet
+    """
+    self.test_property_sheet.newContent(
+      reference='test_property_type_validity_constraint',
+      portal_type='Property Type Validity Constraint')
+
   def afterSetUp(self):
     """
     Create a test Property Sheet (and its properties)
@@ -512,6 +521,9 @@ class TestZodbPropertySheet(ERP5TypeTest
       # Create a TALES Constraint in the test Property Sheet
       self._newTALESConstraint()
 
+      # Create a Property Type Validity Constraint in the test Property Sheet
+      self._newPropertyTypeValidityConstraint()
+
       # Create all the test Properties
       for operation_type in ('change', 'delete', 'assign'):
         self._newStandardProperty(operation_type)
@@ -994,6 +1006,19 @@ class TestZodbPropertySheet(ERP5TypeTest
                           self.test_module.setTitle,
                           'my_tales_constraint_title')
 
+  def testPropertyTypeValidityConstraint(self):
+    """
+    Take the test module and check whether the Property Type Validity
+    Constraint is there, then set the title of Test Module to any
+    value besides of a string. Until the title of Test Module has been
+    set to any string, the constraint should fail
+    """
+    self.test_module.title = 123
+
+    self._checkConstraint('test_property_type_validity_constraint',
+                          self.test_module.setTitle,
+                          'my_property_type_validity_constraint_title')
+
 TestZodbPropertySheet = skip("ZODB Property Sheets code is not enabled yet")(
   TestZodbPropertySheet)
 



More information about the Erp5-report mailing list