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:
		
							parent
							
								
									15ba11faa7
								
							
						
					
					
						commit
						ad06e9a7e6
					
				| @ -1,46 +1,90 @@ | |||||||
| <tal:block define="userHasReplyPermission view/can_reply; | <tal:block define="errors options/state/getErrors|nothing; | ||||||
|                    isDiscussionAllowed view/is_discussion_allowed; |                    replies view/replies | nothing;" | ||||||
|                    replies view/get_replies; |  | ||||||
|                    isAnon view/is_anonymous" |  | ||||||
|            i18n:domain="plone"> |            i18n:domain="plone"> | ||||||
|     <div class="discussion" | 
 | ||||||
|          tal:condition="python:replies or (userHasReplyPermission and isDiscussionAllowed) or (isAnon and not userHasReplyPermission and isDiscussionAllowed)"> |     <div class="reply"> | ||||||
|     	<p>plone.app.discussion viewlet:</p> |         <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" |             <form name="reply" | ||||||
|                   action="" |                   action="" | ||||||
|               method="post" |                   method="get" | ||||||
|               tal:condition="python:userHasReplyPermission and isDiscussionAllowed" |                   tal:attributes="action string:${context/absolute_url}/++conversation++default/@@add-comment"> | ||||||
|               tal:attributes="action string:${context/absolute_url}/discussion_reply_form"> |  | ||||||
| 
 | 
 | ||||||
|               <input class="standalone" |                 <div class="field" | ||||||
|                      style="margin-bottom: 1.25em;" |                      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" |                            type="submit" | ||||||
|                            value="Add Comment" |                            value="Add Comment" | ||||||
|                      i18n:attributes="value label_add_comment;" |                            name="form.button.AddComment" | ||||||
|                      /> |                            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> | ||||||
|  | 
 | ||||||
|  |                 <input type="hidden" name="form.submitted" value="1" /> | ||||||
|  | 
 | ||||||
|  |             </form> | ||||||
|  |         </fieldset> | ||||||
|  |     </div> | ||||||
|  | 
 | ||||||
|  |     <div class="discussion"> | ||||||
|         <tal:getreplies repeat="reply_dict replies"> |         <tal:getreplies repeat="reply_dict replies"> | ||||||
|             <div class="comment" style="" | 
 | ||||||
|                 tal:define="indent python:reply_dict['depth']*2; |             <div class="comment" | ||||||
|                             reply python:reply_dict['object']" |                  tal:define="reply python:reply_dict[1]"> | ||||||
|                 tal:attributes="style string:margin-left:${indent}em;"> |  | ||||||
| 
 | 
 | ||||||
|                 <h3> |                 <h3> | ||||||
|                     <a name="comments" tal:attributes="name reply/id"> |                     <a name="comments" tal:attributes="name reply/title"> | ||||||
|                     <span tal:replace="reply/pretty_title_or_id">Comment title</span> |                     <span tal:replace="reply/title">Comment title</span> | ||||||
|                     </a> |                     </a> | ||||||
|                 </h3> |                 </h3> | ||||||
|                 <div class="documentByLine" | 
 | ||||||
|  |                 <!--<div class="documentByLine" | ||||||
|                      tal:define="creator reply/Creator; |                      tal:define="creator reply/Creator; | ||||||
|                                  anonymous_creator python:creator in ('Anonymous User', ''); |                                  anonymous_creator python:creator in ('Anonymous User', ''); | ||||||
|                                  mi python:not anonymous_creator and view.member_info(creator); |                                  mi python:not anonymous_creator and view.member_info(creator); | ||||||
| @ -52,39 +96,16 @@ | |||||||
|                                   condition="anonymous_creator">Anonymous User</tal:name> |                                   condition="anonymous_creator">Anonymous User</tal:name> | ||||||
|                         <tal:at i18n:translate="label_commented_at">at</tal:at> |                         <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> |                     <span tal:replace="python:view.format_time(reply.ModificationDate())">8/23/2001 12:40:44 PM</span> | ||||||
|                 </div> |                 </div>--> | ||||||
|  | 
 | ||||||
