def add(self, item): if not IResponse.providedBy(item): raise UnaddableError(self, item, "IResponse interface not provided.") self.append(item) id = str(len(self)) event = ObjectAddedEvent(item, newParent=self.context, newName=id) notify(event)
def _setObject(self, id, object): notify(ObjectWillBeAddedEvent(object, self, id)) self._setOb(id, object) object = self._getOb(id) if hasattr(aq_base(object), 'manage_afterAdd'): object.manage_afterAdd(object, self) notify(ObjectAddedEvent(object, self, id)) notifyContainerModified(self) return object
def _setObject(self, id, object, roles=None, user=None, set_owner=1, suppress_events=False): """Set an object into this container. Also sends IObjectAddedEvent. """ ob = object # better name, keep original function signature v = self._checkId(id) if v is not None: id = v t = getattr(ob, 'meta_type', None) # If an object by the given id already exists, remove it. for object_info in self._objects: if object_info['id'] == id: self._delObject(id) break if not suppress_events: notify(ObjectWillBeAddedEvent(ob, self, id)) self._objects = self._objects + ({'id': id, 'meta_type': t}, ) self._setOb(id, ob) ob = self._getOb(id) if set_owner: # TODO: eventify manage_fixupOwnershipAfterAdd # This will be called for a copy/clone, or a normal _setObject. ob.manage_fixupOwnershipAfterAdd() if set_owner: # Try to give user the local role "Owner", but only if # no local roles have been set on the object yet. if getattr(ob, '__ac_local_roles__', _marker) is None: user = getSecurityManager().getUser() if user is not None: userid = user.getId() if userid is not None: ob.manage_setLocalRoles(userid, ['Owner']) if not suppress_events: notify(ObjectAddedEvent(ob, self, id)) notifyContainerModified(self) compatibilityCall('manage_afterAdd', ob, ob, self) return id
def test_simulateNestedFolderCloneRemovingUid1(self): self.root._setObject('foo', PortalFolder(id='foo')) foo = self.root.foo foo._setObject('sub1', PortalFolder(id='sub1')) foo.sub1._setObject('sub2', PortalFolder(id='sub2')) foo.sub1.sub2._setObject('baz', DummyContent(id='baz', catalog=1)) baz = foo.sub1.sub2.baz annotation = self.root.portal_uidannotation(baz, UID_ATTRNAME) self.assertEqual(getattr(baz, UID_ATTRNAME), annotation) notify(ObjectAddedEvent(foo, foo)) notify(ObjectClonedEvent(foo)) self.assertRaises(AttributeError, getattr, baz, UID_ATTRNAME)
def test_notify_user(self): # dummy speaker speaker = self.create_dummy( __parent__=None, __name__=None, title="Jim", absolute_url=lambda: 'http://example.org/speaker', ) # dummy event event = ObjectAddedEvent(speaker) # search result for acl_users user_info = [{'email': '*****@*****.**', 'id': 'jim'}] # email data message = "A speaker called Jim was added here http://example.org/speaker" email = "*****@*****.**" sender = "*****@*****.**" subject = "Is this you?" # mock tools/portal portal_mock = self.mocker.mock() self.expect(portal_mock.getProperty('email_from_address')).result( '*****@*****.**') portal_url_mock = self.mocker.mock() self.mock_tool(portal_url_mock, 'portal_url') self.expect(portal_url_mock.getPortalObject()).result(portal_mock) acl_users_mock = self.mocker.mock() self.mock_tool(acl_users_mock, 'acl_users') self.expect( acl_users_mock.searchUsers(fullname='Jim')).result(user_info) mail_host_mock = self.mocker.mock() self.mock_tool(mail_host_mock, 'MailHost') self.expect(mail_host_mock.secureSend(message, email, sender, subject)) # put mock framework into replay mode self.replay() # call the method under test notifyUser(speaker, event)
def _setObject(self, id, object, roles=None, user=None, set_owner=1, suppress_events=False): ob = object # better name, keep original function signature v = self._checkId(id) if v is not None: id = v # If an object by the given id already exists, remove it. if self.has_key(id): self._delObject(id) if not suppress_events: notify(ObjectWillBeAddedEvent(ob, self, id)) self._setOb(id, ob) ob = self._getOb(id) if set_owner: # TODO: eventify manage_fixupOwnershipAfterAdd # This will be called for a copy/clone, or a normal _setObject. ob.manage_fixupOwnershipAfterAdd() # Try to give user the local role "Owner", but only if # no local roles have been set on the object yet. if getattr(ob, '__ac_local_roles__', _marker) is None: user = getSecurityManager().getUser() if user is not None: userid = user.getId() if userid is not None: ob.manage_setLocalRoles(userid, ['Owner']) if not suppress_events: notify(ObjectAddedEvent(ob, self, id)) notifyContainerModified(self) OFS.subscribers.compatibilityCall('manage_afterAdd', ob, ob, self) return id
def _constructInstance(self, container, id, *args, **kw): """Build a bare instance of the appropriate type. Does not do any security checks. """ constructor = self.restrictedTraverse(self.constructor_path) # make sure ownership is explicit before switching the context if not hasattr(aq_base(constructor), '_owner'): constructor._owner = aq_get(constructor, '_owner') # Rewrap to get into container's context. constructor = aq_base(constructor).__of__(container) id = str(id) obj = constructor(container, id, *args, **kw) if hasattr(obj, '_setPortalTypeName'): obj._setPortalTypeName(self.getId()) notify(ObjectAddedEvent(obj, container, obj.getId())) notifyContainerModified(container) return obj
def _constructInstance(self, container, id, *args, **kw): """Build a bare instance of the appropriate type. Does not do any security checks. """ # XXX: this method violates the rules for tools/utilities: # it depends on self.REQUEST id = str(id) if self.product: # oldstyle factory m = self._getFactoryMethod(container, check_security=0) if getattr(aq_base(m), 'isDocTemp', 0): kw['id'] = id newid = m(m.aq_parent, self.REQUEST, *args, **kw) else: newid = m(id, *args, **kw) # allow factory to munge ID newid = newid or id obj = container._getOb(newid) if hasattr(obj, '_setPortalTypeName'): obj._setPortalTypeName(self.getId()) notify(ObjectCreatedEvent(obj)) notify(ObjectAddedEvent(obj, container, newid)) notifyContainerModified(container) else: # newstyle factory factory = getUtility(IFactory, self.factory) obj = factory(id, *args, **kw) if hasattr(obj, '_setPortalTypeName'): obj._setPortalTypeName(self.getId()) notify(ObjectCreatedEvent(obj)) rval = container._setObject(id, obj) newid = isinstance(rval, basestring) and rval or id obj = container._getOb(newid) return obj