From c47689af42e4645710175c2097507e4eb49ff122 Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Thu, 12 Jul 2012 10:26:39 +0200 Subject: [PATCH] Fix handling of comments with invalid transforms. Write an error msg to the log and just return the untransformed text. --- CHANGES.txt | 4 ++++ plone/app/discussion/comment.py | 20 ++++++++++++++++---- plone/app/discussion/tests/test_comment.py | 10 ++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 0fe1700..af03b6f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -21,6 +21,10 @@ Changelog default type is set properly when creating a new comment. [timo] +- Fix handling of comments with invalid transforms. Write an error msg + to the log and just return the untransformed text. + [timo] + 2.1.8 (unreleased) ------------------ diff --git a/plone/app/discussion/comment.py b/plone/app/discussion/comment.py index 7b4c12c..171f4df 100644 --- a/plone/app/discussion/comment.py +++ b/plone/app/discussion/comment.py @@ -142,10 +142,22 @@ class Comment(CatalogAware, WorkflowAware, DynamicType, Traversable, return '' if isinstance(text, unicode): text = text.encode('utf8') - return transforms.convertTo(targetMimetype, - text, - context=self, - mimetype=sourceMimetype).getData() + transform = transforms.convertTo( + targetMimetype, + text, + context=self, + mimetype=sourceMimetype) + if transform: + return transform.getData() + else: + logger = logging.getLogger("plone.app.discussion") + logger.error( + _(u"Transform '%s' => '%s' not available. Failed to transform comment '%s'." % ( + sourceMimetype, + targetMimetype, + self.absolute_url(), + ))) + return text def Title(self): """The title of the comment. diff --git a/plone/app/discussion/tests/test_comment.py b/plone/app/discussion/tests/test_comment.py index 3afe87e..5a4deb1 100644 --- a/plone/app/discussion/tests/test_comment.py +++ b/plone/app/discussion/tests/test_comment.py @@ -177,6 +177,16 @@ class CommentTest(unittest.TestCase): comment1.text = 'para' self.assertEqual(comment1.getText(targetMimetype='text/plain'), 'para') + def test_getText_invalid_transformation_raises_error(self): + conversation = IConversation(self.portal.doc1) + comment1 = createObject('plone.Comment') + comment1.mime_type = 'text/x-html-safe' + comment1.text = 'para' + conversation.addComment(comment1) + self.assertEqual( + comment1.getText(targetMimetype='text/html'), + 'para') + def test_traversal(self): # make sure comments are traversable, have an id, absolute_url and # physical path