src/pyams_content/shared/common/zmi/review.py
changeset 238 2dc445ad2cf5
parent 237 ccd42b19051a
child 239 b3b7d4bf63f7
--- a/src/pyams_content/shared/common/zmi/review.py	Fri Oct 13 10:03:20 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,282 +0,0 @@
-#
-# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
-
-# import interfaces
-from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION, COMMENT_CONTENT_PERMISSION
-from pyams_content.interfaces.review import IReviewManager, IReviewComments
-from pyams_content.shared.common.interfaces import IWfSharedContent
-from pyams_security.interfaces import ISecurityManager
-from pyams_security.interfaces.profile import IPublicProfile
-from pyams_skin.interfaces.viewlet import IContextActions, IWidgetTitleViewletManager
-from pyams_skin.layer import IPyAMSLayer
-from pyams_zmi.interfaces.menu import IContentManagementMenu
-from pyams_zmi.layer import IAdminLayer
-from pyramid_chameleon.interfaces import IChameleonTranslate
-
-# import packages
-from pyams_content.shared.common.review import ReviewComment
-from pyams_form.form import AJAXAddForm
-from pyams_form.schema import CloseButton
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_security.schema import PrincipalsSet
-from pyams_skin.viewlet.menu import MenuItem
-from pyams_skin.viewlet.toolbar import ToolbarMenuItem, JsToolbarAction
-from pyams_template.template import template_config, get_view_template
-from pyams_utils.date import get_age, format_datetime
-from pyams_utils.registry import get_utility, query_utility
-from pyams_viewlet.viewlet import viewlet_config
-from pyams_zmi.form import AdminDialogAddForm
-from pyams_zmi.view import InnerAdminView
-from pyramid.view import view_config
-from z3c.form import field, button
-from zope.interface import Interface
-from zope.schema import Text, Bool
-
-from pyams_content import _
-
-
-#
-# Review request form
-#
-
-@viewlet_config(name='ask-review.menu', context=IWfSharedContent, layer=IPyAMSLayer,
-                view=Interface, manager=IContextActions, permission=MANAGE_CONTENT_PERMISSION, weight=10)
-class WfSharedContentReviewMenu(ToolbarMenuItem):
-    """Shared content review menu"""
-
-    label = _("Ask for review...")
-    label_css_class = 'fa fa-fw fa-eye'
-
-    url = 'ask-review.html'
-    modal_target = True
-
-
-class ISharedContentReviewInfo(Interface):
-    """Shared content review infos"""
-
-    reviewers = PrincipalsSet(title=_("Sought principals"),
-                              description=_("List of principals from which a review is requested"),
-                              required=True)
-
-    comment = Text(title=_("Comment"),
-                   description=_("Comment associated with this request"),
-                   required=True)
-
-    notify_all = Bool(title=_("Notify all reviewers"),
-                      description=_("If 'yes', selected reviewers will be notified by mail of your request, "
-                                    "even if they were already members of the reviewers group. Otherwise, only new "
-                                    "reviewers will be notified"),
-                      default=False,
-                      required=True)
-
-
-class ISharedContentReviewButtons(Interface):
-    """Shared content review form buttons"""
-
-    close = CloseButton(name='close', title=_("Cancel"))
-    review = button.Button(name='review', title=_("Ask for content review"))
-
-
-@pagelet_config(name='ask-review.html', context=IWfSharedContent, layer=IPyAMSLayer,
-                permission=MANAGE_CONTENT_PERMISSION)
-class WfSharedContentReviewForm(AdminDialogAddForm):
-    """Shared content review form"""
-
-    legend = _("Content review request")
-    icon_css_class = 'fa fa-fw fa-eye'
-
-    fields = field.Fields(ISharedContentReviewInfo)
-    buttons = button.Buttons(ISharedContentReviewButtons)
-
-    ajax_handler = 'ask-review.json'
-    edit_permission = MANAGE_CONTENT_PERMISSION
-
-    label_css_class = 'control-label col-md-4'
-    input_css_class = 'col-md-8'
-
-    def updateWidgets(self, prefix=None):
-        super(WfSharedContentReviewForm, self).updateWidgets(prefix)
-        if 'comment' in self.widgets:
-            self.widgets['comment'].widget_css_class = 'textarea'
-            self.widgets['comment'].addClass('height-100')
-
-    def updateActions(self):
-        super(WfSharedContentReviewForm, self).updateActions()
-        if 'review' in self.actions:
-            self.actions['review'].addClass('btn-primary')
-
-    def createAndAdd(self, data):
-        manager = IReviewManager(self.context, None)
-        if manager is not None:
-            return manager.ask_review(data.get('reviewers'),
-                                      data.get('comment'),
-                                      data.get('notify_all'))
-
-
-@view_config(name='ask-review.json', context=IWfSharedContent, request_type=IPyAMSLayer,
-             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
-class WfSharedContentReviewAJAXForm(AJAXAddForm, WfSharedContentReviewForm):
-    """Shared content review form, JSON renderer"""
-
-    def get_ajax_output(self, changes):
-        translate = self.request.localizer.translate
-        if changes:
-            return {'status': 'success',
-                    'message': translate(_("Request successful. "
-                                           "{count} new notification(s) have been sent")).format(count=changes),
-                    'events': [{
-                        'event': 'PyAMS_content.changed_item',
-                        'options': {'handler': 'PyAMS_content.review.updateComments'}
-                    }]}
-        else:
-            return {'status': 'info',
-                    'message': translate(_("Request successful. No new notification have been sent")),
-                    'events': [{
-                        'event': 'PyAMS_content.changed_item',
-                        'options': {'handler': 'PyAMS_content.review.updateComments'}
-                    }]}
-
-
-#
-# Share contents comments
-#
-
-@viewlet_config(name='review-comments.menu', context=IWfSharedContent, layer=IAdminLayer,
-                manager=IContentManagementMenu, permission=COMMENT_CONTENT_PERMISSION, weight=30)
-class SharedContentReviewCommentsMenu(MenuItem):
-    """Shared content review comments menu"""
-
-    label = _("Comments")
-    icon_class = 'fa-comments-o'
-    url = '#review-comments.html'
-
-    badge_class = 'bg-color-info'
-
-    def update(self):
-        super(SharedContentReviewCommentsMenu, self).update()
-        nb_comments = len(IReviewComments(self.context))
-        self.badge = str(nb_comments)
-        if nb_comments == 0:
-            self.badge_class += ' hidden'
-
-
-@pagelet_config(name='review-comments.html', context=IWfSharedContent, layer=IPyAMSLayer,
-                permission=COMMENT_CONTENT_PERMISSION)
-@template_config(template='templates/review-comments.pt', layer=IPyAMSLayer)
-class SharedContentReviewCommentsView(InnerAdminView):
-    """Shared content review comments view"""
-
-    legend = _("Review comments")
-
-    comments = None
-    security = None
-
-    def update(self):
-        super(SharedContentReviewCommentsView, self).update()
-        self.comments = IReviewComments(self.context).values()
-        self.security = get_utility(ISecurityManager)
-
-    def get_principal(self, principal_id):
-        return self.security.get_principal(principal_id)
-
-    def get_avatar(self, principal):
-        return IPublicProfile(principal).avatar
-
-    def get_date(self, comment):
-        return format_datetime(comment.creation_date)
-
-    def get_age(self, comment):
-        return get_age(comment.creation_date)
-
-
-@viewlet_config(name='add-review-comment.action', context=IWfSharedContent, layer=IAdminLayer,
-                view=SharedContentReviewCommentsView, manager=IWidgetTitleViewletManager,
-                permission=COMMENT_CONTENT_PERMISSION)
-class SharedContentReviewAddCommentAction(JsToolbarAction):
-    """Shared content review add comment action"""
-
-    label = _("Add comment...")
-    url = 'PyAMS_content.review.addCommentAction'
-
-
-@view_config(name='get-last-review-comments.json', context=IWfSharedContent, request_type=IPyAMSLayer,
-             permission=COMMENT_CONTENT_PERMISSION, renderer='json', xhr=True)
-@template_config(template='templates/review-comments-json.pt')
-class ReviewCommentsView(SharedContentReviewCommentsView):
-    """"Get review comments"""
-
-    def __init__(self, request):
-        self.request = request
-        self.context = request.context
-
-    template = get_view_template()
-
-    def __call__(self):
-        result = {'status': 'success',
-                  'count': 0}
-        comments = IReviewComments(self.context)
-        previous_count = int(self.request.params.get('count', 0))
-        current_count = len(comments)
-        if previous_count == current_count:
-            result['count'] = current_count
-        else:
-            self.comments = comments.values()[previous_count:]
-            self.security = get_utility(ISecurityManager)
-            comments_body = self.template(request=self.request,
-                                          context=self.context,
-                                          view=self,
-                                          translate=query_utility(IChameleonTranslate))
-            result.update({'content': comments_body,
-                           'count': len(comments)})
-        return result
-
-
-@view_config(name='add-review-comment.json', context=IWfSharedContent, request_type=IPyAMSLayer,
-             permission=COMMENT_CONTENT_PERMISSION, renderer='json', xhr=True)
-@template_config(template='templates/review-add-comment.pt')
-class ReviewCommentAddForm(object):
-    """Review comment add form"""
-
-    def __init__(self, request):
-        self.request = request
-        self.context = request.context
-
-    template = get_view_template()
-
-    def __call__(self):
-        request = self.request
-        translate = request.localizer.translate
-        comment_body = request.params.get('comment')
-        if not comment_body:
-            return {'status': 'error',
-                    'message': translate(_("Message is mandatory!"))}
-        # add new comment
-        comment = ReviewComment(owner=request.principal.id,
-                                comment=request.params.get('comment'))
-        comments = IReviewComments(request.context)
-        comments.add_comment(comment)
-        # return comment infos
-        profile = IPublicProfile(request.principal)
-        comment_body = self.template(request=request,
-                                     context=self.context,
-                                     translate=query_utility(IChameleonTranslate),
-                                     options={'comment': comment,
-                                              'profile': profile})
-        return {'status': 'success',
-                'callback': 'PyAMS_content.review.addCommentCallback',
-                'options': {'content': comment_body,
-                            'count': len(comments)}}