src/pyams_workflow/zmi/workflow.py
changeset 11 f25326715002
parent 8 3fa6016f969f
child 15 182f33bba91d
--- a/src/pyams_workflow/zmi/workflow.py	Wed May 20 14:57:49 2015 +0200
+++ b/src/pyams_workflow/zmi/workflow.py	Wed Jun 17 10:02:06 2015 +0200
@@ -16,19 +16,28 @@
 # import standard library
 
 # import interfaces
+from pyams_i18n.interfaces import II18n
 from pyams_skin.interfaces.viewlet import IToolbarViewletManager
+from pyams_skin.layer import IPyAMSLayer
 from pyams_workflow.interfaces import IWorkflow, IWorkflowInfo, IWorkflowVersion, IWorkflowManagedContent, \
-    IWorkflowVersions
+    IWorkflowVersions, IWorkflowState
 from pyams_workflow.zmi.interfaces import IWorkflowMenu
 from pyams_zmi.layer import IAdminLayer
+from zope.container.interfaces import IContainer
+from zope.dublincore.interfaces import IZopeDublinCore
 
 # import packages
 from pyams_skin.table import NameColumn
 from pyams_skin.viewlet.toolbar import ToolbarMenu, ToolbarMenuItem
 from pyams_utils.registry import query_utility
+from pyams_utils.date import format_datetime
+from pyams_utils.timezone import tztime
 from pyams_utils.traversing import get_parent
+from pyams_utils.url import absolute_url
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config
+from pyramid.response import Response
+from pyramid.view import view_config
 from zope.interface import implementer, Interface
 
 from pyams_workflow import _
@@ -41,14 +50,14 @@
         super(WorkflowMenuItem, self).__init__(context, request, view, manager)
         self.label = transition.title
         self.label_css_class = transition.user_data.get('menu_css_class') or 'fa fa-fw'
-        self.url = '{url}?transition_id={transition}'.format(url=transition.user_data.get('view_name'),
-                                                             transition=transition.transition_id)
+        self.url = '{url}?form.widgets.transition_id={transition}'.format(url=transition.user_data.get('view_name'),
+                                                                          transition=transition.transition_id)
         self.weight = transition.order
         self.modal_target = True
 
 
 @viewlet_config(name='workflow.menu', view=Interface, layer=IAdminLayer, context=IWorkflowVersion,
-                manager=IToolbarViewletManager, permission='portal.templates.manage', weight=200)
+                manager=IToolbarViewletManager, weight=800)
 @viewletmanager_config(name='workflow.menu', layer=IAdminLayer, provides=IWorkflowMenu)
 @implementer(IWorkflowMenu)
 class WorkflowMenu(ToolbarMenu):
@@ -75,11 +84,53 @@
 class WorkflowContentNameColumn(NameColumn):
     """Workflow content name column"""
 
+    name_field = 'title'
+
     def getValue(self, obj):
-        wf_versions = IWorkflowVersions(obj, None)
-        if wf_versions is None:
-            return super(WorkflowContentNameColumn, self).getValue(obj)
-        versions = wf_versions.get_last_versions(count=1)
-        if len(versions) > 0:
-            version = versions[0]
-            return super(WorkflowContentNameColumn, self).getValue(version)
+        result = ''
+        wf_versions = IWorkflowVersions(obj).get_last_versions(count=0)
+        if wf_versions:
+            content = get_parent(obj, IWorkflowManagedContent)
+            workflow = query_utility(IWorkflow, name=content.workflow_name)
+            result += '<i class="fa fa-fw fa-caret-right margin-top-2 pull-left" ' \
+                      'data-ams-url="PyAMS_workflow.versions_list.switchVersions"></i>'
+            result += '<span class="title">{0}</span>'.format(II18n(wf_versions[0]).query_attribute(self.name_field))
+            result += '<ul class="hidden versions">'
+            translate = self.request.localizer.translate
+            for version in wf_versions[:3]:
+                state = IWorkflowState(version)
+                result += '<li><a data-ams-url="{url}">{version}</a></li>'.format(
+                    url=absolute_url(version, self.request, 'admin.html'),
+                    version=translate(_("Version {version} ({state} - last update {date})")).format(
+                        version=state.version_id,
+                        state=translate(workflow.states.getTerm(state.state).title),
+                        date=format_datetime(tztime(IZopeDublinCore(version).modified))))
+            if len(wf_versions) > 3:
+                result += '<li class="nodot"><span data-ams-url="PyAMS_workflow.versions_list.switchAllVersions">' \
+                          '<i class="fa fa-fw fa-caret-right margin-top-2 pull-left"></i>' + \
+                          translate(_("Older versions")) + '</span>'
+                result += '<ul class="hidden old_versions"></ul></li>'
+            result += '</ul>'
+        return result
+
+
+@view_config(name='get-old-versions.html', context=IContainer, request_type=IPyAMSLayer,
+             permission='system.view', xhr=True)
+def get_old_versions(request):
+    """Get old_versions of template"""
+    container = request.context
+    element = container.get(request.params.get('element_name'))
+    ignore = int(request.params.get('ignore', 3))
+    translate = request.localizer.translate
+    result = []
+    content = get_parent(element, IWorkflowManagedContent)
+    workflow = query_utility(IWorkflow, name=content.workflow_name)
+    for version in IWorkflowVersions(element).get_last_versions(count=0)[ignore:]:
+        state = IWorkflowState(version)
+        result += '<li><a data-ams-url="{url}">{version}</a></li>'.format(
+            url=absolute_url(version, request, 'admin.html'),
+            version=translate(_("Version {version} ({state} - last update {date})")).format(
+                version=state.version_id,
+                state=translate(workflow.states.getTerm(state.state).title),
+                date=format_datetime(tztime(IZopeDublinCore(version).modified))))
+    return Response(''.join(result))