get_commenter_portrait and get_commenter_home_url methods added to comments viewlet.

tests for get_commenter_portrait added.

svn path=/plone.app.discussion/trunk/; revision=27349
This commit is contained in:
Timo Stollenwerk 2009-06-07 20:58:41 +00:00
parent dada11c43c
commit 1af2b435fd
3 changed files with 69 additions and 10 deletions

View File

@ -26,16 +26,16 @@
tal:define="reply reply_dict/comment; tal:define="reply reply_dict/comment;
depth reply_dict/depth|python:0; depth reply_dict/depth|python:0;
creator reply/Creator; creator reply/Creator;
author_home_url string:${context/portal_url}/author/${reply/author_username}; commenter_home_url python:view.get_commenter_home_url(reply.author_username);
portrait python: here.portal_membership.getPersonalPortrait(reply.author_username); portrait python:view.get_commenter_portrait(reply.author_username);
anonymous_creator python:creator in ('Anonymous User', '');" anonymous_creator python:creator in ('Anonymous User', '');"
tal:attributes="class python:'comment replyTreeLevel'+str(depth); tal:attributes="class python:'comment replyTreeLevel'+str(depth);
style string:margin-left: ${depth}em; style string:margin-left: ${depth}em;
id string:comment-${reply/id}"> id string:comment-${reply/id}">
<div class="commentImage" tal:condition="not:anonymous_creator"> <div class="commentImage" tal:condition="not:anonymous_creator">
<a href="" tal:condition="not:isAnon" <a href="" tal:condition="python: commenter_home_url and not isAnon"
tal:attributes="href author_home_url"> tal:attributes="href commenter_home_url">
<img src="defaultUser.gif" <img src="defaultUser.gif"
alt="" alt=""
border="0" border="0"
@ -47,7 +47,7 @@
alt="" alt=""
border="0" border="0"
width="75" width="75"
tal:condition="isAnon" tal:condition="python: isAnon or not commenter_home_url"
tal:attributes="src portrait/absolute_url; tal:attributes="src portrait/absolute_url;
alt reply/creator" /> alt reply/creator" />
</div> </div>
@ -64,7 +64,7 @@
<a href="" <a href=""
tal:condition="not:isAnon" tal:condition="not:isAnon"
tal:content="creator" tal:content="creator"
tal:attributes="href author_home_url"> tal:attributes="href commenter_home_url">
Poster Name Poster Name
</a> </a>
<span tal:condition="isAnon" <span tal:condition="isAnon"

View File

@ -67,6 +67,19 @@ class CommentsViewlet(ViewletBase):
else: else:
return False return False
def get_commenter_home_url(self, username):
if username is None:
return None
else:
return "%s/author/%s" % (self.context.portal_url(), username)
def get_commenter_portrait(self, username):
if username is None:
return None
else:
return self.portal_membership.getPersonalPortrait(username);
def is_anonymous(self): def is_anonymous(self):
return self.portal_state.anonymous() return self.portal_state.anonymous()
@ -78,7 +91,7 @@ class CommentsViewlet(ViewletBase):
return time.strftime("%a, %d %b %Y %H:%M") return time.strftime("%a, %d %b %Y %H:%M")
# XXX: Not working, returns None !!! # XXX: Not working, returns None !!!
#return self.context.restrictedTraverse('@@plone').toLocalizedTime(time, long_format=True) #return self.context.restrictedTraverse('@@plone').toLocalizedTime(time, long_format=True)
class AddComment(BrowserView): class AddComment(BrowserView):
"""Add a comment to a conversation """Add a comment to a conversation
""" """

View File

@ -7,9 +7,13 @@ from zope.component import createObject
from Acquisition import aq_base, aq_parent, aq_inner from Acquisition import aq_base, aq_parent, aq_inner
from OFS.Image import Image
from plone.app.vocabularies.types import BAD_TYPES from plone.app.vocabularies.types import BAD_TYPES
from Products.CMFCore.FSImage import FSImage
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.tests import dummy
from Products.PloneTestCase.ptc import PloneTestCase from Products.PloneTestCase.ptc import PloneTestCase
from plone.app.discussion.browser.comments import CommentsViewlet from plone.app.discussion.browser.comments import CommentsViewlet
@ -22,11 +26,12 @@ class CommentsViewletTest(PloneTestCase):
layer = DiscussionLayer layer = DiscussionLayer
def afterSetUp(self): def afterSetUp(self):
# First we need to create some content.
self.loginAsPortalOwner() self.loginAsPortalOwner()
typetool = self.portal.portal_types typetool = self.portal.portal_types
typetool.constructContent('Document', self.portal, 'doc1') typetool.constructContent('Document', self.portal, 'doc1')
self.portal_discussion = getToolByName(self.portal, 'portal_discussion', None) self.portal_discussion = getToolByName(self.portal, 'portal_discussion', None)
self.membership_tool = getToolByName(self.folder, 'portal_membership')
self.memberdata = self.portal.portal_memberdata
request = self.app.REQUEST request = self.app.REQUEST
context = getattr(self.portal, 'doc1') context = getattr(self.portal, 'doc1')
self.viewlet = CommentsViewlet(context, request, None, None) self.viewlet = CommentsViewlet(context, request, None, None)
@ -35,12 +40,53 @@ class CommentsViewletTest(PloneTestCase):
pass pass
def test_get_commenter_portrait(self): def test_get_commenter_portrait(self):
# Add a user with a member image
self.membership_tool.addMember('jim', 'Jim', ['Member'], [])
self.memberdata._setPortrait(Image(id='jim', file=dummy.File(), title=''), 'jim')
self.assertEqual(self.memberdata._getPortrait('jim').getId(), 'jim')
self.assertEqual(self.memberdata._getPortrait('jim').meta_type, 'Image')
# Add a conversation with a comment
conversation = IConversation(self.portal.doc1)
conversation = conversation.__of__(self.portal.doc1)
comment = createObject('plone.Comment')
comment.title = 'Comment 1'
comment.text = 'Comment text'
comment.Creator = 'Jim'
comment.author_username = 'jim'
new_id = conversation.addComment(comment)
# Call get_commenter_portrait method of the viewlet
self.viewlet.update() self.viewlet.update()
self.viewlet.get_commenter_portrait('Foo') portrait = self.viewlet.get_commenter_portrait('jim')
# Check if the correct member image is returned
self.assert_(isinstance(portrait, Image))
self.assertEquals(portrait.absolute_url(), 'http://nohost/plone/portal_memberdata/portraits/jim')
def test_get_commenter_portrait_without_userimage(self): def test_get_commenter_portrait_without_userimage(self):
# Create a user without a user image # Create a user without a user image
pass self.membership_tool.addMember('jim', 'Jim', ['Member'], [])
# Add a conversation with a comment
conversation = IConversation(self.portal.doc1)
conversation = conversation.__of__(self.portal.doc1)
comment = createObject('plone.Comment')
comment.title = 'Comment 1'
comment.text = 'Comment text'
comment.Creator = 'Jim'
comment.author_username = 'jim'
new_id = conversation.addComment(comment)
# Call get_commenter_portrait method of the viewlet
self.viewlet.update()
portrait = self.viewlet.get_commenter_portrait('jim')
# Check if the correct default member image is returned
self.assert_(isinstance(portrait, FSImage))
self.assertEquals(portrait.absolute_url(), 'http://nohost/plone/defaultUser.gif')
def test_get_commenter_home(self): def test_get_commenter_home(self):
pass pass