From 6d91ae183dd667c024b6f9a6508ca7dd6b352317 Mon Sep 17 00:00:00 2001 From: helmutm Date: Fri, 1 Oct 2004 12:41:42 +0000 Subject: [PATCH] Task: handle multiple resource constraints git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@123 fd906abe-77d9-0310-91a1-e0d9ade77398 --- task.py | 19 +++++++++++-------- tests/test_task.py | 20 +++++++++++++++++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/task.py b/task.py index bc8a0eb..e86e3fa 100644 --- a/task.py +++ b/task.py @@ -122,24 +122,27 @@ class Task(OrderedContainer): # resource constraint stuff: - def isResourceAllowed(self, resource): + def isResourceAllowed(self, resource, rcDontCheck=None): rc = self.resourceConstraints if not rc: return True for c in rc: + if rcDontCheck and c == rcDontCheck: # don't check constraint already checked + continue # that's too simple, we must check all constraints for constraintType: - if c.isResourceAllowed(resource): - return True - return False + if not c.isResourceAllowed(resource): + return False + return True def getCandidateResources(self): rc = self.resourceConstraints if not rc: return () - result = [] for c in rc: - result.extend(c.getAllowedResources()) - return tuple(result) + candidates = c.getAllowedResources() + if candidates is not None: + return tuple([ r for r in candidates if self.isResourceAllowed(r, c) ]) + return () def getAllowedResources(self, candidates=None): rc = self.resourceConstraints @@ -149,7 +152,7 @@ class Task(OrderedContainer): result = self.getCandidateResources() # Empty result means: can't tell return result and result or None - return tuple([ c for c in candidates if self.isResourceAllowed(c) ]) + return tuple([ r for r in candidates if self.isResourceAllowed(r) ]) def isValid(self, checkSubtasks=True): if self.resourceConstraints is not None: diff --git a/tests/test_task.py b/tests/test_task.py index 381b8d6..86a81c6 100755 --- a/tests/test_task.py +++ b/tests/test_task.py @@ -150,7 +150,7 @@ class TestTaskResourceConstraints(unittest.TestCase): # the tests... - def testSelectExplicit(self): + def testRequireExplicit(self): t1 = self.t1 r1 = self.r1 rc1 = self.rc1 @@ -165,9 +165,23 @@ class TestTaskResourceConstraints(unittest.TestCase): self.assertEqual((), t1.getAllowedResources([r1])) rc1.referenceValues = ([r1]) - self.assertEqual(True, rc1.isResourceAllowed(r1)) + self.assertEqual(True, t1.isResourceAllowed(r1)) self.assertEqual((r1,), t1.getCandidateResources()) - self.assertEqual((r1,), rc1.getAllowedResources([r1])) + self.assertEqual((r1,), t1.getAllowedResources([r1])) + + def testRCCombination(self): + t1 = self.t1 + r1 = self.r1 + r2 = Resource() + self.f1['rsc2'] = r2 + rc1 = self.rc1 + rc1.referenceValues = ([r1, r2]) # allow/select both resources + rc2 = ResourceConstraint() + rc2.referenceType = 'checkmethod' + rc2.referenceKey = 'isAllowedForTesting' + Resource.isAllowedForTesting = lambda self: zapi.name(self) == 'rsc1' + t1.resourceConstraints = [rc1, rc2] + self.assertEqual((r1,), t1.getAllowedResources()) def testIsValid(self): t1 = self.t1