Added menus and dialog to display object's versions history
authorThierry Florac <thierry.florac@onf.fr>
Wed, 20 May 2015 12:46:23 +0200
changeset 7 3991ed2e1855
parent 6 6f4862c48019
child 8 3fa6016f969f
Added menus and dialog to display object's versions history
src/pyams_workflow/zmi/versions.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_workflow/zmi/versions.py	Wed May 20 12:46:23 2015 +0200
@@ -0,0 +1,197 @@
+#
+# 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_form.interfaces.form import IWidgetsSuffixViewletsManager
+from pyams_security.interfaces import ISecurityManager
+from pyams_skin.layer import IPyAMSLayer
+from pyams_workflow.interfaces import IWorkflowVersion, IWorkflowState, IWorkflowManagedContent, IWorkflow
+from pyams_zmi.interfaces.menu import IPropertiesMenu
+from z3c.table.interfaces import IValues, IColumn
+
+# import packages
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.table import BaseTable, I18nColumn
+from pyams_skin.viewlet.menu import MenuItem
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.date import format_datetime
+from pyams_utils.registry import query_utility
+from pyams_utils.text import text_to_html
+from pyams_utils.traversing import get_parent
+from pyams_viewlet.viewlet import viewlet_config, Viewlet
+from pyams_zmi.form import AdminDialogDisplayForm
+from pyams_zmi.layer import IAdminLayer
+from z3c.form import field
+from z3c.table.column import GetAttrColumn
+from zope.interface import Interface
+
+from pyams_workflow import _
+
+
+@viewlet_config(name='workflow-history.menu', context=IWorkflowVersion, layer=IAdminLayer,
+                manager=IPropertiesMenu, permission='manage', weight=1)
+class WorkflowVersionHistoryMenuItem(MenuItem):
+    """Workflow history menu item"""
+
+    label = _("Version history...")
+    icon_class = 'fa fa-fw fa-history'
+    url = 'version-history.html'
+    modal_target = True
+
+
+@pagelet_config(name='version-history.html', context=IWorkflowVersion, layer=IPyAMSLayer,
+                permission='manage')
+class WorkflowVersionHistoryDisplayForm(AdminDialogDisplayForm):
+    """Workflow version history view"""
+
+    @property
+    def title(self):
+        return self.context.name
+
+    dialog_class = 'modal-large'
+    fields = field.Fields(Interface)
+
+
+class WorkflowVersionHistoryTable(BaseTable):
+    """Workflow version history table"""
+
+    @property
+    def title(self):
+        translate = self.request.localizer.translate
+        return translate(_("Version {version} history")).format(version=IWorkflowState(self.context).version_id)
+
+    cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight datatable'}
+    sortOn = None
+    hide_toolbar = True
+
+    @property
+    def data_attributes(self):
+        return {'table': {'data-ams-datatable-global-filter': 'false',
+                          'data-ams-datatable-info': 'false',
+                          'data-ams-datatable-sort': 'false',
+                          'data-ams-datatable-sdom': "t<'dt-row dt-bottom-row'<'text-right'p>>",
+                          'data-ams-datatable-display-length': '20',
+                          'data-ams-datatable-pagination-type': 'bootstrap_prevnext'}}
+
+
+@adapter_config(context=(IWorkflowVersion, IAdminLayer, WorkflowVersionHistoryTable), provides=IValues)
+class WorkflowVersionHistoryValuesAdapter(ContextRequestViewAdapter):
+    """Workflow version history values adapter"""
+
+    @property
+    def values(self):
+        return IWorkflowState(self.context).history
+
+
+@adapter_config(name='date', context=(Interface, IAdminLayer, WorkflowVersionHistoryTable), provides=IColumn)
+class WorkflowVersionHistoryDateColumn(I18nColumn, GetAttrColumn):
+    """Workflow version history date column"""
+
+    _header = _("Date")
+    attrName = 'date'
+    weight = 1
+
+    def renderCell(self, item):
+        return format_datetime(item.date, request=self.request)
+
+
+@adapter_config(name='name', context=(Interface, IAdminLayer, WorkflowVersionHistoryTable), provides=IColumn)
+class WorkflowVersionHistoryPrincipalColumn(I18nColumn, GetAttrColumn):
+    """Workflow version history principal column"""
+
+    _header = _("Principal")
+    attrName = 'principal'
+    weight = 4
+
+    def renderCell(self, item):
+        manager = query_utility(ISecurityManager)
+        if manager is not None:
+            return manager.get_principal(item.principal).title
+        else:
+            return item.principal
+
+
+@adapter_config(name='transition', context=(Interface, IAdminLayer, WorkflowVersionHistoryTable), provides=IColumn)
+class WorkflowVersionHistoryTransitionColumn(I18nColumn, GetAttrColumn):
+    """Workflow version history source column"""
+
+    _header = _("Transition")
+    attrName = 'transition'
+    weight = 5
+
+
+@adapter_config(name='source', context=(Interface, IAdminLayer, WorkflowVersionHistoryTable), provides=IColumn)
+class WorkflowVersionHistorySourceColumn(I18nColumn, GetAttrColumn):
+    """Workflow version history source column"""
+
+    _header = _("Source")
+    weight = 10
+
+    def renderCell(self, item):
+        content = get_parent(self.context, IWorkflowManagedContent)
+        workflow = query_utility(IWorkflow, name=content.workflow_name)
+        translate = self.request.localizer.translate
+        if item.source_version:
+            return translate(_("Version {version} ({status})")).format(
+                version=item.source_version,
+                status=translate(workflow.states.getTerm(item.source_state).title))
+        else:
+            return translate(_("Version {version} ({status})")).format(
+                version=IWorkflowState(self.context).version_id,
+                status=translate(workflow.states.getTerm(item.source_state).title) if item.source_state else '--')
+
+
+@adapter_config(name='target', context=(Interface, IAdminLayer, WorkflowVersionHistoryTable), provides=IColumn)
+class WorkflowVersionHistoryTargetColumn(I18nColumn, GetAttrColumn):
+    """Workflow version history source column"""
+
+    _header = _("Target state")
+    weight = 11
+
+    def renderCell(self, item):
+        content = get_parent(self.context, IWorkflowManagedContent)
+        workflow = query_utility(IWorkflow, name=content.workflow_name)
+        translate = self.request.localizer.translate
+        return translate(workflow.states.getTerm(item.target_state).title)
+
+
+@adapter_config(name='comment', context=(Interface, IAdminLayer, WorkflowVersionHistoryTable), provides=IColumn)
+class WorkflowVersionHistoryCommentColumn(I18nColumn, GetAttrColumn):
+    """Workflow version history comment column"""
+
+    _header = _("Comment")
+    weight = 20
+
+    def renderCell(self, item):
+        return text_to_html(item.comment or '--')
+
+
+@viewlet_config(name='version-history', view=WorkflowVersionHistoryDisplayForm, layer=IAdminLayer,
+                manager=IWidgetsSuffixViewletsManager)
+class WorkflowVersionHistoryViewlet(Viewlet):
+    """Workflow version history viewlet"""
+
+    def __init__(self, context, request, view, manager):
+        super(WorkflowVersionHistoryViewlet, self).__init__(context, request, view, manager)
+        self.table = WorkflowVersionHistoryTable(context, request)
+
+    def update(self):
+        super(WorkflowVersionHistoryViewlet, self).update()
+        self.table.update()
+
+    def render(self):
+        return '<div class="no-widget-toolbar">{0}</div>'.format(self.table.render())