Commit d00be8da authored by Florent Guillaume's avatar Florent Guillaume

ObjectManager now has an hasObject method to test presence. This

brings it in line with BTreeFolder.
parent f27a9754
...@@ -26,6 +26,9 @@ Zope Changes ...@@ -26,6 +26,9 @@ Zope Changes
Features added Features added
- ObjectManager now has an hasObject method to test presence. This
brings it in line with BTreeFolder.
- Using FastCGI is officially deprecated - Using FastCGI is officially deprecated
- Improved logging of ConflictErrors. All conflict errors are - Improved logging of ConflictErrors. All conflict errors are
......
...@@ -273,6 +273,20 @@ class ObjectManager( ...@@ -273,6 +273,20 @@ class ObjectManager(
raise AttributeError, id raise AttributeError, id
return default return default
def hasObject(self, id):
"""Indicate whether the folder has an item by ID.
This doesn't try to be more intelligent than _getOb, and doesn't
consult _objects (for performance reasons). The common use case
is to check that an object does *not* exist.
"""
if (id in ('.', '..') or
id.startswith('_') or
id.startswith('aq_') or
id.endswith('__')):
return False
return getattr(aq_base(self), id, None) is not None
def _setObject(self, id, object, roles=None, user=None, set_owner=1, def _setObject(self, id, object, roles=None, user=None, set_owner=1,
suppress_events=False): suppress_events=False):
"""Set an object into this container. """Set an object into this container.
......
...@@ -531,6 +531,10 @@ class IObjectManager(IZopeObject, ICopyContainer, INavigation, IManageable, ...@@ -531,6 +531,10 @@ class IObjectManager(IZopeObject, ICopyContainer, INavigation, IManageable,
""" """
""" """
def hasObject(id):
"""Indicate whether the folder has an item by ID.
"""
def objectIds(spec=None): def objectIds(spec=None):
"""List the IDs of the subobjects of the current object. """List the IDs of the subobjects of the current object.
......
...@@ -328,6 +328,22 @@ class ObjectManagerTests(PlacelessSetup, unittest.TestCase): ...@@ -328,6 +328,22 @@ class ObjectManagerTests(PlacelessSetup, unittest.TestCase):
om2._setObject(ob.getId(), ob) om2._setObject(ob.getId(), ob)
self.assertRaises(DeleteFailed, om1._delObject, 'om2') self.assertRaises(DeleteFailed, om1._delObject, 'om2')
def test_hasObject(self):
om = self._makeOne()
self.failIf(om.hasObject('_properties'))
self.failIf(om.hasObject('_getOb'))
self.failIf(om.hasObject('__of__'))
self.failIf(om.hasObject('.'))
self.failIf(om.hasObject('..'))
self.failIf(om.hasObject('aq_base'))
om.zap__ = True
self.failIf(om.hasObject('zap__'))
self.failIf(om.hasObject('foo'))
si = SimpleItem('foo')
om._setObject('foo', si)
self.assert_(om.hasObject('foo'))
om._delObject('foo')
self.failIf(om.hasObject('foo'))
def test_setObject_checkId_ok(self): def test_setObject_checkId_ok(self):
om = self._makeOne() om = self._makeOne()
......
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