Exemplo n.º 1
0
    def test_order_events(self):
        # Prepare the setup::
        from zope.container.contained import ContainerModifiedEvent

        # Prepare some objects::

        from zope.container.ordered import OrderedContainer
        oc = OrderedContainer()
        oc['foo'] = 'bar'
        oc['baz'] = 'quux'
        oc['zork'] = 'grue'
        self.assertEqual(oc.keys(),
                         ['foo', 'baz', 'zork'])

        # Now change the order::

        clearEvents()
        oc.updateOrder(['baz', 'foo', 'zork'])
        self.assertEqual(oc.keys(),
                         ['baz', 'foo', 'zork'])

        # Check what events have been sent::

        events = getEvents()
        self.assertEqual(1, len(events))
        self.assertIsInstance(events[0], ContainerModifiedEvent)

        # This is in fact a specialized modification event::

        from zope.lifecycleevent.interfaces import IObjectModifiedEvent
        self.assertTrue(IObjectModifiedEvent.providedBy(events[0]))
Exemplo n.º 2
0
    def __init__(self, manager=u'', category=u'', name=u''):
        # XXX: This depends on implementation detail in OrderedContainer,
        # but it uses a PersistentDict, which sucks :-/
        OrderedContainer.__init__(self)
        self._data = OOBTree()

        self.__manager__ = manager
        self.__category__ = category
        self.__name__ = name
    def test_namechooser_rename_preserve_order(self):
        # Test for OrderedContainerItemRenamer.renameItem

        # This is a regression test for
        # http://www.zope.org/Collectors/Zope3-dev/658

        # Also: https://bugs.launchpad.net/zope.copypastemove/+bug/98385
        provideAdapter(ObjectMover)

        # There's an ordered container

        from zope.container.ordered import OrderedContainer
        container = OrderedContainer()

        class Obj(Contained):
            def __init__(self, title):
                self.title = title

        objects = [Obj('Foo'), Obj('Bar'), Obj('Baz')]
        container['foo'] = objects[0]
        container['bar'] = objects[1]
        container['baz'] = objects[2]

        # with a custom name chooser

        import codecs
        from zope.interface import implementer, Interface
        from zope.container.interfaces import INameChooser
        class IMyContainer(Interface):
            "An interface"
        @adapter(IMyContainer)
        @implementer(INameChooser)
        class MyNameChooser(object):
            def __init__(self, container):
                self.container = container
            def chooseName(self, name, obj):
                return codecs.getencoder('rot-13')(name)[0]
        provideAdapter(MyNameChooser)

        from zope.interface import alsoProvides
        alsoProvides(container, IMyContainer)

        # OrderedContainerItemRenamer renames and preserves the order of items

        from zope.copypastemove import OrderedContainerItemRenamer
        renamer = OrderedContainerItemRenamer(container)
        self.assertEqual(renamer.renameItem('bar', 'quux'),
                         u'dhhk')

        self.assertEqual(list(container.keys()),
                         [u'foo', u'dhhk', u'baz'])
        self.assertEqual(container.values(),
                         objects)
Exemplo n.º 4
0
class RelationshipStates(Persistent, Contained):
    implements(IRelationshipStates)

    title = None
    states = None
    system_titles = None
    factory = RelationshipState

    def __init__(self, title):
        self.title = title
        self.states = OrderedContainer()
        locate(self.states, self, 'states')
        self.system_titles = OrderedContainer()
        locate(self.system_titles, self, 'system_titles')

    def __iter__(self):
        return iter(self.states.values())

    def getState(self, state_tuple):
        if state_tuple is None:
            return None
        meaning, code = state_tuple
        state = self.states.get(code)
        return state

    def getTitle(self, state_tuple):
        state = self.getState(state_tuple)
        if state is None:
            return None
        return state.title

    def getDescription(self, state_tuple):
        if state_tuple is None:
            return None
        meaning, code = state_tuple
        state = self.system_titles.get(meaning)
        return state

    @classmethod
    def overlap(cls, codes, other):
        for code in codes:
            if code in other:
                return True
        return False

    def add(self, *args, **kw):
        state = self.factory(*args, **kw)
        self.states[state.code] = state

    def describe(self, active, title):
        active = ''.join(sorted(set(active)))
        self.system_titles[active] = title
Exemplo n.º 5
0
class RelationshipStates(Persistent, Contained):
    implements(IRelationshipStates)

    title = None
    states = None
    system_titles = None
    factory = RelationshipState

    def __init__(self, title):
        self.title = title
        self.states = OrderedContainer()
        locate(self.states, self, 'states')
        self.system_titles = OrderedContainer()
        locate(self.system_titles, self, 'system_titles')

    def __iter__(self):
        return iter(self.states.values())

    def getState(self, state_tuple):
        if state_tuple is None:
            return None
        meaning, code = state_tuple
        state = self.states.get(code)
        return state

    def getTitle(self, state_tuple):
        state = self.getState(state_tuple)
        if state is None:
            return None
        return state.title

    def getDescription(self, state_tuple):
        if state_tuple is None:
            return None
        meaning, code = state_tuple
        state = self.system_titles.get(meaning)
        return state

    @classmethod
    def overlap(cls, codes, other):
        for code in codes:
            if code in other:
                return True
        return False

    def add(self, *args, **kw):
        state = self.factory(*args, **kw)
        self.states[state.code] = state

    def describe(self, active, title):
        active = ''.join(sorted(set(active)))
        self.system_titles[active] = title
