Plain text to HTML transformation added for comment text

svn path=/plone.app.discussion/trunk/; revision=38898
This commit is contained in:
Timo Stollenwerk 2010-08-25 09:19:28 +00:00
parent 5a379b7c4f
commit 35d7743b7a
4 changed files with 59 additions and 11 deletions

View File

@ -4,6 +4,9 @@ Changelog
1.0b7 (unreleased) 1.0b7 (unreleased)
------------------ ------------------
* Plain text to HTML transformation added for comment text.
[timo]
* Rewrote all tal:condition in comments.pt. The authenticated user has * Rewrote all tal:condition in comments.pt. The authenticated user has
the reply button and the comment form if he has the "Reply to item" the reply button and the comment form if he has the "Reply to item"
permission And the discussion is currently allowed. permission And the discussion is currently allowed.

View File

@ -81,7 +81,7 @@
</div> </div>
<div class="commentBody" <div class="commentBody"
tal:content="reply/getText" /> tal:content="structure python:view.cook(reply.getText())" />
<div class="commentActions"> <div class="commentActions">
<button class="context reply-to-comment-button hide allowMultiSubmit" <button class="context reply-to-comment-button hide allowMultiSubmit"

View File

@ -173,9 +173,10 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
comment_id = conversation.addComment(comment) comment_id = conversation.addComment(comment)
# If a user post a comment and moderation is enabled, a message is shown # If a user post a comment and moderation is enabled, a message is shown
# to the user that his/her comment awaits moderation. If the user has manage # to the user that his/her comment awaits moderation. If the user has
# right, he/she is redirected directly to the comment. # manage right, he/she is redirected directly to the comment.
can_manage = getSecurityManager().checkPermission('Manage portal', context) can_manage = getSecurityManager().checkPermission('Manage portal',
context)
if wf.getChainForPortalType('Discussion Item') == \ if wf.getChainForPortalType('Discussion Item') == \
('comment_review_workflow',) and not can_manage: ('comment_review_workflow',) and not can_manage:
# Show info message when comment moderation is enabled # Show info message when comment moderation is enabled
@ -209,11 +210,22 @@ class CommentsViewlet(ViewletBase):
# view methods # view methods
def cook(self, text):
transforms = getToolByName(self, 'portal_transforms')
targetMimetype = 'text/html'
mimetype = 'text/plain'
return transforms.convertTo(targetMimetype,
text,
context=self,
mimetype=mimetype).getData()
def can_reply(self): def can_reply(self):
return getSecurityManager().checkPermission('Reply to item', aq_inner(self.context)) return getSecurityManager().checkPermission('Reply to item',
aq_inner(self.context))
def can_manage(self): def can_manage(self):
return getSecurityManager().checkPermission('Manage portal', aq_inner(self.context)) return getSecurityManager().checkPermission('Manage portal',
aq_inner(self.context))
def is_discussion_allowed(self): def is_discussion_allowed(self):
context = aq_inner(self.context) context = aq_inner(self.context)
@ -268,7 +280,7 @@ class CommentsViewlet(ViewletBase):
r = r.copy() r = r.copy()
r['workflow_status'] = workflow_status r['workflow_status'] = workflow_status
yield r yield r
# Return all direct replies # Return all direct replies
if conversation.total_comments > 0: if conversation.total_comments > 0:
if workflow_actions: if workflow_actions:
@ -288,7 +300,9 @@ class CommentsViewlet(ViewletBase):
# return the default user image if no username is given # return the default user image if no username is given
return 'defaultUser.gif' return 'defaultUser.gif'
else: else:
portal_membership = getToolByName(self.context, 'portal_membership', None) portal_membership = getToolByName(self.context,
'portal_membership',
None)
return portal_membership.getPersonalPortrait(username).absolute_url(); return portal_membership.getPersonalPortrait(username).absolute_url();
def anonymous_discussion_allowed(self): def anonymous_discussion_allowed(self):
@ -304,15 +318,23 @@ class CommentsViewlet(ViewletBase):
return settings.show_commenter_image return settings.show_commenter_image
def is_anonymous(self): def is_anonymous(self):
portal_membership = getToolByName(self.context, 'portal_membership', None) portal_membership = getToolByName(self.context,
'portal_membership',
None)
return portal_membership.isAnonymousUser() return portal_membership.isAnonymousUser()
def login_action(self): def login_action(self):
return '%s/login_form?came_from=%s' % (self.navigation_root_url, url_quote(self.request.get('URL', '')),) return '%s/login_form?came_from=%s' % (self.navigation_root_url,
url_quote(self.request.get('URL', '')),)
def format_time(self, time): def format_time(self, time):
# We have to transform Python datetime into Zope DateTime # We have to transform Python datetime into Zope DateTime
# before we can call toLocalizedTime. # before we can call toLocalizedTime.
util = getToolByName(self.context, 'translation_service') util = getToolByName(self.context, 'translation_service')
zope_time = DateTime(time.year, time.month, time.day, time.hour, time.minute, time.second) zope_time = DateTime(time.year,
time.month,
time.day,
time.hour,
time.minute,
time.second)
return util.toLocalizedTime(zope_time, long_format=True) return util.toLocalizedTime(zope_time, long_format=True)

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import unittest import unittest
from datetime import datetime from datetime import datetime
@ -106,6 +107,28 @@ class TestCommentsViewlet(PloneTestCase):
context = getattr(self.portal, 'doc1') context = getattr(self.portal, 'doc1')
self.viewlet = CommentsViewlet(context, request, None, None) self.viewlet = CommentsViewlet(context, request, None, None)
def test_cook(self):
text = """First paragraph
Second paragraph"""
self.assertEquals(self.viewlet.cook(text),
"<p>First paragraph<br /> <br /> Second paragraph</p>")
def test_cook_no_html(self):
text = """<b>Got HTML?</b>"""
self.assertEquals(self.viewlet.cook(text),
"<p>&lt;b&gt;Got HTML?&lt;/b&gt;</p>")
def test_cook_with_no_ascii_characters(self):
text = """Umlaute sind ä, ö und ü."""
self.assertEquals(self.viewlet.cook(text),
"<p>Umlaute sind \xc3\xa4, \xc3\xb6 und \xc3\xbc.</p>")
def test_cook_links(self):
text = "Go to http://www.plone.org"
self.assertEquals(self.viewlet.cook(text),
"<p>Go to http://www.plone.org</p>")
def test_can_reply(self): def test_can_reply(self):
# Portal owner can reply # Portal owner can reply
self.failUnless(self.viewlet.can_reply()) self.failUnless(self.viewlet.can_reply())