def remove_ghosts(obj, event): """We are going to remove a object: we need to check if its neutral and remove their indexes also. """ if not IObjectWillBeAddedEvent.providedBy(event) \ and (IObjectWillBeMovedEvent.providedBy(event) or IObjectWillBeRemovedEvent.providedBy(event)): if not is_shared_original(obj): return content_id = IUUID(obj).split('-')[0] site = getSite() try: pc = getToolByName(site, 'portal_catalog') except AttributeError: # In case we are removing the site there is no portal_catalog return language_tool = getToolByName(site, 'portal_languages') language_infos = language_tool.supported_langs for language_info in language_infos: brains = pc.unrestrictedSearchResults( UID=content_id + '-' + language_info) for brain in brains: obj.unrestrictedTraverse(brain.getPath()).unindexObject() brains = pc.unrestrictedSearchResults( UID=content_id) for brain in brains: obj.unrestrictedTraverse(brain.getPath()).unindexObject() if IActionSucceededEvent.providedBy(event): reindex_neutral(obj, event)
def item_moved(obj, event): """OFS.item removed, cut or renamed (event also called for added and pasted)""" if (IObjectWillBeRemovedEvent.providedBy(event) # deletion or event.oldParent): # cut or rename if IProtectedItem.providedBy(obj) and not check_zope_admin(): api.portal.show_message( message=_(u"You cannot delete, cut or rename this item '${title}' !", mapping={'title': obj.Title().decode('utf8')}), request=obj.REQUEST, type='error') raise Redirect(obj.REQUEST.get('HTTP_REFERER'))
def related_change_on_moving(obj, event): """ Set local roles on related objects before moving """ if IObjectWillBeAddedEvent.providedBy(event) or IObjectWillBeRemovedEvent.providedBy(event): # not move return if event.oldParent and event.newParent and event.oldParent == event.newParent: # rename return (fti_config, fti) = fti_configuration(obj) if 'static_config' not in fti_config: return related_role_removal(obj, get_state(obj), fti_config)
def related_change_on_moving(obj, event): """ Set local roles on related objects before moving """ if IObjectWillBeAddedEvent.providedBy(event) or IObjectWillBeRemovedEvent.providedBy(event): # not move return if event.oldParent and event.newParent and event.oldParent == event.newParent: # rename return (fti_config, fti) = fti_configuration(obj) if not fti_config: return for (name, f) in get_localrole_fields(fti): if name not in fti_config: continue related_role_removal(obj, get_state(obj), fti_config[name], name)
def _update_workspace_groupings(obj, event): """ If the relevant object is inside a workspace, the workspace grouping parameters (for the sidebar) need to be updated. """ parent = parent_workspace(obj) if parent is None or not IGroupingStoragable.providedBy(parent): return storage = getAdapter(parent, IGroupingStorage) if IObjectRemovedEvent.providedBy(event) or IObjectWillBeRemovedEvent.providedBy(event): storage.remove_from_groupings(obj) else: storage.update_groupings(obj)
def related_change_on_moving(obj, event): """ Set local roles on related objects before moving """ if IObjectWillBeAddedEvent.providedBy( event) or IObjectWillBeRemovedEvent.providedBy(event): # not move return if event.oldParent and event.newParent and event.oldParent == event.newParent: # rename return (fti_config, fti) = fti_configuration(obj) if not fti_config: return for (name, f) in get_localrole_fields(fti): if name not in fti_config: continue related_role_removal(obj, get_state(obj), fti_config[name], name)
def _update_workspace_groupings(obj, event): """ If the relevant object is inside a workspace, the workspace grouping parameters (for the sidebar) need to be updated. """ parent = parent_workspace(obj) if parent is None or not IGroupingStoragable.providedBy(parent): return storage = getAdapter(parent, IGroupingStorage) if IObjectRemovedEvent.providedBy(event) or \ IObjectWillBeRemovedEvent.providedBy(event): storage.remove_from_groupings(obj) else: storage.update_groupings(obj)
def test_copying_proposals_is_prevented(self): self.login(self.administrator) create(Builder('proposal') .within(self.empty_dossier) .titled(u'My Proposal') .having(committee=self.committee.load_model())) register_event_recorder(IObjectWillBeRemovedEvent) copied_dossier = api.content.copy( source=self.empty_dossier, target=self.empty_repofolder) self.assertItemsEqual([], copied_dossier.getFolderContents()) self.assertFalse( any(IObjectWillBeRemovedEvent.providedBy(event) for event in get_recorded_events()) )
def test_copying_tasks_is_prevented(self): self.login(self.dossier_responsible) create(Builder('task') .within(self.empty_dossier) .having(responsible_client='fa', responsible=self.regular_user.getId(), issuer=self.dossier_responsible.getId())) register_event_recorder(IObjectWillBeRemovedEvent) copied_dossier = api.content.copy( source=self.empty_dossier, target=self.empty_repofolder) self.assertItemsEqual([], copied_dossier.getFolderContents()) self.assertFalse( any(IObjectWillBeRemovedEvent.providedBy(event) for event in get_recorded_events()) )
def object_will_be_moved(context, event): if IObjectWillBeAddedEvent.providedBy(event): return # Ignore object removals if IObjectWillBeRemovedEvent.providedBy(event): return # Skip automatically renamed objects during copy & paste process. if ICopyPasteRequestLayer.providedBy(getRequest()): return title = _(u'label_object_cut', default=u'Object cut: ${title}', mapping={'title': context.title_or_id()} ) journal_entry_factory( context.aq_inner.aq_parent, OBJECT_WILL_BE_MOVED_EVENT, title) return
def testDeleteEvents(self): self.assertRaises(KeyError, self.reflecto.manage_delObjects, ('monty',)) self.assertEqual(len(getEvents()), 0) self.assertRaises(KeyError, self.reflecto.manage_delObjects, ('foo', 'monty',)) self.assertEqual(len(getEvents()), 1) events = getEvents(IObjectWillBeRemovedEvent) self.assertEqual(len(events), 1) self.assertEqual(events[0].oldName, 'foo') self.assertEqual(self.reflecto.manage_delObjects(('foo',)), None) events = getEvents() self.assertEqual(len(events), 4) self.assertTrue(IObjectWillBeRemovedEvent.providedBy(events[1])) self.assertTrue(IObjectRemovedEvent.providedBy(events[2])) self.assertEqual(events[2].oldName, 'foo') self.assertTrue(IContainerModifiedEvent.providedBy(events[3])) self.assertTrue(events[3].object is self.reflecto)
def testDeleteEvents(self): self.assertRaises(KeyError, self.reflecto.manage_delObjects, ('monty', )) self.assertEqual(len(getEvents()), 0) self.assertRaises(KeyError, self.reflecto.manage_delObjects, ( 'foo', 'monty', )) self.assertEqual(len(getEvents()), 1) events = getEvents(IObjectWillBeRemovedEvent) self.assertEqual(len(events), 1) self.assertEqual(events[0].oldName, 'foo') self.assertEqual(self.reflecto.manage_delObjects(('foo', )), None) events = getEvents() self.assertEqual(len(events), 4) self.assertTrue(IObjectWillBeRemovedEvent.providedBy(events[1])) self.assertTrue(IObjectRemovedEvent.providedBy(events[2])) self.assertEqual(events[2].oldName, 'foo') self.assertTrue(IContainerModifiedEvent.providedBy(events[3])) self.assertTrue(events[3].object is self.reflecto)
def changeMailingList(ml, event): """An event listener which registers and unregisters lists with the ListLookup utility on list changes. Some framework setup: >>> from zope.app.testing.placelesssetup import setUp, tearDown >>> import Products.Five >>> from Products.Five import zcml >>> setUp() >>> zcml.load_config('meta.zcml', package=Products.Five) >>> zcml.load_config('permissions.zcml', package=Products.Five) >>> zcml.load_config("configure.zcml", package=Products.Five.site) >>> from Products.listen.utilities import tests >>> zcml.load_config('configure.zcml', package=tests) Now let's make a fake mailing list in our site >>> from zope.app.component.hooks import setSite >>> app = self.folder >>> tests.enable_local_site(app) >>> setSite(app) >>> ml = tests.install_fake_ml(app, suppress_events=True) >>> setSite(ml) Then we setup our listener >>> from zope.app.event.interfaces import IObjectModifiedEvent >>> from zope.app.container.interfaces import IObjectMovedEvent >>> from OFS.interfaces import IObjectWillBeRemovedEvent >>> from Products.listen.utilities.list_lookup import changeMailingList >>> from zope.component import handle >>> from Products.listen.interfaces import IMailingList >>> handle([IMailingList, IObjectWillBeRemovedEvent], changeMailingList) >>> handle([IMailingList, IObjectMovedEvent], changeMailingList) >>> handle([IMailingList, IObjectModifiedEvent], changeMailingList) Create and register our utility. Have to do some faking of the component registry stuff to get our mock environment to work: >>> from Products.listen.interfaces import IListLookup >>> from Products.listen.utilities.list_lookup import ListLookup >>> from Products.listen.lib.common import get_utility_for_context >>> sm = app.getSiteManager() >>> sm.registerUtility(ListLookup('list_lookup'), IListLookup) >>> tests.register_fake_component_adapter() >>> ll = get_utility_for_context(IListLookup, context=app) >>> print ll.getListForAddress(ml.mailto) None Send our added event: >>> from zope.event import notify >>> from zope.app.container.contained import ObjectAddedEvent >>> notify(ObjectAddedEvent(ml, app, 'ml')) >>> ll.getListForAddress(ml.mailto) == ml True Change the list and send an event: >>> ml.mailto = '*****@*****.**' >>> from zope.app.event.objectevent import ObjectModifiedEvent >>> notify(ObjectModifiedEvent(ml)) >>> ll.getListForAddress(ml.mailto) == ml True Send a removal event, which should do nothing, as we rely on before removal: >>> from zope.app.container.contained import ObjectRemovedEvent >>> notify(ObjectRemovedEvent(ml, app, 'ml')) >>> ll.getListForAddress(ml.mailto) == ml True Send a before removal event: >>> from OFS.event import ObjectWillBeRemovedEvent >>> notify(ObjectWillBeRemovedEvent(ml)) >>> ll.getListForAddress(ml.mailto) is None True >>> tearDown() """ # Use the new parent object as the context, unless it is unavailable, then # use the list itself parent = None if hasattr(event, 'newParent'): parent = event.newParent if parent is None: parent = ml try: ll = getUtility(IListLookup) except ComponentLookupError: # site won't be set if you delete the Plone site from the ZMI orig_site = getSite() setSite(ml) ll = getUtility(IListLookup, context=ml) setSite(orig_site) if IObjectWillBeAddedEvent.providedBy(event): # Registration is taken care of after add pass elif IObjectWillBeRemovedEvent.providedBy(event): ll.unregisterList(ml) elif IObjectWillBeMovedEvent.providedBy(event): ll.unregisterList(ml) elif IObjectRemovedEvent.providedBy(event): # Unregistration is taken care of before removal pass elif IObjectModifiedEvent.providedBy(event): if not IContainerModifiedEvent.providedBy(event): ll.updateList(ml) elif IObjectAddedEvent.providedBy(event) or IObjectCreatedEvent.providedBy( event): ll.registerList(ml) elif IObjectMovedEvent.providedBy(event): ll.registerList(ml)
def changeMailingList(ml, event): """An event listener which registers and unregisters lists with the ListLookup utility on list changes. Some framework setup: >>> from zope.app.testing.placelesssetup import setUp, tearDown >>> import Products.Five >>> from Products.Five import zcml >>> setUp() >>> zcml.load_config('meta.zcml', package=Products.Five) >>> zcml.load_config('permissions.zcml', package=Products.Five) >>> zcml.load_config("configure.zcml", package=Products.Five.site) >>> from Products.listen.utilities import tests >>> zcml.load_config('configure.zcml', package=tests) Now let's make a fake mailing list in our site >>> from zope.app.component.hooks import setSite >>> app = self.folder >>> tests.enable_local_site(app) >>> setSite(app) >>> ml = tests.install_fake_ml(app, suppress_events=True) >>> setSite(ml) Then we setup our listener >>> from zope.app.event.interfaces import IObjectModifiedEvent >>> from zope.app.container.interfaces import IObjectMovedEvent >>> from OFS.interfaces import IObjectWillBeRemovedEvent >>> from Products.listen.utilities.list_lookup import changeMailingList >>> from zope.component import handle >>> from Products.listen.interfaces import IMailingList >>> handle([IMailingList, IObjectWillBeRemovedEvent], changeMailingList) >>> handle([IMailingList, IObjectMovedEvent], changeMailingList) >>> handle([IMailingList, IObjectModifiedEvent], changeMailingList) Create and register our utility. Have to do some faking of the component registry stuff to get our mock environment to work: >>> from Products.listen.interfaces import IListLookup >>> from Products.listen.utilities.list_lookup import ListLookup >>> from Products.listen.lib.common import get_utility_for_context >>> sm = app.getSiteManager() >>> sm.registerUtility(ListLookup('list_lookup'), IListLookup) >>> tests.register_fake_component_adapter() >>> ll = get_utility_for_context(IListLookup, context=app) >>> print ll.getListForAddress(ml.mailto) None Send our added event: >>> from zope.event import notify >>> from zope.app.container.contained import ObjectAddedEvent >>> notify(ObjectAddedEvent(ml, app, 'ml')) >>> ll.getListForAddress(ml.mailto) == ml True Change the list and send an event: >>> ml.mailto = '*****@*****.**' >>> from zope.app.event.objectevent import ObjectModifiedEvent >>> notify(ObjectModifiedEvent(ml)) >>> ll.getListForAddress(ml.mailto) == ml True Send a removal event, which should do nothing, as we rely on before removal: >>> from zope.app.container.contained import ObjectRemovedEvent >>> notify(ObjectRemovedEvent(ml, app, 'ml')) >>> ll.getListForAddress(ml.mailto) == ml True Send a before removal event: >>> from OFS.event import ObjectWillBeRemovedEvent >>> notify(ObjectWillBeRemovedEvent(ml)) >>> ll.getListForAddress(ml.mailto) is None True >>> tearDown() """ # Use the new parent object as the context, unless it is unavailable, then # use the list itself parent = None if hasattr(event, 'newParent'): parent = event.newParent if parent is None: parent = ml try: ll = getUtility(IListLookup) except ComponentLookupError: # site won't be set if you delete the Plone site from the ZMI orig_site = getSite() setSite(ml) ll = getUtility(IListLookup, context=ml) setSite(orig_site) if IObjectWillBeAddedEvent.providedBy(event): # Registration is taken care of after add pass elif IObjectWillBeRemovedEvent.providedBy(event): ll.unregisterList(ml) elif IObjectWillBeMovedEvent.providedBy(event): ll.unregisterList(ml) elif IObjectRemovedEvent.providedBy(event): # Unregistration is taken care of before removal pass elif IObjectModifiedEvent.providedBy(event): if not IContainerModifiedEvent.providedBy(event): ll.updateList(ml) elif IObjectAddedEvent.providedBy(event) or IObjectCreatedEvent.providedBy(event): ll.registerList(ml) elif IObjectMovedEvent.providedBy(event): ll.registerList(ml)