Commit 08016c15 authored by Arnaud Fontaine's avatar Arnaud Fontaine

Add Category Related Membership Arity Constraint for ZODB Property

Sheets and its test


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@40830 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0cd35ba3
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
# Sebastien Robin <seb@nexedi.com>
# Courteaud Romain <romain@nexedi.com>
# Arnaud Fontaine <arnaud.fontaine@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.
#
##############################################################################
from Products.ERP5Type import PropertySheet
from Products.ERP5Type.Core.CategoryMembershipArityConstraint \
import CategoryMembershipArityConstraint
class CategoryRelatedMembershipArityConstraint(CategoryMembershipArityConstraint):
"""
This constraint checks if an object respects the arity from a
category reverse membership point of view. 'filter_parameter' is an
additional parameters passed to catalog.
This is only relevant for ZODB Property Sheets (filesystem Property
Sheets rely on Products.ERP5Type.Constraint.CategoryRelatedMembershipArity
instead).
For example, if we would like to check whether the object respects a
minimum arity of '1', a maximum arity of '1', and with a filter
parameter 'simulation_state': ('planned',)' for the Portal Type
'Organisation' and the Base Category 'source', then we would create
a 'Category Related Membership Arity Constraint' within that
Property Sheet and set 'Minimum arity' to '1', 'Maximum Arity' to
'1', 'Filter parameters' to 'python: {'simulation_state':
('planned',)}' 'Portal Types' to 'Organisation', 'Base Categories'
to 'source', then set the 'Predicate' if necessary (known as
'condition' for filesystem Property Sheets).
"""
meta_type = 'ERP5 Category Related Membership Arity Constraint'
portal_type = 'Category Related Membership Arity Constraint'
property_sheets = CategoryMembershipArityConstraint.property_sheets + \
(PropertySheet.CategoryRelatedMembershipArityConstraint,)
def _calculateArity(self, obj, base_category_list, portal_type_list):
# XXX: Only supports one category, the code with filesystem
# Property Sheets assumes that 'base_category' is not a tuple but
# a string
assert len(base_category_list) != 0
sql_kw = {'portal_type': portal_type_list,
'%s_uid' % base_category_list[0]: obj.getUid()}
sql_kw.update(self._getExpressionValue(obj, self.getFilterParameter()))
portal = obj.getPortalObject()
return len(portal.portal_catalog.unrestrictedSearchResults(**sql_kw))
##############################################################################
#
# Copyright (c) 2010 Nexedi SARL and Contributors. All Rights Reserved.
# Arnaud Fontaine <arnaud.fontaine@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.
#
##############################################################################
class CategoryRelatedMembershipArityConstraint:
"""
Define a Category Related Membership Arity Constraint for ZODB
Property Sheets
@see CategoryMembershipArityConstraint
"""
# TALES Expression
_properties = (
{ 'id': 'filter_parameter',
'type': 'string',
'description' : 'Addition parameters passed to catalog',
'default': 'python: {}' },
)
...@@ -22,3 +22,5 @@ from PropertyExistenceConstraint import PropertyExistenceConstraint ...@@ -22,3 +22,5 @@ from PropertyExistenceConstraint import PropertyExistenceConstraint
from AttributeEqualityConstraint import AttributeEqualityConstraint from AttributeEqualityConstraint import AttributeEqualityConstraint
from ContentExistenceConstraint import ContentExistenceConstraint from ContentExistenceConstraint import ContentExistenceConstraint
from CategoryMembershipArityConstraint import CategoryMembershipArityConstraint from CategoryMembershipArityConstraint import CategoryMembershipArityConstraint
from CategoryRelatedMembershipArityConstraint import \
CategoryRelatedMembershipArityConstraint
...@@ -436,6 +436,20 @@ class TestZodbPropertySheet(ERP5TypeTestCase): ...@@ -436,6 +436,20 @@ class TestZodbPropertySheet(ERP5TypeTestCase):
constraint_portal_type=('Test Migration',), constraint_portal_type=('Test Migration',),
constraint_base_category=(reference,)) constraint_base_category=(reference,))
def _newCategoryRelatedMembershipArityConstraint(self):
"""
Create a new Category Related Membership Arity Constraint within
test Property Sheet, using an existing Base Category because
creating a new Base Category would involve clearing up the cache
"""
self.test_property_sheet.newContent(
reference='test_category_related_membership_arity_constraint',
portal_type='Category Related Membership Arity Constraint',
min_arity=1,
max_arity=1,
constraint_portal_type=('Test Migration',),
constraint_base_category=('gender',))
def afterSetUp(self): def afterSetUp(self):
""" """
Create a test Property Sheet (and its properties) Create a test Property Sheet (and its properties)
...@@ -482,6 +496,10 @@ class TestZodbPropertySheet(ERP5TypeTestCase): ...@@ -482,6 +496,10 @@ class TestZodbPropertySheet(ERP5TypeTestCase):
'test_category_acquired_membership_arity_constraint', 'test_category_acquired_membership_arity_constraint',
'Category Acquired Membership Arity Constraint') 'Category Acquired Membership Arity Constraint')
# Create a Category Related Membership Arity Constraint in the
# test Property Sheet
self._newCategoryRelatedMembershipArityConstraint()
# Create all the test Properties # Create all the test Properties
for operation_type in ('change', 'delete', 'assign'): for operation_type in ('change', 'delete', 'assign'):
self._newStandardProperty(operation_type) self._newStandardProperty(operation_type)
...@@ -525,11 +543,6 @@ class TestZodbPropertySheet(ERP5TypeTestCase): ...@@ -525,11 +543,6 @@ class TestZodbPropertySheet(ERP5TypeTestCase):
# Ensure that erp5.acessor_holder is empty # Ensure that erp5.acessor_holder is empty
synchronizeDynamicModules(portal, force=True) synchronizeDynamicModules(portal, force=True)
# Make sure there is no pending transaction which could interfere
# with the tests
transaction.commit()
self.tic()
def _forceTestAccessorHolderGeneration(self): def _forceTestAccessorHolderGeneration(self):
""" """
Force generation of TestMigration accessor holder by accessing any Force generation of TestMigration accessor holder by accessing any
...@@ -931,12 +944,34 @@ class TestZodbPropertySheet(ERP5TypeTestCase): ...@@ -931,12 +944,34 @@ class TestZodbPropertySheet(ERP5TypeTestCase):
Take the test module and check whether the Category Acquired Take the test module and check whether the Category Acquired
Membership Arity Constraint is there. Until a Base Category is set Membership Arity Constraint is there. Until a Base Category is set
on the Test Module, the constraint should fail on the Test Module, the constraint should fail
XXX: Test with acquisition?
""" """
self._checkConstraint( self._checkConstraint(
'test_category_acquired_membership_arity_constraint', 'test_category_acquired_membership_arity_constraint',
self.test_module.setCategoryList, self.test_module.setCategoryList,
('test_category_acquired_membership_arity_constraint/Test Migration',)) ('test_category_acquired_membership_arity_constraint/Test Migration',))
def testCategoryRelatedMembershipArityConstraint(self):
"""
Take the test module and check whether the Category Related
Membership Arity Constraint is there. Until a Base Category is set
on the Test Module, the constraint should fail
XXX: Test filter_parameter
"""
constraint = self._getConstraintByReference(
'test_category_related_membership_arity_constraint')
self.failIfEqual(None, constraint)
self.assertEquals(1, len(constraint.checkConsistency(self.test_module)))
self.test_module.setCategoryList(('gender/Test Migration',))
transaction.commit()
self.tic()
self.assertEquals([], constraint.checkConsistency(self.test_module))
TestZodbPropertySheet = skip("ZODB Property Sheets code is not enabled yet")( TestZodbPropertySheet = skip("ZODB Property Sheets code is not enabled yet")(
TestZodbPropertySheet) TestZodbPropertySheet)
......
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