Exemplo n.º 6
0
    def test_order_adding_none(self):
        # This is a regression test: adding None to an OrderedContainer
        # used to corrupt its internal data structure (_order and _data
        # would get out of sync, causing KeyErrors when you tried to iterate).

        from zope.container.ordered import OrderedContainer
        oc = OrderedContainer()
        oc['foo'] = None
        self.assertEqual(oc.keys(), ['foo'])
        self.assertEqual(oc.values(), [None])
        self.assertEqual(oc.items(), [('foo', None)])
        # None got proxied, so 'is None is not true
        self.assertIsNotNone(oc['foo'])
        self.assertEqual(None, oc['foo'])
Exemplo n.º 7
0
    def test_order_adding_none(self):
        # This is a regression test: adding None to an OrderedContainer
        # used to corrupt its internal data structure (_order and _data
        # would get out of sync, causing KeyErrors when you tried to iterate).

        from zope.container.ordered import OrderedContainer
        oc = OrderedContainer()
        oc['foo'] = None
        self.assertEqual(oc.keys(), ['foo'])
        self.assertEqual(oc.values(), [None])
        self.assertEqual(oc.items(),
                         [('foo', None)])
        # None got proxied, so 'is None is not true
        self.assertIsNotNone(oc['foo'])
        self.assertEqual(None, oc['foo'])
Exemplo n.º 8
0
    def test_deny_dublincore_view(self):
        """Tests the denial of dublincore view permissions to anonymous.

        Users who can view a folder contents page but cannot view dublin core
        should still be able to see the folder items' names, but not their
        title, modified, and created info.
        """
        # add an item that can be viewed from the root folder
        obj = OrderedContainer()
        alsoProvides(obj, IAttributeAnnotatable)

        self.getRootFolder()['obj'] = obj
        IZopeDublinCore(obj).title = u'My object'

        # deny zope.app.dublincore.view to zope.Anonymous
        prm = IRolePermissionManager(self.getRootFolder())
        prm.denyPermissionToRole('zope.dublincore.view', 'zope.Anonymous')
        # Try both spellings just in case we are used with an older zope.dc
        prm.denyPermissionToRole('zope.app.dublincore.view', 'zope.Anonymous')
        transaction.commit()

        response = self.publish('/')
        self.assertEquals(response.getStatus(), 200)
        body = response.getBody()

        # confirm we can see the file name
        self.assert_(body.find('<a href="obj">obj</a>') != -1)

        # confirm we *cannot* see the metadata title
        self.assert_(body.find('My object') == -1)
Exemplo n.º 9
0
    def test_order_events(self):
        # Prepare the setup::
        from zope.container.contained import ContainerModifiedEvent

        # Prepare some objects::

        from zope.container.ordered import OrderedContainer
        oc = OrderedContainer()
        oc['foo'] = 'bar'
        oc['baz'] = 'quux'
        oc['zork'] = 'grue'
        self.assertEqual(oc.keys(), ['foo', 'baz', 'zork'])

        # Now change the order::

        clearEvents()
        oc.updateOrder(['baz', 'foo', 'zork'])
        self.assertEqual(oc.keys(), ['baz', 'foo', 'zork'])

        # Check what events have been sent::

        events = getEvents()
        self.assertEqual(1, len(events))
        self.assertIsInstance(events[0], ContainerModifiedEvent)

        # This is in fact a specialized modification event::

        from zope.lifecycleevent.interfaces import IObjectModifiedEvent
        self.assertTrue(IObjectModifiedEvent.providedBy(events[0]))
Exemplo n.º 10
0
    def test_order_updateOrder_bytes(self):
        # https://github.com/zopefoundation/zope.container/issues/21
        from zope.container.ordered import OrderedContainer
        keys = [u'a', u'b']
        oc = OrderedContainer()
        oc[keys[0]] = 0
        oc[keys[1]] = 1

        self.assertEqual(keys, oc.keys())

        # Updating with bytes keys...
        oc.updateOrder((b'b', b'a'))
        # still produces text keys
        text_type = str if str is not bytes else unicode
        self.assertEqual(list(reversed(keys)), oc.keys())
        self.assertIsInstance(oc.keys()[0], text_type)
