move reply to comment view to z3cform.

svn path=/plone.app.discussion/trunk/; revision=28349
This commit is contained in:
Timo Stollenwerk 2009-08-04 14:51:34 +00:00
parent a5c8fe33ed
commit ddd8596dbd
4 changed files with 99 additions and 135 deletions

View File

@ -154,7 +154,7 @@
</p>
<div tal:replace="structure view/contents" />
<!--
<form name="reply"
action=""
method="get"
@ -218,6 +218,7 @@
<input type="hidden" name="form.submitted" value="1" />
</form>
-->
</fieldset>
</div>

View File

@ -77,7 +77,6 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
'__parent__',
'__name__',
'comment_id',
'in_reply_to',
'mime_type',
'creator',
'creation_date',
@ -86,51 +85,106 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
'author_name',
'author_email',)
@button.buttonAndHandler(u'Post comment')
@button.buttonAndHandler(u'Comment')
def handleApply(self, action):
data, errors = self.extractData()
if data.has_key('title'):
subject = data['title']
if data.has_key('text'):
if data.has_key('title') and data.has_key('text'):
title = data['title']
text = data['text']
if data.has_key('author_username'):
author_username = data['author_username']
if data.has_key('author_email'):
author_email = data['author_email']
# The add-comment view is called on the conversation object
conversation = IConversation(self.__parent__)
# The add-comment view is called on the conversation object
conversation = IConversation(self.__parent__)
# Create the comment
comment = CommentFactory()
comment.title = subject
comment.text = text
# Create the comment
comment = CommentFactory()
comment.title = title
comment.text = text
portal_membership = getToolByName(self.context, 'portal_membership')
portal_membership = getToolByName(self.context, 'portal_membership')
if portal_membership.isAnonymousUser():
comment.creator = author_username
comment.author_name = author_username
comment.author_email = author_email
comment.creation_date = comment.modification_date = datetime.now()
else:
member = portal_membership.getAuthenticatedMember()
fullname = member.getProperty('fullname')
if fullname == '' or None:
comment.creator = member.id
if portal_membership.isAnonymousUser():
comment.creator = author_username
comment.author_name = author_username
comment.author_email = author_email
comment.creation_date = comment.modification_date = datetime.now()
else:
comment.creator = fullname
comment.author_username = member.getUserName()
comment.author_name = member.getProperty('fullname')
comment.author_email = member.getProperty('email')
comment.creation_date = comment.modification_date = datetime.now()
member = portal_membership.getAuthenticatedMember()
fullname = member.getProperty('fullname')
if fullname == '' or None:
comment.creator = member.id
else:
comment.creator = fullname
comment.author_username = member.getUserName()
comment.author_name = member.getProperty('fullname')
comment.author_email = member.getProperty('email')
comment.creation_date = comment.modification_date = datetime.now()
# Add comment to the conversation
comment_id = conversation.addComment(comment)
# Add comment to the conversation
comment_id = conversation.addComment(comment)
# Redirect to comment (inside a content object page)
self.request.response.redirect(aq_parent(aq_inner(self.context)).absolute_url() + '#' + str(comment_id))
@button.buttonAndHandler(u'Reply')
def handleReply(self, action):
data, errors = self.extractData()
if data.has_key('title') and data.has_key('text') and data.has_key('in_reply_to'):
title = data['title']
text = data['text']
reply_to_comment_id = data['in_reply_to']
if data.has_key('author_username'):
author_username = data['author_username']
if data.has_key('author_email'):
author_email = data['author_email']
# The add-comment view is called on the conversation object
conversation = IConversation(self.__parent__)
# Fetch the comment we want to reply to
comment_to_reply_to = conversation.get(reply_to_comment_id)
replies = IReplies(comment_to_reply_to)
# Create the comment
comment = CommentFactory()
comment.title = title
comment.text = text
portal_membership = getToolByName(self.context, 'portal_membership')
if portal_membership.isAnonymousUser():
comment.creator = author_username
comment.author_name = author_username
comment.author_email = author_email
comment.creation_date = comment.modification_date = datetime.now()
else:
member = portal_membership.getAuthenticatedMember()
fullname = member.getProperty('fullname')
if fullname == '' or None:
comment.creator = member.id
else:
comment.creator = fullname
comment.author_username = member.getUserName()
comment.author_name = member.getProperty('fullname')
comment.author_email = member.getProperty('email')
comment.creation_date = comment.modification_date = datetime.now()
# Add the reply to the comment
new_re_id = replies.addComment(comment)
#IStatusMessage(self.request).addStatusMessage(\
# _("Comment field is empty."),
# type="info")
# Redirect to comment (inside a content object page)
self.request.response.redirect(aq_parent(aq_inner(self.context)).absolute_url() + '#' + str(reply_to_comment_id))
# Redirect to comment (inside a content object page)
self.request.response.redirect(aq_parent(aq_inner(self.context)).absolute_url() + '#' + str(comment_id))
class ViewletFormWrapper(ViewletBase, layout.FormWrapper):
@ -236,84 +290,4 @@ class ViewletFormWrapper(ViewletBase, layout.FormWrapper):
zope_time = DateTime(time.year, time.month, time.day, time.hour, time.minute, time.second)
return util.toLocalizedTime(zope_time, long_format=True)
CommentsViewlet = layout.wrap_form(CommentForm, __wrapper_class=ViewletFormWrapper)
class ReplyToComment(BrowserView):
"""Reply to a comment
"""
def __call__(self):
context = aq_inner(self.context)
if self.request.has_key('form.button.AddComment'):
reply_to_comment_id = self.request.get('form.reply_to_comment_id')
subject = self.request.get('subject')
text = self.request.get('body_text')
author_username = self.request.get('author_username')
author_email = self.request.get('author_email')
# Check the form input
if author_username == '':
IStatusMessage(self.request).addStatusMessage(\
_("Username field is empty."),
type="info")
return self.request.response.redirect(aq_parent(aq_inner(context)).absolute_url())
if author_email == '':
IStatusMessage(self.request).addStatusMessage(\
_("Email field is empty."),
type="info")
return self.request.response.redirect(aq_parent(aq_inner(context)).absolute_url())
if subject == '':
IStatusMessage(self.request).addStatusMessage(\
_("Subject field is empty."),
type="info")
return self.request.response.redirect(aq_parent(aq_inner(context)).absolute_url())
if text == '':
IStatusMessage(self.request).addStatusMessage(\
_("Comment field is empty."),
type="info")
return self.request.response.redirect(aq_parent(aq_inner(context)).absolute_url())
# The add-comment view is called on the conversation object
conversation = context
# Fetch the comment we want to reply to
comment_to_reply_to = conversation.get(reply_to_comment_id)
replies = IReplies(comment_to_reply_to)
# Create the comment
comment = CommentFactory()
comment.title = subject
comment.text = text
portal_membership = getToolByName(context, 'portal_membership')
if portal_membership.isAnonymousUser():
comment.creator = author_username
comment.author_name = author_username
comment.author_email = author_email
comment.creation_date = comment.modification_date = datetime.now()
else:
member = portal_membership.getAuthenticatedMember()
fullname = member.getProperty('fullname')
if fullname == '' or None:
comment.creator = member.id
else:
comment.creator = fullname
comment.author_username = member.getUserName()
comment.author_name = member.getProperty('fullname')
comment.author_email = member.getProperty('email')
comment.creation_date = comment.modification_date = datetime.now()
# Add the reply to the comment
new_re_id = replies.addComment(comment)
# Redirect to comment (inside a content object page)
#self.request.response.redirect(aq_parent(aq_inner(context)).absolute_url() + '#comment-' + str(reply_to_comment_id))
# Todo: Temporarily remove the "#comment-" to fix a bug
# in CMFPlone/skins/plone_ecmascript/form_tabbing.js
self.request.response.redirect(aq_parent(aq_inner(context)).absolute_url() + '#' + str(reply_to_comment_id))
CommentsViewlet = layout.wrap_form(CommentForm, __wrapper_class=ViewletFormWrapper)

