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 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 modifiedDynamicPropertiesItem(obj, event): if not IObjectRemovedEvent.providedBy(event): #a DynamicPropertiesItem was added l_dp_dict = {} lang = obj.gl_get_selected_language() for dp in obj.getDynamicProperties(): l_dp_dict[dp.id] = dp.defaultvalue for item in obj.getCatalogedObjects(obj.id): item.createDynamicProperties(l_dp_dict, lang)
def modifiedDynamicPropertiesItem(obj, event): if not IObjectRemovedEvent.providedBy(event): #a DynamicPropertiesItem was added l_dp_dict = {} lang = obj.gl_get_selected_language() for dp in obj.getDynamicProperties(): l_dp_dict[dp.id] = dp.defaultvalue for item in obj.getCatalogedObjects(obj.id): item.createDynamicProperties(l_dp_dict, lang)
def handleContentishEvent(ob, event): """ Event subscriber for (IContentish, IObjectEvent) events. """ from zope.app.container.interfaces import IObjectRemovedEvent if IObjectAddedEvent.providedBy(event): if event.newParent is not None: ob.indexObject() elif IObjectClonedEvent.providedBy(event): ob.notifyWorkflowCreated() elif IObjectRemovedEvent.providedBy(event): ob.unindexObject() elif IObjectMovedEvent.providedBy(event): if event.newParent is not None: uid = getattr(ob, "_v_old_path", None) if uid is not None: new_uid = "/".join(ob.getPhysicalPath()) # print 'moved from', uid del ob._v_old_path catalog = getToolByName(ob, "portal_catalog")._catalog index = catalog.uids.get(uid) if index is not None: catalog.uids[new_uid] = index catalog.paths[index] = new_uid del catalog.uids[uid] ob.reindexObject( idxs=( "modified", "allowedRolesAndUsers", "effective", "effectiveRange", "expires", "getId", "id", "lastModified", "path", "Date", "review_state", ) ) return ob.indexObject() elif IObjectWillBeMovedEvent.providedBy(event): if event.oldParent is not None: try: ob._v_old_path = "/".join(ob.getPhysicalPath()) except: ob.unindexObject()
def logEvent(self, ob, event): """ """ msg = ["uid: %s" % ob.UID(), "portal_type: %s" % ob.portal_type] if IAfterTransitionEvent.providedBy(event): if not event.transition: return # Ignore null transitions msg.append("transition: %s" % event.transition.id) if IObjectRemovedEvent.providedBy(event): msg.append("removed: 1\nid: %s\n" % ob.getId()) else: marshaller = ob.Schema().getLayerImpl("marshall") msg.append(marshaller.marshall(ob)[2]) logger.info("\n".join(msg))
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 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 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 SearchableListMoved(ml, event): if not IObjectRemovedEvent.providedBy(event): # don't bother with this stuff on removal, it's pointless search = get_utility_for_context(ISearchableArchive, context=ml) # XXX: Assumes a ZCatalog based utility, we need to rebuild the whole # catalog after a move # Clear the catalog search.manage_catalogClear() # Add the moved messages to the catalog, then reindex again to make sure # that we don't get any threading inconsistencies. index_meth = search.catalog_object path = '/'.join(ml.getPhysicalPath()) search.ZopeFindAndApply(ml, obj_metatypes=('MailingListMessage',), apply_func=index_meth, apply_path=path, search_sub=1) search.refreshCatalog()
def logEvent(self,ob,event): """ """ msg = ['uid: %s' % ob.UID(), 'portal_type: %s' % ob.portal_type, ] if IAfterTransitionEvent.providedBy(event): if not event.transition: return # Ignore null transitions msg.append( "transition: %s" % event.transition.id ) if IObjectRemovedEvent.providedBy(event): msg.append( 'removed: 1\nid: %s\n' % ob.getId() ) else: marshaller = ob.Schema().getLayerImpl('marshall') msg.append( marshaller.marshall(ob)[2] ) logger.info('\n'.join(msg))
def handleContentishEvent(ob, event): """ Event subscriber for (IContentish, IObjectEvent) events. """ from zope.app.container.interfaces import IObjectRemovedEvent if IObjectAddedEvent.providedBy(event): if event.newParent is not None: ob.indexObject() elif IObjectClonedEvent.providedBy(event): ob.notifyWorkflowCreated() elif IObjectRemovedEvent.providedBy(event): ob.unindexObject() elif IObjectMovedEvent.providedBy(event): if event.newParent is not None: uid = getattr(ob, '_v_old_path', None) if uid is not None: new_uid = '/'.join(ob.getPhysicalPath()) #print 'moved from', uid del ob._v_old_path catalog = getToolByName(ob, 'portal_catalog')._catalog index = catalog.uids.get(uid) if index is not None: catalog.uids[new_uid] = index catalog.paths[index] = new_uid del catalog.uids[uid] ob.reindexObject(idxs=('modified', 'allowedRolesAndUsers', 'effective', 'effectiveRange', 'expires', 'getId', 'id', 'lastModified', 'path', 'Date', 'review_state')) return ob.indexObject() elif IObjectWillBeMovedEvent.providedBy(event): if event.oldParent is not None: try: ob._v_old_path = '/'.join(ob.getPhysicalPath()) except: ob.unindexObject()
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 fixOwnership(ob, event): if not IObjectRemovedEvent.providedBy(event): ob.fixOwnership()
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)