From a7f61eb4d3e1f223ff15e6bbabd11a98c75e2f9f Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Fri, 27 May 2011 13:47:15 +0000 Subject: [PATCH] Refactor/clean up the handleComment method. svn path=/plone.app.discussion/trunk/; revision=50069 --- CHANGES.txt | 5 ++ plone/app/discussion/browser/comments.py | 70 ++++++++++-------------- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 1797c24..1e60f19 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,9 @@ Changelog 2.0.4 (Unreleased) ------------------ +- Refactor/clean up the handleComment method. + [timo] + - Make handleComment method store comment attributes from form extenders. This allows us to extend the comment form with external add-ons. [timo] @@ -18,6 +21,7 @@ Changelog - Italian translation review. [gborelli] + 2.0.2 (2011-05-12) ------------------ @@ -25,6 +29,7 @@ Changelog Item. [erico_andrei] + 2.0.1 (2011-04-22) ------------------ diff --git a/plone/app/discussion/browser/comments.py b/plone/app/discussion/browser/comments.py index fad9c6a..d9f0431 100644 --- a/plone/app/discussion/browser/comments.py +++ b/plone/app/discussion/browser/comments.py @@ -124,19 +124,19 @@ class CommentForm(extensible.ExtensibleForm, form.Form): name='comment') def handleComment(self, action): 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() if errors: return - - text = u"" - author_name = u"" - author_email = u"" - user_notification = None - - # Captcha check for anonymous users (if Captcha is enabled and - # anonymous commenting is allowed) + + # Validate Captcha registry = queryUtility(IRegistry) settings = registry.forInterface(IDiscussionSettings, check=False) portal_membership = getToolByName(self.context, 'portal_membership') @@ -152,46 +152,28 @@ class CommentForm(extensible.ExtensibleForm, form.Form): None) captcha.validate(data['captcha']) - # Create a comment - comment = createObject('plone.Comment') - comment.text = text + # some attributes are not always set + author_name = u"" + 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(): setattr(comment, attribute, data[attribute]) - - # Fetch data from request - if 'text' in data: - text = data['text'] + # Make sure author_name is properly encoded if 'author_name' in data: author_name = data['author_name'] if isinstance(author_name, str): 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', context) - + portal_membership = getToolByName(self.context, 'portal_membership') if portal_membership.isAnonymousUser() and \ - settings.anonymous_comments: + settings.anonymous_comments: # Anonymous Users comment.creator = 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 " "'reply to item' permission.") - # Check if the added comment is a reply to an existing comment - # or just a regular reply to the content object. + # Add comment to conversation + conversation = IConversation(self.__parent__) if data['in_reply_to']: # 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) else: # Add a comment to the conversation comment_id = conversation.addComment(comment) + # Redirect after form submit: # 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 # has 'review comments' permission, he/she is redirected directly # to the comment. can_review = getSecurityManager().checkPermission('Review comments', 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: # Show info message when comment moderation is enabled IStatusMessage(self.context.REQUEST).addStatusMessage(