src/pyams_content/shared/common/zmi/owner.py
changeset 0 7c0001cacf8e
child 32 66baddd786d5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/common/zmi/owner.py	Thu Oct 08 13:37:29 2015 +0200
@@ -0,0 +1,127 @@
+#
+# 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.
+#
+from pyramid.view import view_config
+from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
+from zope.lifecycleevent import ObjectModifiedEvent
+from pyams_content.interfaces import MANAGE_SITE_PERMISSION
+from pyams_content.shared.common.interfaces import IWfSharedContent, IWfSharedContentRoles
+from pyams_form.form import AJAXAddForm
+from pyams_form.help import FormHelp
+from pyams_form.interfaces.form import IFormHelp
+from pyams_form.schema import CloseButton
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_security.schema import Principal
+from pyams_security.zmi.interfaces import IObjectSecurityMenu
+from pyams_skin.layer import IPyAMSLayer
+from pyams_skin.viewlet.menu import MenuItem
+from pyams_utils.adapter import adapter_config
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_workflow.interfaces import IWorkflowVersions, IWorkflow, IWorkflowState
+from pyams_zmi.form import AdminDialogAddForm
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+
+# import packages
+from z3c.form import field, button
+from zope.interface import Interface
+from zope.schema import Bool
+
+from pyams_content import _
+
+
+@viewlet_config(name='change-owner.menu', context=IWfSharedContent, layer=IPyAMSLayer,
+                view=Interface, manager=IObjectSecurityMenu, permission=MANAGE_SITE_PERMISSION, weight=10)
+class WfSharedContentOwnerChangeMenu(MenuItem):
+    """Shared content owner change menu"""
+
+    label = _("Change owner...")
+    icon_class = 'fa fa-fw fa-user'
+
+    url = 'change-owner.html'
+    modal_target = True
+
+
+class IWfSharedContentOwnerChangeInfo(Interface):
+    """Shared content owner change form fields"""
+
+    new_owner = Principal(title=_("New owner"),
+                          description=_("The selected user will become the new content's owner"))
+
+    keep_owner_as_contributor = Bool(title=_("Keep previous owner as contributor"),
+                                     description=_("If 'yes', the previous owner will still be able to modify this "
+                                                   "content"),
+                                     required=False,
+                                     default=False)
+
+
+class IWfSharedContentOwnerChangeButtons(Interface):
+    """Shared content owner change form buttons"""
+
+    close = CloseButton(name='close', title=_("Cancel"))
+    change = button.Button(name='change', title=_("Change owner"))
+
+
+@pagelet_config(name='change-owner.html', context=IWfSharedContent, layer=IPyAMSLayer,
+                permission=MANAGE_SITE_PERMISSION)
+class WfSharedContentOwnerChangeForm(AdminDialogAddForm):
+    """Shared content owner change form"""
+
+    legend = _("Change content's owner")
+
+    fields = field.Fields(IWfSharedContentOwnerChangeInfo)
+    fields['keep_owner_as_contributor'].widgetFactory = SingleCheckBoxFieldWidget
+    buttons = button.Buttons(IWfSharedContentOwnerChangeButtons)
+
+    ajax_handler = 'change-owner.json'
+    edit_permission = MANAGE_SITE_PERMISSION
+
+    def updateActions(self):
+        super(WfSharedContentOwnerChangeForm, self).updateActions()
+        if 'change' in self.actions:
+            self.actions['change'].addClass('btn-primary')
+
+    def createAndAdd(self, data):
+        new_owner = data.get('new_owner')
+        workflow = IWorkflow(self.context)
+        for version in IWorkflowVersions(self.context).get_versions():
+            if IWorkflowState(version).state in workflow.readonly_states:
+                continue
+            roles = IWfSharedContentRoles(version)
+            previous_owner = next(iter(roles.owner))
+            roles.owner = {new_owner}
+            contributors = roles.contributors
+            if (previous_owner in contributors) and not data.get('keep_owner_as_contributor'):
+                contributors.remove(previous_owner)
+            contributors.add(new_owner)
+            self.request.registry.notify(ObjectModifiedEvent(version))
+
+
+@view_config(name='change-owner.json', context=IWfSharedContent, request_type=IPyAMSLayer,
+             permission=MANAGE_SITE_PERMISSION, renderer='json', xhr=True)
+class WfSharedContentOwnerChangeAJAXForm(AJAXAddForm,WfSharedContentOwnerChangeForm):
+    """Shared content owner change form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        return {'status': 'reload'}
+
+
+@adapter_config(context=(IWfSharedContent, IPyAMSLayer, WfSharedContentOwnerChangeForm), provides=IFormHelp)
+class WfSharedContentOwnerChangeFormHelp(FormHelp):
+    """Shared content owner change form help"""
+
+    message = _("All versions of this content which are not archived will be transferred to newly selected owner")
+    message_format = 'rest'