[Zope-Checkins] SVN: Zope/branches/elro-parent-pointers/src/OFS/ Set parent pointers
Laurence Rowe
l at lrowe.co.uk
Tue Nov 1 00:54:05 UTC 2011
Log message for revision 123223:
Set parent pointers
Changed:
U Zope/branches/elro-parent-pointers/src/OFS/Application.py
U Zope/branches/elro-parent-pointers/src/OFS/ObjectManager.py
U Zope/branches/elro-parent-pointers/src/OFS/tests/testApplication.py
U Zope/branches/elro-parent-pointers/src/OFS/tests/testObjectManager.py
-=-
Modified: Zope/branches/elro-parent-pointers/src/OFS/Application.py
===================================================================
--- Zope/branches/elro-parent-pointers/src/OFS/Application.py 2011-10-31 23:46:54 UTC (rev 123222)
+++ Zope/branches/elro-parent-pointers/src/OFS/Application.py 2011-11-01 00:54:04 UTC (rev 123223)
@@ -33,6 +33,7 @@
from zope.globalrequest import getRequest
from zope.interface import implements
+from zope.location.interfaces import ILocation
import Folder
import misc_
@@ -51,13 +52,18 @@
):
"""Top-level system object"""
- implements(IApplication)
+ implements(
+ IApplication,
+ ILocation,
+ )
security = ClassSecurityInfo()
title = 'Zope'
__defined_roles__ = ('Manager', 'Anonymous', 'Owner')
__error_log__ = None
+ __name__ = None
+ __parent__ = None
isTopLevelPrincipiaApplicationObject = 1
manage_options=((
Modified: Zope/branches/elro-parent-pointers/src/OFS/ObjectManager.py
===================================================================
--- Zope/branches/elro-parent-pointers/src/OFS/ObjectManager.py 2011-10-31 23:46:54 UTC (rev 123222)
+++ Zope/branches/elro-parent-pointers/src/OFS/ObjectManager.py 2011-11-01 00:54:04 UTC (rev 123223)
@@ -49,11 +49,14 @@
from webdav.Lockable import ResourceLockedError
from webdav.NullResource import NullResource
from zExceptions import BadRequest
+from zope.interface import alsoProvides
from zope.interface import implements
from zope.component.interfaces import ComponentLookupError
from zope.event import notify
from zope.lifecycleevent import ObjectAddedEvent
from zope.lifecycleevent import ObjectRemovedEvent
+from zope.location.interfaces import IContained
+from zope.location.interfaces import ILocation
from zope.container.contained import notifyContainerModified
from OFS.CopySupport import CopyContainer
@@ -274,10 +277,36 @@
_checkId = checkValidId
+ def _contained(self, id, object):
+ if ILocation.providedBy(object):
+ if not ILocation.providedBy(self):
+ raise AssertionError(
+ "Cannot add an object providing ILocation to an "
+ "unlocated container."
+ )
+ if not IContained.providedBy(object):
+ alsoProvides(object, IContained)
+ oldparent = getattr(aq_base(object), '__parent__', None)
+ if aq_base(self) is not aq_base(oldparent):
+ aq_base(object).__parent__ = aq_base(self)
+ # __name__ assumed to be a property or set elsewhere
+
def _setOb(self, id, object):
+ self._contained(id, object)
setattr(self, id, object)
+ def _uncontained(self, id):
+ obj = self._getOb(id, _marker)
+ if obj is not _marker:
+ if IContained.providedBy(obj):
+ try:
+ aq_base(obj).__parent__ = None
+ except AttributeError:
+ # No need to fail if we can't set these
+ pass
+
def _delOb(self, id):
+ self._uncontained(id)
delattr(self, id)
def _getOb(self, id, default=_marker):
Modified: Zope/branches/elro-parent-pointers/src/OFS/tests/testApplication.py
===================================================================
--- Zope/branches/elro-parent-pointers/src/OFS/tests/testApplication.py 2011-10-31 23:46:54 UTC (rev 123222)
+++ Zope/branches/elro-parent-pointers/src/OFS/tests/testApplication.py 2011-11-01 00:54:04 UTC (rev 123223)
@@ -102,6 +102,12 @@
self.assertTrue(isinstance(result, NullResource))
self.assertTrue(aq_parent(aq_inner(result)) is app)
+ def test_name_parent(self):
+ app = self._makeOne()
+ self.assertEquals(app.__name__, None)
+ self.assertEquals(app.__parent__, None)
+
+
def _noWay(self, key, default=None):
raise KeyError(key)
Modified: Zope/branches/elro-parent-pointers/src/OFS/tests/testObjectManager.py
===================================================================
--- Zope/branches/elro-parent-pointers/src/OFS/tests/testObjectManager.py 2011-10-31 23:46:54 UTC (rev 123222)
+++ Zope/branches/elro-parent-pointers/src/OFS/tests/testObjectManager.py 2011-11-01 00:54:04 UTC (rev 123223)
@@ -13,6 +13,7 @@
from zExceptions import BadRequest
from zope.component.testing import PlacelessSetup
from zope.interface import implements
+from zope.location.interfaces import ILocation
from Zope2.App import zcml
from OFS.interfaces import IItem
@@ -70,6 +71,11 @@
implements(IItem)
+class ObjectManagerWithILocation(ObjectManager, SimpleItem):
+ """A located ObjectManager."""
+ implements(ILocation)
+
+
class ObjectManagerTests(PlacelessSetup, unittest.TestCase):
def setUp(self):
@@ -478,7 +484,35 @@
self.assertTrue(filename.endswith('.zexp') or
filename.endswith('.xml'))
+
+class LocatedObjectManagerTests(ObjectManagerTests):
+ def _getTargetClass(self):
+ return ObjectManagerWithILocation
+
+ def test_container_must_provide_ILocation(self):
+ unlocated = ObjectManager()
+ located = self._makeOne('located')
+ self.assertRaises( AssertionError
+ , unlocated._setObject, 'located', located )
+
+ def test_set_name_parent(self):
+ root = self._makeOne()
+ one = self._makeOne()
+ one._setId('one')
+ root._setObject('one', one, set_owner=0)
+ self.assertEquals(aq_base(root), aq_base(aq_base(one).__parent__))
+ self.assertEquals(one.__name__, 'one')
+
+ def test_delete(self):
+ root = self._makeOne()
+ one = self._makeOne('one')
+ root._setObject('one', one, set_owner=0)
+ root._delObject('one')
+ self.assertEquals(aq_base(one).__parent__, None)
+
+
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( ObjectManagerTests ) )
+ suite.addTest( unittest.makeSuite( LocatedObjectManagerTests ) )
return suite
More information about the Zope-Checkins
mailing list