make comments viewlet show the plone.app.discussion comments and allow to add new comments. (redirection after add-comment is not working yet)

svn path=/plone.app.discussion/trunk/; revision=27112
This commit is contained in:
Timo Stollenwerk 2009-05-25 18:59:25 +00:00
parent 15ba11faa7
commit ad06e9a7e6
3 changed files with 143 additions and 120 deletions

View File

@ -1,46 +1,90 @@
<tal:block define="userHasReplyPermission view/can_reply;
isDiscussionAllowed view/is_discussion_allowed;
replies view/get_replies;
isAnon view/is_anonymous"
<tal:block define="errors options/state/getErrors|nothing;
replies view/replies | nothing;"
i18n:domain="plone">
<div class="discussion"
tal:condition="python:replies or (userHasReplyPermission and isDiscussionAllowed) or (isAnon and not userHasReplyPermission and isDiscussionAllowed)">
<p>plone.app.discussion viewlet:</p>
<form name="reply"
action=""
method="post"
tal:condition="python:userHasReplyPermission and isDiscussionAllowed"
tal:attributes="action string:${context/absolute_url}/discussion_reply_form">
<input class="standalone"
style="margin-bottom: 1.25em;"
type="submit"
value="Add Comment"
i18n:attributes="value label_add_comment;"
/>
</form>
<form tal:condition="python:isAnon and not userHasReplyPermission and isDiscussionAllowed"
tal:attributes="action view/login_action">
<input class="standalone"
style="margin-bottom: 1.25em;"
type="submit"
value="Log in to add comments"
i18n:attributes="value label_login_to_add_comments;"
/>
</form>
<div class="reply">
<fieldset>
<legend i18n:translate="legend_add_comment">Add comment</legend>
<p i18n:translate="description_add_comment">
You can add a comment by filling out the form below. Plain text
formatting.
</p>
<form name="reply"
action=""
method="get"
tal:attributes="action string:${context/absolute_url}/++conversation++default/@@add-comment">
<div class="field"
tal:define="error errors/subject|nothing;"
tal:attributes="class python: error and 'field error' or 'field'">
<label for="subject" i18n:translate="label_subject">Subject</label>
<span class="fieldRequired" title="Required"
i18n:attributes="title title_required;"
i18n:translate="label_required">(Required)</span>
<div tal:content="error">Validation error output</div>
<input name="subject"
id="subject"
value=""
size="40"
tal:attributes="value request/subject|request/title_override|nothing;" />
</div>
<div class="field"
tal:define="error errors/body_text|nothing;"
tal:attributes="class python: error and 'field error' or 'field'">
<label for="body_text" i18n:translate="label_comment">Comment</label>
<span class="fieldRequired" title="Required"
i18n:attributes="title title_required;"
i18n:translate="label_required">(Required)</span>
<div tal:content="error">Validation error output</div>
<textarea name="body_text"
id="body_text"
cols="40"
rows="8"
tal:content="request/body_text|request/text_override | nothing"></textarea>
</div>
<div class="formControls">
<input class="context"
type="submit"
value="Add Comment"
name="form.button.AddComment"
i18n:attributes="value label_add_comment;" />
</div>
<input type="hidden" name="form.submitted" value="1" />
</form>
</fieldset>
</div>
<div class="discussion">
<tal:getreplies repeat="reply_dict replies">
<div class="comment" style=""
tal:define="indent python:reply_dict['depth']*2;
reply python:reply_dict['object']"
tal:attributes="style string:margin-left:${indent}em;">
<div class="comment"
tal:define="reply python:reply_dict[1]">
<h3>
<a name="comments" tal:attributes="name reply/id">
<span tal:replace="reply/pretty_title_or_id">Comment title</span>
<a name="comments" tal:attributes="name reply/title">
<span tal:replace="reply/title">Comment title</span>
</a>
</h3>
<div class="documentByLine"
<!--<div class="documentByLine"
tal:define="creator reply/Creator;
anonymous_creator python:creator in ('Anonymous User', '');
mi python:not anonymous_creator and view.member_info(creator);
@ -52,39 +96,16 @@
condition="anonymous_creator">Anonymous User</tal:name>
<tal:at i18n:translate="label_commented_at">at</tal:at>
<span tal:replace="python:view.format_time(reply.ModificationDate())">8/23/2001 12:40:44 PM</span>
</div>
</div>-->
<div class="commentBody"
tal:content="structure reply/CookedBody">
tal:content="structure reply/text">
This is the body text of the comment.
</div>
<form name="reply"
action="discussion_reply_form"
method="post"
style="display: inline;"
tal:attributes="action string:${reply/absolute_url}/discussion_reply_form"
tal:condition="python:userHasReplyPermission and isDiscussionAllowed">
<input type="hidden"
name="subject"
tal:attributes="value reply/pretty_title_or_id" />
<input class="standalone"
type="submit"
value="Reply"
i18n:attributes="value label_reply;"
/>
</form>
<form name="delete"
action=""
method="post"
style="display: inline;"
tal:condition="view/can_manage"
tal:attributes="action string:${reply/absolute_url}/deleteDiscussion">
<input class="destructive"
type="submit"
value="Remove"
i18n:attributes="value label_remove;"
/>
</form>
</div>
</tal:getreplies>
</div>
</tal:block>

