def afterAdd(self, event): """ This method is called, whenever _setObject in ObjectManager gets called. This is the case after a normal add and if the object is a result of cut-paste- or rename-operation. In the first case, the external files doesn't exist yet, otherwise it was renamed to .undo by beforeDelete before and must be restored by _undo(). Subscriber for (IExtFile, IObjectMovedEvent) """ from zope.app.container.interfaces import IObjectAddedEvent from zope.app.container.interfaces import IObjectRemovedEvent # If this is a Removed event we are done if IObjectRemovedEvent.providedBy(event): return # The disk file has been renamed to .undo by beforeDelete self._undo(self.filename) # If this is an Added event we are done if IObjectAddedEvent.providedBy(event): return # We have been moved, also move the disk file fn = self._get_fsname(self.filename) if fn: self._register() # Register with TM try: new_fn = self._get_new_ufn() self._update_data(fn, self._temp_fsname(new_fn)) self._delete(self.filename) self.filename = new_fn finally: self._dir__unlock()
def test_events(self): from OFS.interfaces import IObjectWillBeAddedEvent from zope.app.container.interfaces import IContainerModifiedEvent from zope.app.container.interfaces import IObjectAddedEvent from zope.component import adapter from zope.component import provideHandler from zope.lifecycleevent.interfaces import IObjectCreatedEvent events = [] @adapter(IObjectCreatedEvent) def _handleObjectCreated(event): events.append(event) provideHandler(_handleObjectCreated) @adapter(IObjectWillBeAddedEvent) def _handleObjectWillBeAdded(event): events.append(event) provideHandler(_handleObjectWillBeAdded) @adapter(IObjectAddedEvent) def _handleObjectAdded(event): events.append(event) provideHandler(_handleObjectAdded) @adapter(IContainerModifiedEvent) def _handleContainerModified(event): events.append(event) provideHandler(_handleContainerModified) self.ti.constructInstance(self.f, 'foo') self.assertEquals(len(events), 4) evt = events[0] self.failUnless(IObjectCreatedEvent.providedBy(evt)) self.assertEquals(evt.object, self.f.foo) evt = events[1] self.failUnless(IObjectWillBeAddedEvent.providedBy(evt)) self.assertEquals(evt.object, self.f.foo) self.assertEquals(evt.oldParent, None) self.assertEquals(evt.oldName, None) self.assertEquals(evt.newParent, self.f) self.assertEquals(evt.newName, 'foo') evt = events[2] self.failUnless(IObjectAddedEvent.providedBy(evt)) self.assertEquals(evt.object, self.f.foo) self.assertEquals(evt.oldParent, None) self.assertEquals(evt.oldName, None) self.assertEquals(evt.newParent, self.f) self.assertEquals(evt.newName, 'foo') evt = events[3] self.failUnless(IContainerModifiedEvent.providedBy(evt)) self.assertEquals(evt.object, self.f)
def modifiedNyContainer(obj, event): if not IObjectRemovedEvent.providedBy(event): #a NyContainer was added to a container obj.catalogNyObject(obj) if not IObjectRemovedEvent.providedBy(event) and \ not IObjectAddedEvent.providedBy(event): #a NyContainer was moved. obj.catalogNyObject(obj)
def object_moved(context, event): # Since IObjectAddedEvent subclasses IObjectMovedEvent this event # handler is also called for IObjectAddedEvent but we should not # do anything in this case. if IObjectAddedEvent.providedBy(event): return title = _(u"label_object_moved", default=u"Object moved: ${title}", mapping={"title": context.title_or_id()}) journal_entry_factory(context.aq_inner.aq_parent, OBJECT_MOVED_EVENT, title) return
def object_moved(context, event): # Since IObjectAddedEvent subclasses IObjectMovedEvent this event # handler is also called for IObjectAddedEvent but we should not # do anything in this case. if IObjectAddedEvent.providedBy(event): return title = _(u'label_object_moved', default=u'Object moved: ${title}', mapping={'title': context.title_or_id()}) journal_entry_factory(context.aq_inner.aq_parent, OBJECT_MOVED_EVENT, title) return
def movedNySite(ob, event): """ A NySite was moved """ if IObjectAddedEvent.providedBy(event): #Hadled by addedNySite return elif IObjectRemovedEvent.providedBy(event): #Hadled by removedNySite return else: old_handle = ob.meta_type + '/' + event.oldName BeforeTraverse.unregisterBeforeTraverse(ob, old_handle) handle = ob.meta_type + '/' + event.newName nc = BeforeTraverse.NameCaller(event.newName) BeforeTraverse.registerBeforeTraverse(ob, nc, handle)
def handleContentishEvent(ob, event): """ Event subscriber for (IContentish, IObjectEvent) events. """ if IObjectAddedEvent.providedBy(event): if event.newParent is not None: ob.indexObject() elif IObjectClonedEvent.providedBy(event): ob.notifyWorkflowCreated() elif IObjectMovedEvent.providedBy(event): if event.newParent is not None: ob.indexObject() elif IObjectWillBeMovedEvent.providedBy(event): if event.oldParent is not None: ob.unindexObject()
def afterAddNyFSFile(obj, event): """ Added """ ext_file = obj.get_data(as_string=False) ext_file_id = ext_file.getId() if IObjectRemovedEvent.providedBy(event): _handle_obj_version(obj, ObjectRemovedEvent, obj) _handle_obj_versions(obj, ObjectRemovedEvent, obj) return zope_event.notify(ObjectRemovedEvent(ext_file, obj)) if IObjectAddedEvent.providedBy(event): _handle_obj_version(obj, ObjectAddedEvent, obj, 'version') _handle_obj_versions(obj, ObjectAddedEvent, obj, 'versions') return zope_event.notify(ObjectAddedEvent(ext_file, obj, ext_file_id)) _handle_obj_version(obj, ObjectMovedEvent, obj, 'version', obj, 'version') _handle_obj_versions(obj, ObjectMovedEvent, obj, 'versions', obj, 'versions') zope_event.notify(ObjectMovedEvent( ext_file, obj, ext_file_id, obj, ext_file_id))
def handleOpaqueItemEvent(ob, event): """ Event subscriber for (ICallableOpaqueItemEvents, IObjectEvent) events. """ if IObjectAddedEvent.providedBy(event): if event.newParent is not None: ob.manage_afterAdd(ob, event.newParent) elif IObjectClonedEvent.providedBy(event): ob.manage_afterClone(ob) elif IObjectMovedEvent.providedBy(event): if event.newParent is not None: ob.manage_afterAdd(ob, event.newParent) elif IObjectWillBeMovedEvent.providedBy(event): if event.oldParent is not None: ob.manage_beforeDelete(ob, event.oldParent)
def afterAdd(self, event): """ This method is called, whenever _setObject in ObjectManager gets called. This is the case after a normal add and if the object is a result of cut-paste- or rename-operation. In the first case, the external files doesn't exist yet, otherwise it was renamed to .undo by beforeDelete before and must be restored by _undo(). Subscriber for (IExtImage, IObjectMovedEvent) """ from zope.app.container.interfaces import IObjectAddedEvent from zope.app.container.interfaces import IObjectRemovedEvent # If this is a Removed event we are done if IObjectRemovedEvent.providedBy(event): return # The preview file has been renamed to .undo by beforeDelete if self.has_preview and self.filename != self.prev_filename: self._undo(self.prev_filename) # If this is an Added event we are done if IObjectAddedEvent.providedBy(event): return # We have been moved, also move the preview file if self.has_preview and self.filename != self.prev_filename: fn = self._get_fsname(self.prev_filename) if fn: self._register() # Register with TM try: new_fn = self._get_new_ufn(content_type=self.prev_content_type) self._update_data(fn, self._temp_fsname(new_fn)) self.prev_filename = new_fn old_fn = fn[:-4] # Chop off .tmp try: os.rename(fn, old_fn + '.undo') except OSError: logger.error('afterAdd', exc_info=True) finally: self._dir__unlock() elif not self.has_preview: self.prev_filename = []
def afterAdd(self, event): """ This method is called, whenever _setObject in ObjectManager gets called. This is the case after a normal add and if the object is a result of cut-paste- or rename-operation. In the first case, the external files doesn't exist yet, otherwise it was renamed to .undo by beforeDelete before and must be restored by _undo(). Subscriber for (IExtImage, IObjectMovedEvent) """ from zope.app.container.interfaces import IObjectAddedEvent from zope.app.container.interfaces import IObjectRemovedEvent # If this is a Removed event we are done if IObjectRemovedEvent.providedBy(event): return # The preview file has been renamed to .undo by beforeDelete if self.has_preview and self.filename != self.prev_filename: self._undo(self.prev_filename) # If this is an Added event we are done if IObjectAddedEvent.providedBy(event): return # We have been moved, also move the preview file if self.has_preview and self.filename != self.prev_filename: fn = self._get_fsname(self.prev_filename) if fn: self._register() # Register with TM try: new_fn = self._get_new_ufn(content_type=self.prev_content_type) self._update_data(fn, self._temp_fsname(new_fn)) self.prev_filename = new_fn old_fn = fn[:-4] # Chop off .tmp try: os.rename(fn, old_fn+'.undo') except OSError: logger.error('afterAdd', exc_info=True) finally: self._dir__unlock() elif not self.has_preview: self.prev_filename = []
def handleUidAnnotationEvent(ob, event): """ Event subscriber for (IUniqueIdAnnotation, IObjectEvent) events """ if IObjectAddedEvent.providedBy(event): if event.newParent is not None: anno_tool = queryUtility(IUniqueIdAnnotationManagement) uid_handler = getToolByName(ob, 'portal_uidhandler', None) if anno_tool is not None: remove_on_add = anno_tool.getProperty('remove_on_add', False) remove_on_clone = anno_tool.getProperty( 'remove_on_clone', False) assign_on_add = anno_tool.getProperty('assign_on_add', False) if (remove_on_add and remove_on_clone) or assign_on_add: try: uid_handler.unregister(ob) except UniqueIdError: # did not have one pass if assign_on_add: # assign new uid uid_handler.register(ob) elif IObjectClonedEvent.providedBy(event): anno_tool = queryUtility(IUniqueIdAnnotationManagement) uid_handler = getToolByName(ob, 'portal_uidhandler', None) if anno_tool is not None: remove_on_clone = anno_tool.getProperty('remove_on_clone', False) assign_on_clone = anno_tool.getProperty('assign_on_clone', False) if remove_on_clone or assign_on_clone: try: uid_handler.unregister(ob) except UniqueIdError: # did not have one pass if assign_on_clone: # assign new uid uid_handler.register(ob)
def handleUidAnnotationEvent(ob, event): """ Event subscriber for (IUniqueIdAnnotation, IObjectEvent) events """ if IObjectAddedEvent.providedBy(event): if event.newParent is not None: anno_tool = queryUtility(IUniqueIdAnnotationManagement) uid_handler = getToolByName(ob, 'portal_uidhandler', None) if anno_tool is not None: remove_on_add = anno_tool.getProperty('remove_on_add',False) remove_on_clone = anno_tool.getProperty('remove_on_clone',False) assign_on_add = anno_tool.getProperty('assign_on_add',False) if (remove_on_add and remove_on_clone) or assign_on_add: try: uid_handler.unregister(ob) except UniqueIdError: # did not have one pass if assign_on_add: # assign new uid uid_handler.register(ob) elif IObjectClonedEvent.providedBy(event): anno_tool = queryUtility(IUniqueIdAnnotationManagement) uid_handler = getToolByName(ob, 'portal_uidhandler', None) if anno_tool is not None: remove_on_clone = anno_tool.getProperty('remove_on_clone', False) assign_on_clone = anno_tool.getProperty('assign_on_clone', False) if remove_on_clone or assign_on_clone: try: uid_handler.unregister(ob) except UniqueIdError: # did not have one pass if assign_on_clone: # assign new uid uid_handler.register(ob)
def handleContentishEvent(ob, event): """ Event subscriber for (IContentish, IObjectEvent) events. """ if IObjectAddedEvent.providedBy(event): ob.notifyWorkflowCreated() ob.indexObject() elif IObjectMovedEvent.providedBy(event): if event.newParent is not None: ob.indexObject() elif IObjectWillBeMovedEvent.providedBy(event): if event.oldParent is not None: ob.unindexObject() elif IObjectCopiedEvent.providedBy(event): if hasattr(aq_base(ob), 'workflow_history'): del ob.workflow_history elif IObjectCreatedEvent.providedBy(event): if hasattr(aq_base(ob), 'addCreator'): ob.addCreator()
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)