From 7efe448b49ff448aebde7acc1af8a1f4c1d00042 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Tue, 11 Dec 2007 12:34:44 +0000
Subject: [PATCH] CategoryRelatedMembershipArity and
 CategoryAcquiredMembershipArity were sharing too much code with
 CategoryMembershipArity, so make them subclass CategoryMembershipArity and
 redefine only the different part

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18226 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../CategoryAcquiredMembershipArity.py        | 37 +++----------
 .../Constraint/CategoryMembershipArity.py     |  9 +++-
 .../CategoryRelatedMembershipArity.py         | 53 ++++---------------
 3 files changed, 25 insertions(+), 74 deletions(-)

diff --git a/product/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py b/product/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py
index 4e96b95aed..0b4e9120d2 100644
--- a/product/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py
+++ b/product/ERP5Type/Constraint/CategoryAcquiredMembershipArity.py
@@ -28,8 +28,10 @@
 ##############################################################################
 
 from Constraint import Constraint
+from Products.ERP5Type.Constraint.CategoryMembershipArity \
+          import CategoryMembershipArity
 
-class CategoryAcquiredMembershipArity(Constraint):
+class CategoryAcquiredMembershipArity(CategoryMembershipArity):
   """
     This method check and fix if an object respects the arity.
     For example we can check if every Order has at
@@ -46,34 +48,9 @@ class CategoryAcquiredMembershipArity(Constraint):
     },
   """
 
-  def checkConsistency(self, obj, fixit=0):
-    """
-      This is the check method, we return a list of string,
-      each string corresponds to an error.
-      We are looking the definition of the constraing where
-      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)
+  def _calculateArity(self, obj):
     base_category = self.constraint_definition['base_category']
-    min_arity = int(self.constraint_definition['min_arity'])
-    max_arity = int(self.constraint_definition['max_arity'])
     portal_type = self.constraint_definition['portal_type']
-    # Check arity and compare it with the min and max
-    arity = len(obj.getAcquiredCategoryMembershipList(base_category,
-                                                 portal_type=portal_type))
-    if (arity < min_arity) or (arity > max_arity):
-      # Generate error message
-      error_message = "Arity error for relation '%s'" % \
-                      base_category
-      if portal_type is not ():
-        error_message += " and portal_type: '%s'" % str(portal_type)
-      error_message += \
-          ", arity is equal to %i but should be between %i and %i" % \
-          (arity, min_arity, max_arity)
-      # Add error
-      errors.append(self._generateError(obj, error_message))
-    return errors
+    return len(obj.getAcquiredCategoryMembershipList(base_category,
+                                              portal_type=portal_type))
+
diff --git a/product/ERP5Type/Constraint/CategoryMembershipArity.py b/product/ERP5Type/Constraint/CategoryMembershipArity.py
index e299ea9899..46e5b418d2 100644
--- a/product/ERP5Type/Constraint/CategoryMembershipArity.py
+++ b/product/ERP5Type/Constraint/CategoryMembershipArity.py
@@ -46,6 +46,12 @@ class CategoryMembershipArity(Constraint):
     },
   """
 
+  def _calculateArity(self, obj):
+    base_category = self.constraint_definition['base_category']
+    portal_type = self.constraint_definition['portal_type']
+    return len(obj.getCategoryMembershipList(base_category,
+                                              portal_type=portal_type))
+
   def checkConsistency(self, obj, fixit=0):
     """
       This is the check method, we return a list of string,
@@ -65,8 +71,7 @@ class CategoryMembershipArity(Constraint):
       max_arity = int(self.constraint_definition['max_arity'])
     portal_type = self.constraint_definition['portal_type']
     # Check arity and compare it with the min and max
-    arity = len(obj.getCategoryMembershipList(base_category,
-                                              portal_type=portal_type))
+    arity = self._calculateArity(obj)
     if not (max_arity is None and (min_arity <= arity)
         or (min_arity <= arity <= max_arity)):
       # Generate error message
diff --git a/product/ERP5Type/Constraint/CategoryRelatedMembershipArity.py b/product/ERP5Type/Constraint/CategoryRelatedMembershipArity.py
index 5399fc27db..6e8cdaae67 100644
--- a/product/ERP5Type/Constraint/CategoryRelatedMembershipArity.py
+++ b/product/ERP5Type/Constraint/CategoryRelatedMembershipArity.py
@@ -28,9 +28,11 @@
 ##############################################################################
 
 from Constraint import Constraint
+from Products.ERP5Type.Constraint.CategoryMembershipArity \
+          import CategoryMembershipArity
 
-class CategoryRelatedMembershipArity(Constraint):
-    """
+class CategoryRelatedMembershipArity(CategoryMembershipArity):
+  """
     This method check and fix if an object respects the arity
     from a category reverse membership point of view.
     For example we can check if every Order has at
@@ -45,44 +47,11 @@ class CategoryRelatedMembershipArity(Constraint):
       'base_category' : ('causality',)
       'condition'     : 'python: object.getPortalType() == 'Foo',
     },
-    """
+  """
+  
+  def _calculateArity(self, obj):
+    base_category = self.constraint_definition['base_category']
+    portal_type = self.constraint_definition['portal_type']
+    return len(obj._getRelatedValueList(base_category,
+                                        portal_type=portal_type))
 
-    def checkConsistency(self, obj, fixit=0):
-      """
-        This is the check method, we return a list of string,
-        each string corresponds to an error.
-        We are looking the definition of the constraing where
-        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']
-      min_arity = int(self.constraint_definition['min_arity'])
-      max_arity = None
-      if 'max_arity' in self.constraint_definition:
-        max_arity = int(self.constraint_definition['max_arity'])
-      portal_type = self.constraint_definition['portal_type']
-      # Check arity and compare it with the min and max
-      arity = len(obj._getRelatedValueList(base_category, 
-                                              portal_type=portal_type))
-      if not (max_arity is None and (min_arity <= arity)
-          or (min_arity <= arity <= max_arity)):
-        # Generate error message
-        error_message = "Arrity error for reverse relation '%s'" % \
-                        base_category
-        if portal_type is not ():
-          error_message += " and portal_type: '%s'" % str(portal_type)
-        if max_arity is None:
-          error_message += \
-            ", arity is equal to %i but should be at least %i" % \
-            (arity, min_arity)
-        else:
-          error_message += \
-            ", arity is equal to %i but should be between %i and %i" % \
-            (arity, min_arity, max_arity)
-        # Add error
-        errors.append(self._generateError(obj, error_message))
-      return errors
-- 
2.30.9