|                 <div class="commentBody" |                 <div class="commentBody" | ||||||
|                      tal:content="structure reply/CookedBody"> |                      tal:content="structure reply/text"> | ||||||
|                      This is the body text of the comment. |                      This is the body text of the comment. | ||||||
|                 </div> |                 </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> |             </div> | ||||||
|  | 
 | ||||||
|         </tal:getreplies> |         </tal:getreplies> | ||||||
|     </div> |     </div> | ||||||
|  | 
 | ||||||
| </tal:block> | </tal:block> | ||||||
| @ -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 Acquisition import aq_inner | ||||||
| from AccessControl import getSecurityManager | from Products.Five.browser import BrowserView | ||||||
| from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile | 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): | from plone.app.discussion.comment import CommentFactory | ||||||
|     index = ViewPageTemplateFile('comments.pt') | 
 | ||||||
|  | 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): |     def update(self): | ||||||
|         super(CommentsViewlet, self).update() |         pass | ||||||
|         self.portal_discussion = getToolByName(self.context, 'portal_discussion', None) |  | ||||||
|         self.portal_membership = getToolByName(self.context, 'portal_membership', None) |  | ||||||
| 
 | 
 | ||||||
|     def can_reply(self): |     def replies(self): | ||||||
|         return getSecurityManager().checkPermission('Reply to item', aq_inner(self.context)) |         conversation = conversationAdapterFactory(self.context) | ||||||
|  |         return conversation.items() | ||||||
| 
 | 
 | ||||||
|     def is_discussion_allowed(self): | class AddComment(BrowserView): | ||||||
|         if self.portal_discussion is None: |     """Add a comment to a conversation | ||||||
|             return False |     """ | ||||||
|         else: |  | ||||||
|             return self.portal_discussion.isDiscussionAllowedFor(aq_inner(self.context)) |  | ||||||
| 
 | 
 | ||||||
|     def get_replies(self): |     def __call__(self): | ||||||
|         replies = [] |  | ||||||
| 
 | 
 | ||||||
|         context = aq_inner(self.context) |         if self.request.has_key('form.button.AddComment'): | ||||||
|         container = aq_parent(context) |  | ||||||
|         pd = self.portal_discussion |  | ||||||
| 
 | 
 | ||||||
|         def getRs(obj, replies, counter): |             subject = self.request.get('subject') | ||||||
|             rs = pd.getDiscussionFor(obj).getReplies() |             text = self.request.get('body_text') | ||||||
|             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) |  | ||||||
| 
 | 
 | ||||||
|         try: |             # The add-comment view is called on the conversation object | ||||||
|             getRs(context, replies, 0) |             conversation = self.context | ||||||
|         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 |  | ||||||
| 
 | 
 | ||||||
|     def is_anonymous(self): |             # Create the comment | ||||||
|         return self.portal_state.anonymous() |             comment = CommentFactory() | ||||||
|  |             comment.title = subject | ||||||
|  |             comment.text = text | ||||||
| 
 | 
 | ||||||
|     def login_action(self): |             # Add comment to the conversation | ||||||
|         return '%s/login_form?came_from=%s' % (self.navigation_root_url, url_quote(self.request.get('URL', '')),) |             conversation.addComment(comment) | ||||||
| 
 | 
 | ||||||
|     def can_manage(self): |             # TODO: Redirect to the document object page | ||||||
|         return getSecurityManager().checkPermission('Manage portal', aq_inner(self.context)) |             # conversation.__parent__ is not working?! | ||||||
| 
 |             #self.request.response.redirect(aq_inner(self.context).absolute_url()) | ||||||
|     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') |  | ||||||
|  | |||||||
| @ -18,4 +18,13 @@ | |||||||
|         permission="zope2.View" |         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> | </configure> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user