def test_dict_operations(self): # test dict operations and acquisition wrapping # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in comment1 = createObject('plone.Comment') comment1.text = 'Comment text' new_id1 = conversation.addComment(comment1) comment2 = createObject('plone.Comment') comment2.text = 'Comment text' new_id2 = conversation.addComment(comment2) # check if get returns a comment object, and None if the key # can not be found self.assertTrue(IComment.providedBy(conversation.get(new_id1))) self.assertTrue(IComment.providedBy(conversation.get(new_id2))) self.assertEqual(conversation.get(123), None) # check if keys return the ids of all comments self.assertEqual(len(conversation.keys()), 2) self.assertTrue(new_id1 in conversation.keys()) self.assertTrue(new_id2 in conversation.keys()) self.assertFalse(123 in conversation.keys()) # check if items returns (key, comment object) pairs self.assertEqual(len(conversation.items()), 2) self.assertTrue((new_id1, comment1) in conversation.items()) self.assertTrue((new_id2, comment2) in conversation.items()) # check if values returns the two comment objects self.assertEqual(len(conversation.values()), 2) self.assertTrue(comment1 in conversation.values()) self.assertTrue(comment2 in conversation.values()) # check if comment ids are in iterkeys self.assertTrue(new_id1 in conversation.iterkeys()) self.assertTrue(new_id2 in conversation.iterkeys()) self.assertFalse(123 in conversation.iterkeys()) # check if comment objects are in itervalues self.assertTrue(comment1 in conversation.itervalues()) self.assertTrue(comment2 in conversation.itervalues()) # check if iteritems returns (key, comment object) pairs self.assertTrue((new_id1, comment1) in conversation.iteritems()) self.assertTrue((new_id2, comment2) in conversation.iteritems())
def test_add_comment(self): # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in comment = createObject('plone.Comment') comment.text = 'Comment text' new_id = conversation.addComment(comment) # Check that the conversation methods return the correct data self.assertTrue(isinstance(comment.comment_id, long)) self.assertTrue(IComment.providedBy(conversation[new_id])) self.assertEqual( aq_base(conversation[new_id].__parent__), aq_base(conversation) ) self.assertEqual(new_id, comment.comment_id) self.assertEqual(len(list(conversation.getComments())), 1) self.assertEqual(len(tuple(conversation.getThreads())), 1) self.assertEqual(conversation.total_comments(), 1) self.assertTrue( conversation.last_comment_date - datetime.utcnow() < timedelta(seconds=1) )
def test_traversal(self): # make sure comments are traversable, have an id, absolute_url and # physical path conversation = IConversation(self.portal.doc1) comment1 = createObject('plone.Comment') comment1.text = 'Comment text' new_comment1_id = conversation.addComment(comment1) comment = self.portal.doc1.restrictedTraverse( '++conversation++default/{0}'.format(new_comment1_id) ) self.assertTrue(IComment.providedBy(comment)) self.assertEqual( ( '', 'plone', 'doc1', '++conversation++default', str(new_comment1_id) ), comment.getPhysicalPath() ) self.assertEqual( 'http://nohost/plone/doc1/++conversation++default/' + str(new_comment1_id), comment.absolute_url() )
def getSetter(self, obj, indexName): """Gets the setter function for the field based on the index name. Returns None if it can't get the function """ fieldName = self.fieldNameForIndex(indexName) field = None if IComment.providedBy(obj): #Discussion field = getattr(obj, 'getField', None) else: #Archetype field = getattr(aq_base(obj), 'getField', None) # Archetypes: if field: fieldObj = field(fieldName) or field(fieldName.lower()) if not fieldObj and fieldName.startswith('get'): fieldName = fieldName.lstrip('get_') fieldName = fieldName[0].lower() + fieldName[1:] fieldObj = obj.getField(fieldName) if fieldObj is not None: return fieldObj.getMutator(obj) return None # DefaultDublinCoreImpl: setterName = 'set' + indexName if getattr(aq_base(obj), setterName, None) is not None: return getattr(obj, setterName) # Dexterity if IDexterityContent.providedBy(obj): if fieldName.startswith('get'): fieldName = fieldName.lstrip('get_') fieldName = fieldName[0].lower() + fieldName[1:] return lambda value: setattr(obj, fieldName, value) return None
def test_traversal(self): # make sure comments are traversable, have an id, absolute_url and # physical path conversation = IConversation(self.portal.doc1) comment1 = createObject('plone.Comment') comment1.text = 'Comment text' new_comment1_id = conversation.addComment(comment1) comment = self.portal.doc1.restrictedTraverse( '++conversation++default/{0}'.format(new_comment1_id), ) self.assertTrue(IComment.providedBy(comment)) self.assertEqual( ( '', 'plone', 'doc1', '++conversation++default', str(new_comment1_id), ), comment.getPhysicalPath(), ) self.assertEqual( 'http://nohost/plone/doc1/++conversation++default/' + str(new_comment1_id), comment.absolute_url(), )
def test_add_comment(self): # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in comment = createObject('plone.Comment') comment.text = 'Comment text' new_id = conversation.addComment(comment) # Check that the conversation methods return the correct data self.assertTrue(isinstance(comment.comment_id, int)) self.assertTrue(IComment.providedBy(conversation[new_id])) self.assertEqual( aq_base(conversation[new_id].__parent__), aq_base(conversation), ) self.assertEqual(new_id, comment.comment_id) self.assertEqual(len(list(conversation.getComments())), 1) self.assertEqual(len(tuple(conversation.getThreads())), 1) self.assertEqual(conversation.total_comments(), 1) self.assertTrue( conversation.last_comment_date - datetime.utcnow() < timedelta(seconds=1), )
def test_migrate_comment_with_creator(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') reply.author_username = '******' reply.email = '*****@*****.**' self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) self.assertEqual(reply.author_username, 'Jim') self.assertEqual(reply.email, '*****@*****.**') # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc) ) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual( comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6) ) self.assertEqual( comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7) ) self.assertEqual([ {'comment': comment1, 'depth': 0, 'id': long(comment1.id)} ], list(conversation.getThreads())) self.assertFalse(self.doc.talkback) # Though this should be Jimmy, but looks like getProperty won't pick # up 'author_username' (reply.author_username is not None), so it's # propagating Creator()..? self.assertEqual(comment1.author_username, 'Jim') self.assertEqual(comment1.author_name, 'Jimmy Jones') self.assertEqual(comment1.author_email, '*****@*****.**')
def test_migrate_comment_with_creator(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') reply.author_username = '******' reply.email = '*****@*****.**' self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) self.assertEqual(reply.author_username, 'Jim') self.assertEqual(reply.email, '*****@*****.**') # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc)) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual(comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6)) self.assertEqual(comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7)) self.assertEqual([{ 'comment': comment1, 'depth': 0, 'id': long(comment1.id) }], list(conversation.getThreads())) self.assertFalse(self.doc.talkback) # Though this should be Jimmy, but looks like getProperty won't pick # up 'author_username' (reply.author_username is not None), so it's # propagating Creator()..? self.assertEqual(comment1.author_username, 'Jim') self.assertEqual(comment1.author_name, 'Jimmy Jones') self.assertEqual(comment1.author_email, '*****@*****.**')
def copied(event): """When an object is copied, execute rules assigned to its parent """ obj = event.object if not (IContentish.providedBy(obj) or IComment.providedBy(obj)): return if is_portal_factory(obj): return execute(aq_parent(aq_inner(event.original)), event)
def moved_event(event): # only execute moved event if it's not a added or removed event since # those are handled elsewhere and they base off of this event class if (IObjectAddedEvent.providedBy(event) or IObjectRemovedEvent.providedBy(event)): return obj = event.object if not (IContentish.providedBy(obj) or IComment.providedBy(obj)): return execute_event(event)
def removed(event): """When an IObjectRemovedEvent was received, execute rules assigned to its previous parent. """ obj = event.object if not (IContentish.providedBy(obj) or IComment.providedBy(obj)): return if is_portal_factory(obj): return execute(event.oldParent, event)
def modified(event): """When an object is modified, execute rules assigned to its parent """ obj = event.object if not (IContentish.providedBy(obj) or IComment.providedBy(obj)): return # Let the special handler take care of IObjectInitializedEvent for event_if in (IObjectInitializedEvent, IObjectAddedEvent, IObjectRemovedEvent, IContainerModifiedEvent, IObjectCopiedEvent): if event_if.providedBy(event): return execute_rules(event)
def test_migrate_comment(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc) ) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual( comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6) ) self.assertEqual( comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7) ) self.assertEqual([ {'comment': comment1, 'depth': 0, 'id': long(comment1.id)} ], list(conversation.getThreads())) self.assertFalse(self.doc.talkback)
def modified(event): """When an object is modified, fire the ParentModifiedEvent for its direct descendents. """ obj = event.object if not (IContentish.providedBy(obj) or IComment.providedBy(obj) or IFolderish.providedBy(obj)): return # IObjectModified event is called when a site is created, but before the # catalog has been initialised which throws an AttributeError exception. try: children = obj.getFolderContents() except AttributeError: return for child in children: notify(ParentModifiedEvent(child.getObject()))
def test_migrate_comment(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc)) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual(comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6)) self.assertEqual(comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7)) self.assertEqual([{ 'comment': comment1, 'depth': 0, 'id': long(comment1.id) }], list(conversation.getThreads())) self.assertFalse(self.doc.talkback)
def added(event): """When an object is added, execute rules assigned to its new parent. There is special handling for Archetypes objects. """ obj = event.object if is_portal_factory(obj): return # The object added event executes too early for Archetypes objects. # We need to delay execution until we receive a subsequent # IObjectInitializedEvent if IBaseObject.providedBy(obj): init() _status.delayed_events['IObjectInitializedEvent-%s' % _get_uid(obj)] = event elif IContentish.providedBy(obj) or IComment.providedBy(obj): execute(event.newParent, event) else: return
def created_event(event): obj = event.object if is_portal_factory(obj): return if IObjectCopiedEvent.providedBy(event): return # ignore this event since we're listening to cloned instead # The object added event executes too early for Archetypes objects. # We need to delay execution until we receive a subsequent # IObjectInitializedEvent if IBaseObject.providedBy(obj): cr_handlers.init() cr_handlers._status.delayed_events[ 'IObjectInitializedEvent-audit-%s' % getUID(obj)] = event elif IContentish.providedBy(obj) or IComment.providedBy(obj): execute_event(event) else: return
def test_factory(self): comment1 = createObject('plone.Comment') self.assertTrue(IComment.providedBy(comment1))