View File

@ -73,15 +73,6 @@
permission="zope2.View"
/>
<!-- Reply to comment view -->
<browser:view
name="reply-to-comment"
for="plone.app.discussion.interfaces.IConversation"
layer="..interfaces.IDiscussionLayer"
class=".comments.ReplyToComment"
permission="zope2.View"
/>
<!-- Resource directory for javascripts -->
<browser:resourceDirectory
name="plone.app.discussion.javascripts"

View File

@ -36,21 +36,19 @@ jq(document).ready(function() {
/* Fetch the reply form inside the reply div */
var reply_form = reply_div.find("form");
/* Add a hidden field with the id of the comment */
reply_form.append("<input type=\"hidden\" value=\"" + comment_id + "\" name=\"form.reply_to_comment_id\" />");
/* Remove already typed in text from the reply form. */
reply_form.find(".field").find("input").attr("value", "")
reply_form.find(".field").find("textarea").attr("value", "")
/* Change the form action to @@reply-to-comment */
var old_action = reply_form.attr("action");
var new_action = old_action.replace("@@add-comment", "@@reply-to-comment");
reply_form.attr("action", new_action);
/* Populate the hidden 'in_reply_to' field with the correct comment id */
reply_form.find("input[name='form.widgets.in_reply_to']").val(comment_id);
/* Add a remove-reply-to-comment Javascript function to remove the form */
var cancel_reply_button = reply_div.find(".cancelreplytocomment");
cancel_reply_button.attr("id", comment_id);
/* Remove already typed in text from the reply form. */
reply_form.find(".field").find("input").attr("value", "")
reply_form.find(".field").find("textarea").attr("value", "")
/* Hide the comment button */
reply_form.find("input[name='form.buttons.comment'").css("background", "red");
/* Show the reply layer with a slide down effect */
reply_div.slideDown("slow");