Commit 80fe5575 authored by Nicolas Delaby's avatar Nicolas Delaby

Improve Constraint, no need to call portal_catalog. Thanks to jerome

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@26753 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 33e302e3
...@@ -37,7 +37,7 @@ class AttributeBlacklisted(PropertyExistence): ...@@ -37,7 +37,7 @@ class AttributeBlacklisted(PropertyExistence):
{ 'id' : 'title', { 'id' : 'title',
'description' : 'Title should not belong to blacklist words', 'description' : 'Title should not belong to blacklist words',
'type' : 'AttributeBlacklisted', 'type' : 'AttributeBlacklisted',
'title' : "python: {'portal_type': object.getPortalType(), 'title': ('Foo', 'Bar',)}", 'title' : "python: ('Foo', 'Bar',)",
'condition' : 'object/getTitle', 'condition' : 'object/getTitle',
}, },
""" """
...@@ -50,22 +50,21 @@ class AttributeBlacklisted(PropertyExistence): ...@@ -50,22 +50,21 @@ class AttributeBlacklisted(PropertyExistence):
def checkConsistency(self, obj, fixit=0): def checkConsistency(self, obj, fixit=0):
"""Check the object's consistency. """Check the object's consistency.
We will make sure that each non None constraint_definition is We will make sure that each non None constraint_definition is
satisfied (unicity) satisfied
This Constraint use portal_catalog
""" """
if not self._checkConstraintCondition(obj): if not self._checkConstraintCondition(obj):
return [] return []
errors = PropertyExistence.checkConsistency(self, obj, fixit=fixit) errors = PropertyExistence.checkConsistency(self, obj, fixit=fixit)
for attribute_name, expression_criterion_dict in self.constraint_definition.items(): for attribute_name, expression_blaklisted_list in self.constraint_definition.items():
message_id = None message_id = None
mapping = dict(attribute_name=attribute_name) mapping = dict(attribute_name=attribute_name)
#Evaluate expression_criterion_dict #Evaluate expression_criterion_dict
expression = Expression(expression_criterion_dict) expression = Expression(expression_blaklisted_list)
from Products.ERP5Type.Utils import createExpressionContext from Products.ERP5Type.Utils import createExpressionContext
econtext = createExpressionContext(obj) econtext = createExpressionContext(obj)
criterion_dict = expression(econtext) blaklisted_list = expression(econtext)
result = obj.portal_catalog.countResults(**criterion_dict)[0][0] value = obj.getProperty(attribute_name)
if result: if value in blaklisted_list:
message_id = 'message_invalid_attribute_blacklisted' message_id = 'message_invalid_attribute_blacklisted'
# Generate error # Generate error
if message_id is not None: if message_id is not None:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment