Printable version XML version
Login
Name

Password


Join
Forgot your password?
erp5.org => wiki.erp5.org !

erp5.org has permanently moved to wiki.erp5.org !

Current status of ERP5 community websites:

  • www.erp5.org should redirect to wiki.erp5.org automaticcaly.
  • wiki.erp5.org is the place where fresh news and documentation are published.
  • cps.erp5.org is the old erp5 community website.

Note: if you created content in this ancient portal, please migrate it to the wiki. The old website will stay online as long as all contents are not mograted to the wiki.

ERP5 Data Consistency

Consistency :

An ERP can manage so many objects that we probably want to check how strong they are. Many obects have many relations with others objects, and it's possible that one of them could be broken for any reason (for example the object pointed was deleted). An object could have some mandatory datas, but for some reasons, we can't put them when this object is created, so we have to check at some moment whever or not thoses mandatory datas are present or not.

Definition of the consistency :

The definition of the consistency will be defined inside the PropertySheet of our object. This is an example :

    _constraints = (
      { 'id'            : 'forme',
        'description'   : 'The must be one and one only Forme',
        'type'          : 'CategoryMembershipArity',
        'min_arity'     : '0',
        'max_arity'     : '1',
        'portal_type'   : ('Forme',),
        'base_category' : ('specialise',)
       },
      { 'id'            : 'gamme',
        'description'   : 'The must be one and one only Gamme',
        'type'          : 'CategoryMembershipArity',
        'min_arity'     : '0',
        'max_arity'     : '1',
        'portal_type'   : ('Gamme',),
        'base_category' : ('specialise')
       },
    )

As you can see, we needs to set properly the constraint. In this example we want to check the arity of two components of an object. So we should create two constraints inside the PropertySheet, one for each component from which we want to check the arity. So for each constraint we first give an id and a description.

The type corresponds to the name of the class inside the ERP5/Constraint folder. Here this is CategoryMembershipArity. Let's take a look to this class :

    from Constraint import Constraint

    class CategoryMembershipArity(Constraint):
      """
      This method check and fix if an object respects the arity.

      For example we can check if every Organisation has at
      least one address.

      """

Every specific Constraint class have to be a subclass of Constraint and should have at least one function : checkConsistency. This is what we can see in CategoryMembershipArity :

    def checkConsistency(self, object, 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.
      """
      errors = []
      # Retrieve values inside de PropertySheet (_constraints)
      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(object.getCategoryMembershipList(base_category,spec=portal_type))
      if arity < min_arity or arity > max_arity:
        if portal_type is ():
          error_message = "Arrity error for relation '%s'" % base_category  \
                + ", arity is equal to " \
                + str(arity) \
                + " but should be between " \
                + str(min_arity) + " and " + str(max_arity)
        else:
          error_message = "Arrity error for relation '%s' and  portal_type : " % base_category  \
                + str(portal_type) \
                + ", arity is equal to " \
                + str(arity) \
                + " but should be between " \
                + str(min_arity) + " and " + str(max_arity)
        errors += [(object.getRelativeUrl(), 'CategoryMembershipArity inconsistency',104, error_message)]
      return errors

I will not explain in details what this particular code do, we can have as many kind of method that we have of type of consistency to check...

The fixit parameter of checkConsistency can be used in order to fix problems. Indeed, most of the time, it's almost the same code if we want to find inconsistencies or if we want to fix them. So instead to create a fixConsistency with a code very similar to checkConsistency, by subclassing the Constraint class, we already have a fixConstency method defined like this :

    def fixConsistency(self, object):
      """
        Default method is to call checkConsistency with
        fixit set to 1
      """
      return self.checkConsistency(object, fixit = 1)

Like this, inside your checkConsistency method, when you found an error, you can simply add :

    if fixit==1:
      do_whatever_to_fix


(c) 2001-2004 ERP5 Foundation
www.erp5.org
All Content Published Under Free Licenses
Powered by ERP5 Open Source ERP, Zope, CPS and Nexedi