StringAttributeMatchConstraint.py 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Romain Courteaud <romain@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.
#
##############################################################################

import re
from Products.ERP5Type import PropertySheet
from Products.ERP5Type.Utils import createExpressionContext
from Products.ERP5Type.Utils import evaluateExpressionFromString
from Products.ERP5Type.Core.PropertyExistenceConstraint import \
    PropertyExistenceConstraint

class StringAttributeMatchConstraint(PropertyExistenceConstraint):
  """
    This constraint class allows to check that a string property matches a
    regular expression.
  """
  property_sheets = PropertyExistenceConstraint.property_sheets + \
42
      (PropertySheet.StringAttributeMatchConstraint,)
43 44 45 46 47 48 49 50 51 52 53

  def _checkConsistency(self, obj, fixit=0):
    """Check the object's consistency.
    Check that each attribute matches the regular expression.
    """
    error_list = PropertyExistenceConstraint._checkConsistency(
                                                self, obj, fixit=fixit)
    if not error_list:
      regular_expression = self.getRegularExpression()
      regexp = re.compile(regular_expression)
      for property_id in self.getConstraintPropertyList():
54
        # If property does not exist, error will be raised by
55 56 57 58
        # PropertyExistence Constraint.
        current_value = obj.getProperty(property_id)
        if (current_value is not None) and \
            (regexp.match(current_value) is None):
59

60 61
          # Generate error
          error_list.append(self._generateError(
62
                       obj,
63
                       self._getMessage("message_attribute_not_match"),
64 65 66 67
            mapping=dict(attribute_name=property_id,
                         attribute_value=repr(current_value),
                         regular_expression=repr(regular_expression))))
    return error_list
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  _message_id_tuple = PropertyExistenceConstraint._message_id_tuple + \
      ('message_attribute_not_match',)

  @staticmethod
  def _preConvertBaseFromFilesystemDefinition(filesystem_definition_dict):
    """
    'message_property_does_not_match' has been renamed to
    'message_property_not_match' to follow ERP5 naming conventions
    """
    filesystem_definition_dict['message_property_not_match'] = \
        filesystem_definition_dict.pop('message_property_does_not_match',
                                       None)

    return PropertyExistenceConstraint._preConvertBaseFromFilesystemDefinition(
      filesystem_definition_dict)

  @staticmethod
  def _convertFromFilesystemDefinition(**property_dict):
    """
    @see ERP5Type.mixin.constraint.ConstraintMixin._convertFromFilesystemDefinition

    One constraint per regular expression and containing one or several
    properties is created. Filesystem definition example:
    { 'id'            : 'title_not_empty',
      'description'   : 'Title must be defined',
      'type'          : 'StringAttributeMatch',
      'title'         : '^[^ ]'
    }
    """
    property_list = property_dict.keys()
    regex_list = property_dict.values()
    regex_list_len = len(regex_list)
    seen_property_set = set()
    for property_index, property_id in enumerate(property_list):
      if property_id in seen_property_set:
        continue

      constraint_property_list = [property_id]
      constraint_regex = regex_list[property_index]
      property_index += 1
      for regex_index, regex in enumerate(regex_list[property_index:], property_index):
        if constraint_regex == regex:
          regex_property_id = property_list[regex_index]
          constraint_property_list.append(regex_property_id)
          seen_property_set.add(regex_property_id)

      yield dict(constraint_property_list=constraint_property_list,
                 regular_expression=constraint_regex)