Refactor/clean up the handleComment method.

svn path=/plone.app.discussion/trunk/; revision=50069
This commit is contained in:
Timo Stollenwerk 2011-05-27 13:47:15 +00:00
parent ca997df2a0
commit a7f61eb4d3
2 changed files with 33 additions and 42 deletions

View File

@ -4,6 +4,9 @@ Changelog
2.0.4 (Unreleased) 2.0.4 (Unreleased)
------------------ ------------------
- Refactor/clean up the handleComment method.
[timo]
- Make handleComment method store comment attributes from form extenders. This - Make handleComment method store comment attributes from form extenders. This
allows us to extend the comment form with external add-ons. allows us to extend the comment form with external add-ons.
[timo] [timo]
@ -18,6 +21,7 @@ Changelog
- Italian translation review. - Italian translation review.
[gborelli] [gborelli]
2.0.2 (2011-05-12) 2.0.2 (2011-05-12)
------------------ ------------------
@ -25,6 +29,7 @@ Changelog
Item. Item.
[erico_andrei] [erico_andrei]
2.0.1 (2011-04-22) 2.0.1 (2011-04-22)
------------------ ------------------

View File

@ -124,19 +124,19 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
name='comment') name='comment')
def handleComment(self, action): def handleComment(self, action):
context = aq_inner(self.context) context = aq_inner(self.context)
wf = getToolByName(context, 'portal_workflow')
# Check if conversation is enabled on this content object
if not self.__parent__.restrictedTraverse(
'@@conversation_view').enabled():
raise Unauthorized("Discussion is not enabled for this content "
"object.")
# Validation form
data, errors = self.extractData() data, errors = self.extractData()
if errors: if errors:
return return
text = u"" # Validate Captcha
author_name = u""
author_email = u""
user_notification = None
# Captcha check for anonymous users (if Captcha is enabled and
# anonymous commenting is allowed)
registry = queryUtility(IRegistry) registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False) settings = registry.forInterface(IDiscussionSettings, check=False)
portal_membership = getToolByName(self.context, 'portal_membership') portal_membership = getToolByName(self.context, 'portal_membership')
@ -152,46 +152,28 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
None) None)
captcha.validate(data['captcha']) captcha.validate(data['captcha'])
# Create a comment # some attributes are not always set
comment = createObject('plone.Comment') author_name = u""
comment.text = text author_email = u""
user_notification = None
# Create comment
comment = createObject('plone.Comment')
# Set comment attributes (including extended comment form attributes)
for attribute in self.fields.keys(): for attribute in self.fields.keys():
setattr(comment, attribute, data[attribute]) setattr(comment, attribute, data[attribute])
# Make sure author_name is properly encoded
# Fetch data from request
if 'text' in data:
text = data['text']
if 'author_name' in data: if 'author_name' in data:
author_name = data['author_name'] author_name = data['author_name']
if isinstance(author_name, str): if isinstance(author_name, str):
author_name = unicode(author_name, 'utf-8') author_name = unicode(author_name, 'utf-8')
if 'author_email' in data:
author_email = data['author_email']
if 'user_notification' in data:
user_notification = data['user_notification']
# Check if conversation is enabled on this content object
if not self.__parent__.restrictedTraverse(
'@@conversation_view').enabled():
raise Unauthorized("Discussion is not enabled for this content "
"object.")
# The add-comment view is called on the conversation object
conversation = IConversation(self.__parent__)
if data['in_reply_to']:
# Fetch the comment we want to reply to
conversation_to_reply_to = conversation.get(data['in_reply_to'])
replies = IReplies(conversation_to_reply_to)
portal_membership = getToolByName(self.context, 'portal_membership')
# Set comment author properties for anonymous users or members
can_reply = getSecurityManager().checkPermission('Reply to item', can_reply = getSecurityManager().checkPermission('Reply to item',
context) context)
portal_membership = getToolByName(self.context, 'portal_membership')
if portal_membership.isAnonymousUser() and \ if portal_membership.isAnonymousUser() and \
settings.anonymous_comments: settings.anonymous_comments:
# Anonymous Users # Anonymous Users
comment.creator = author_name comment.creator = author_name
comment.author_name = author_name comment.author_name = author_name
@ -222,22 +204,26 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
"anonymous commenting is disabled. Or user does not have the " "anonymous commenting is disabled. Or user does not have the "
"'reply to item' permission.") "'reply to item' permission.")
# Check if the added comment is a reply to an existing comment # Add comment to conversation
# or just a regular reply to the content object. conversation = IConversation(self.__parent__)
if data['in_reply_to']: if data['in_reply_to']:
# Add a reply to an existing comment # Add a reply to an existing comment
conversation_to_reply_to = conversation.get(data['in_reply_to'])
replies = IReplies(conversation_to_reply_to)
comment_id = replies.addComment(comment) comment_id = replies.addComment(comment)
else: else:
# Add a comment to the conversation # Add a comment to the conversation
comment_id = conversation.addComment(comment) comment_id = conversation.addComment(comment)
# Redirect after form submit:
# If a user posts a comment and moderation is enabled, a message is # If a user posts a comment and moderation is enabled, a message is
# shown to the user that his/her comment awaits moderation. If the user # shown to the user that his/her comment awaits moderation. If the user
# has 'review comments' permission, he/she is redirected directly # has 'review comments' permission, he/she is redirected directly
# to the comment. # to the comment.
can_review = getSecurityManager().checkPermission('Review comments', can_review = getSecurityManager().checkPermission('Review comments',
context) context)
comment_review_state = wf.getInfoFor(comment, 'review_state') workflowTool = getToolByName(context, 'portal_workflow')
comment_review_state = workflowTool.getInfoFor(comment, 'review_state')
if comment_review_state == 'pending' and not can_review: if comment_review_state == 'pending' and not can_review:
# Show info message when comment moderation is enabled # Show info message when comment moderation is enabled
IStatusMessage(self.context.REQUEST).addStatusMessage( IStatusMessage(self.context.REQUEST).addStatusMessage(