Exemplo n.º 11
0
 def listItems(self):
     # Now this is tricky: we want to construct a small object graph using
     # old state pickles without ever calling __setstate__ on a real
     # Persistent object, as _that_ would poison ZODB in-memory caches
     # in a nasty way (LP #487243).
     container = OrderedContainer()
     container.__setstate__(self.state)
     if isinstance(container._data, PersistentDict):
         old_data_state = IObjectHistory(container._data).loadState(
             self.tid)
         container._data = PersistentDict()
         container._data.__setstate__(old_data_state)
     if isinstance(container._order, PersistentList):
         old_order_state = IObjectHistory(container._order).loadState(
             self.tid)
         container._order = PersistentList()
         container._order.__setstate__(old_order_state)
     return container.items()
Exemplo n.º 12
0
    def test_order_all_items_available_at_object_added_event(self):
        # Now register an event subscriber to object added events.
        import zope.component
        from zope.lifecycleevent.interfaces import IObjectAddedEvent

        keys = []

        @zope.component.adapter(IObjectAddedEvent)
        def printContainerKeys(event):
            keys.extend(event.newParent.keys())

        zope.component.provideHandler(printContainerKeys)

        # Now we are adding an object to the container.

        from zope.container.ordered import OrderedContainer
        oc = OrderedContainer()
        oc['foo'] = 'FOO'

        self.assertEqual(keys, ['foo'])
Exemplo n.º 13
0
    def test_default_view_permissions(self):
        """Tests the default view permissions.
        """
        # add an item that can be viewed from the root folder
        obj = OrderedContainer()
        alsoProvides(obj, IAttributeAnnotatable)

        self.getRootFolder()['obj'] = obj
        IZopeDublinCore(obj).title = u'My object'
        transaction.commit()

        response = self.publish('/')
        self.assertEquals(response.getStatus(), 200)
        body = response.getBody()

        # confirm we can see the file name
        self.assert_(body.find('<a href="obj">obj</a>') != -1)

        # confirm we can see the metadata title
        self.assert_(body.find('<td><span>My object</span></td>') != -1)
Exemplo n.º 14
0
 def listItems(self):
     # Now this is tricky: we want to construct a small object graph using
     # old state pickles without ever calling __setstate__ on a real
     # Persistent object, as _that_ would poison ZODB in-memory caches
     # in a nasty way (LP #487243).
     container = OrderedContainer()
     container.__setstate__(self.state)
     if isinstance(container._data, PersistentDict):
         old_data_state = IObjectHistory(container._data).loadState(self.tid)
         container._data = PersistentDict()
         container._data.__setstate__(old_data_state)
     if isinstance(container._order, PersistentList):
         old_order_state = IObjectHistory(container._order).loadState(self.tid)
         container._order = PersistentList()
         container._order.__setstate__(old_order_state)
     return container.items()
Exemplo n.º 15
0
    def test_order_updateOrder_bytes(self):
        # https://github.com/zopefoundation/zope.container/issues/21
        from zope.container.ordered import OrderedContainer
        keys = [u'a', u'b']
        oc = OrderedContainer()
        oc[keys[0]] = 0
        oc[keys[1]] = 1

        self.assertEqual(keys, oc.keys())

        # Updating with bytes keys...
        oc.updateOrder((b'b', b'a'))
        # still produces text keys
        text_type = str if str is not bytes else unicode
        self.assertEqual(list(reversed(keys)), oc.keys())
        self.assertIsInstance(oc.keys()[0], text_type)
Exemplo n.º 16
0
 def __init__(self):
     # XXX: This depends on implementation detail in OrderedContainer,
     # but it uses a PersistentDict, which sucks :-/
     OrderedContainer.__init__(self)
     self._data = OOBTree()
Exemplo n.º 17
0
 def __init__(self, title=u''):
     OrderedContainer.__init__(self)
     self.title = title
Exemplo n.º 18
0
 def __init__(self, title):
     self.title = title
     self.states = OrderedContainer()
     locate(self.states, self, 'states')
     self.system_titles = OrderedContainer()
     locate(self.system_titles, self, 'system_titles')
Exemplo n.º 19
0
 def __init__(self, title=u''):
     OrderedContainer.__init__(self)
     self.title = title
Exemplo n.º 20
0
 def __init__(self):
     # XXX: This depends on implementation detail in OrderedContainer,
     # but it uses a PersistentDict, which sucks :-/
     OrderedContainer.__init__(self)
     self._data = OOBTree()
Exemplo n.º 21
0
 def __init__(self, title):
     self.title = title
     self.states = OrderedContainer()
     locate(self.states, self, 'states')
     self.system_titles = OrderedContainer()
     locate(self.system_titles, self, 'system_titles')
Exemplo n.º 22
0
 def __init__(self):
     OrderedContainer.__init__(self)
     self._data = OOBTree()
Exemplo n.º 23
0
 def __init__(self):
     OrderedContainer.__init__(self)
     self._data=OOBTree()