From: yenzenz <svn...@pl...> - 2007-01-04 12:26:17
|
Author: yenzenz Date: Thu Jan 4 12:26:11 2007 New Revision: 7325 Added: Relations/branches/zuccaro-events/BRANCH-zuccaro-events.txt Relations/branches/zuccaro-events/doc/events.txt Relations/branches/zuccaro-events/events.py Relations/branches/zuccaro-events/tests/testEvents.py Modified: Relations/branches/zuccaro-events/processor.py Relations/branches/zuccaro-events/ruleset.py Log: added first (non-working) code Added: Relations/branches/zuccaro-events/BRANCH-zuccaro-events.txt ============================================================================== --- (empty file) +++ Relations/branches/zuccaro-events/BRANCH-zuccaro-events.txt Thu Jan 4 12:26:11 2007 @@ -0,0 +1,4 @@ +This branch aims to enable relations to send z3 events on +connect/disconnect. + +--Jensens <je...@bl...> Added: Relations/branches/zuccaro-events/doc/events.txt ============================================================================== Added: Relations/branches/zuccaro-events/events.py ============================================================================== --- (empty file) +++ Relations/branches/zuccaro-events/events.py Thu Jan 4 12:26:11 2007 @@ -0,0 +1,37 @@ +from zope.interface import implements, Interface, Attribute + +class IRelationEvent(Interface): + """An generic event related to a Relation. + + The references are all from one single Relation. + """ + + references = Attribute("A list of references") + + +class IRelationConnectedEvent(IRelationEvent): + """An event related to a Relation connection.""" + + +class IRelationDisconnectedEvent(IRelationEvent): + """An event related to a Relation disconnection.""" + + +class RelationEvent(object): + """connect happend.""" + + implements(IRelationConnectedEvent) + + def __init__(self, references): + self.references = references + +class RelationConnectedEvent(RelationEvent): + """connect happend.""" + + implements(IRelationConnectedEvent) + + +class RelationDisconnectedEvent(RelationEvent): + """disconnect happend.""" + + implements(IRelationDisconnectedEvent) Modified: Relations/branches/zuccaro-events/processor.py ============================================================================== --- Relations/branches/zuccaro-events/processor.py (original) +++ Relations/branches/zuccaro-events/processor.py Thu Jan 4 12:26:11 2007 @@ -11,6 +11,10 @@ import brain import interfaces +from events import RelationConnectedEvent +from events import RelationDisconnectedEvent +from zope.event import notify + modulesec = ModuleSecurityInfo('Products.Relations.processor') class Chain(dict): @@ -48,13 +52,19 @@ # implyOnConnect for sUID, tUID, relationship in connect: + chainstart = len(chain.added) ruleset = _lookup(relationship, library, seen_rulesets) sbrain = brain.makeBrainAggregate(context, sUID) tbrain = brain.makeBrainAggregate(context, tUID) ruleset.implyOnConnect(sbrain, tbrain, chain) + connected = chain.added[chainstart:] + if connected != []: + notify(RelationConnectedEvent(connected)) + # implyOnDisconnect for item in disconnect: + chainstart = len(chain.deleted) if not isinstance(item, str): # it's a triple (sUID, tUID, relship) sUID, tUID, relationship = item ruleset = _lookup(relationship, library, seen_rulesets) @@ -69,6 +79,9 @@ ref = refs[0] ruleset = _lookup(ref.relationship, library, seen_rulesets) ruleset.implyOnDisconnect(refs[0].getObject(), chain) + disconnected = chain.deleted[chainstart:] + if disconnected != []: + notify(RelationDisconnectedEvent(disconnected)) # validateConnected and validateDisconnected _validate(context, chain, seen_rulesets) Modified: Relations/branches/zuccaro-events/ruleset.py ============================================================================== --- Relations/branches/zuccaro-events/ruleset.py (original) +++ Relations/branches/zuccaro-events/ruleset.py Thu Jan 4 12:26:11 2007 @@ -1,4 +1,5 @@ from xml.dom import minidom +from sets import Set from Acquisition import ImplicitAcquisitionWrapper, aq_parent, aq_inner from Globals import InitializeClass @@ -81,7 +82,7 @@ (self, ref, node.getAttribute('uidref').strip())) elif tagname+'s' in self.Schema().keys(): - key=tagname+'s' + key = tagname + 's' self.update( ** { str(key): (self.Schema().get(key).get(self) or []) + @@ -89,10 +90,10 @@ } ) else: - id=node.getAttribute('id') - uid=node.getAttribute('uid') + id = node.getAttribute('id') + uid = node.getAttribute('uid') - typename_=[tagname[0]] + typename_ = [tagname[0]] for c in tagname[1:]: if c.isupper(): typename_.append(' '+c) @@ -102,16 +103,16 @@ typename=str(''.join(typename_)) #we are in overwrite mode, so we delete the existing subobjects - if hasattr(self.aq_base,id): + if hasattr(self.aq_base, id): self.manage_delObjects([id]) - self.invokeFactory(typename,id) - obj=getattr(self,id) + self.invokeFactory(typename, id) + obj=getattr(self, id) if uid: - doc.objectsByUid[uid]=obj + doc.objectsByUid[uid] = obj if hasattr(obj.aq_base,'importDOM'): obj.importDOM(doc, node, root=0) @@ -380,7 +381,7 @@ ruleset._afterRename(self) else: # Assume a copy: we need to register the ruleset library.addReference(ruleset, RELATIONSHIP_LIBRARY) - + class Library(RulesetAwareContainer, utils.AllowedTypesByIface, ActionProviderBase, OrderedBaseFolder, XMLImportExport): @@ -439,6 +440,15 @@ if obj not in v: v.append(obj) return v + + def getRulesetIds(self): + """ recurse return all ids of a ruleset """ + ids = Set( self.objectIds('Ruleset') ) + subsets = self.objectValues('Ruleset Collection') + [ids.update(set.getRulesetIds()) for set in subsets] + # look for objs that are not contained in self + ids.update( [obj.getId() for obj in self.getRefs(RELATIONSHIP_LIBRARY)]) + return list(ids) def getFolder(self): return self Added: Relations/branches/zuccaro-events/tests/testEvents.py ============================================================================== --- (empty file) +++ Relations/branches/zuccaro-events/tests/testEvents.py Thu Jan 4 12:26:11 2007 @@ -0,0 +1,68 @@ +import os, sys +if __name__ == '__main__': + execfile(os.path.join(sys.path[0], 'framework.py')) + +from Products.CMFTestCase import CMFTestCase + +import common +common.installProducts() +CMFTestCase.setupCMFSite() + +import Products.Relations.processor as processor + +from zope.app.event.tests.plcelesssetup import getEvents +from Products.Relations.events import IRelationConnectedEvent +from Products.Relations.events import IRelationDisconnectedEvent + +def subscriber(a): + pass + +class TestEvents(CMFTestCase.CMFTestCase): + + TYPES = 'SimpleType', 'ComplexType' + RULESETS = 'OneRuleset', 'AnotherRuleset' + + def afterSetUp(self): + common.installWithinPortal(self, self.portal) + self.objs = common.createObjects(self, self.TYPES) + self.rulesets = [common.createRuleset(self, j) for j in self.RULESETS] + + def testConnectEventAndDisconnectEvent(self): + triples = [(self.objs[0].UID(), self.objs[1].UID(), ruleset.getId()) + for ruleset in self.rulesets] + processor.process(self.portal, connect=[triples[0]] ) + events = getEvents(IRelationConnectedEvent) + + +# for ruleset in self.rulesets: +# self.assertEquals(self.objs[0].getRefs(ruleset.getId())[0], +# self.objs[1]) +# +# processor.process(self.portal, disconnect=triples) +# self.assertEquals(len(self.objs[0].getRefs()), 0) +# self.assertEquals(len(self.objs[1].getBRefs()), 0) + + def testConnectAndDisconnectWithReferenceUIDs(self): + triples = [(self.objs[0].UID(), self.objs[1].UID(), ruleset.getId()) + for ruleset in self.rulesets] + processor.process(self.portal, connect=triples) + for ruleset in self.rulesets: + self.assertEquals(self.objs[0].getRefs(ruleset.getId())[0], + self.objs[1]) + + refUIDs = [ref.UID() for ref in self.objs[0].getReferenceImpl()] + self.assertEquals(len(refUIDs), 2) + + processor.process(self.portal, disconnect=refUIDs) + self.assertEquals(len(self.objs[0].getRefs()), 0) + self.assertEquals(len(self.objs[1].getBRefs()), 0) + + +def test_suite(): + from unittest import TestSuite, makeSuite + suite = TestSuite() + suite.addTest(makeSuite(TestProcessor)) + return suite + +if __name__ == '__main__': + framework() |