View File

@ -1,70 +1,63 @@
from urllib import quote as url_quote
from zope.interface import implements
from zope.component import getMultiAdapter
from zope.viewlet.interfaces import IViewlet
from Acquisition import aq_inner, aq_parent
from AccessControl import getSecurityManager
from Acquisition import aq_inner
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.CMFCore.utils import getToolByName
from Products.CMFDefault.DiscussionTool import DiscussionNotAllowed
from plone.app.layout.viewlets.common import ViewletBase
from plone.app.discussion.interfaces import IComment
from plone.app.discussion.conversation import conversationAdapterFactory
class CommentsViewlet(ViewletBase):
index = ViewPageTemplateFile('comments.pt')
from plone.app.discussion.comment import CommentFactory
from zope.component import createObject
class CommentsViewlet(BrowserView):
"""Discussion Viewlet
"""
implements(IViewlet)
template = ViewPageTemplateFile('comments.pt')
def __init__(self, context, request, view, manager):
super(CommentsViewlet, self).__init__(context, request)
self.__parent__ = view
self.view = view
self.manager = manager
self.portal_state = getMultiAdapter((context, self.request), name=u"plone_portal_state")
def update(self):
super(CommentsViewlet, self).update()
self.portal_discussion = getToolByName(self.context, 'portal_discussion', None)
self.portal_membership = getToolByName(self.context, 'portal_membership', None)
pass
def can_reply(self):
return getSecurityManager().checkPermission('Reply to item', aq_inner(self.context))
def replies(self):
conversation = conversationAdapterFactory(self.context)
return conversation.items()
def is_discussion_allowed(self):
if self.portal_discussion is None:
return False
else:
return self.portal_discussion.isDiscussionAllowedFor(aq_inner(self.context))
class AddComment(BrowserView):
"""Add a comment to a conversation
"""
def get_replies(self):
replies = []
def __call__(self):
context = aq_inner(self.context)
container = aq_parent(context)
pd = self.portal_discussion
if self.request.has_key('form.button.AddComment'):
def getRs(obj, replies, counter):
rs = pd.getDiscussionFor(obj).getReplies()
if len(rs) > 0:
rs.sort(lambda x, y: cmp(x.modified(), y.modified()))
for r in rs:
replies.append({'depth':counter, 'object':r})
getRs(r, replies, counter=counter + 1)
subject = self.request.get('subject')
text = self.request.get('body_text')
try:
getRs(context, replies, 0)
except DiscussionNotAllowed:
# We tried to get discussions for an object that has not only
# discussions turned off but also no discussion container.
return []
return replies
# The add-comment view is called on the conversation object
conversation = self.context
def is_anonymous(self):
return self.portal_state.anonymous()
# Create the comment
comment = CommentFactory()
comment.title = subject
comment.text = text
def login_action(self):
return '%s/login_form?came_from=%s' % (self.navigation_root_url, url_quote(self.request.get('URL', '')),)
# Add comment to the conversation
conversation.addComment(comment)
def can_manage(self):
return getSecurityManager().checkPermission('Manage portal', aq_inner(self.context))
def member_info(self, creator):
if self.portal_membership is None:
return None
else:
return self.portal_membership.getMemberInfo(creator)
def format_time(self, time):
context = aq_inner(self.context)
util = getToolByName(context, 'translation_service')
return util.ulocalized_time(time, 1, context, domain='plonelocales')
# TODO: Redirect to the document object page
# conversation.__parent__ is not working?!
#self.request.response.redirect(aq_inner(self.context).absolute_url())

View File

@ -18,4 +18,13 @@
permission="zope2.View"
/>
<!-- add-comment view -->
<browser:view
name="add-comment"
for="plone.app.discussion.interfaces.IConversation"
layer="..interfaces.IDiscussionLayer"
class=".comments.AddComment"
permission="zope2.View"
/>
</configure>