Update header links to related versions
authorThierry Florac <thierry.florac@onf.fr>
Fri, 08 Dec 2017 10:45:10 +0100
changeset 310 61c586146b50
parent 309 7f470b562ab8
child 311 28a39fd182ea
Update header links to related versions
src/pyams_content/shared/common/zmi/header.py
--- a/src/pyams_content/shared/common/zmi/header.py	Fri Dec 08 10:44:43 2017 +0100
+++ b/src/pyams_content/shared/common/zmi/header.py	Fri Dec 08 10:45:10 2017 +0100
@@ -116,46 +116,53 @@
         self.state_date = translate(_("since {date}")).format(date=format_datetime(state.state_date, request=request))
         # check for links to other versions
         links = []
+        published_versions = sorted(versions.get_versions(workflow.published_states),
+                                    key=lambda x: IWorkflowState(x).version_id)
         draft_versions = sorted(versions.get_versions(workflow.update_states),
                                 key=lambda x: IWorkflowState(x).version_id)
-        waiting_versions = sorted(versions.get_versions(workflow.waiting_states),
-                                  key=lambda x: IWorkflowState(x).version_id)
-        published_versions = sorted(versions.get_versions(workflow.published_states),
-                                    key=lambda x: IWorkflowState(x).version_id)
         if (state.version_id > 1) or (state.state != workflow.initial_state):
+            targets = set()
+            if published_versions:
+                target = published_versions[0]
+                if (target is not context) and (target not in targets):
+                    links.append({'title': translate(_("access published version")),
+                                  'href': absolute_url(target, request, 'admin'),
+                                  'css_class': 'text-danger'})
+                    targets.add(target)
             if draft_versions:
                 target = draft_versions[0]
-                if target is not context:
+                if (target is not context) and (target not in targets):
                     links.append({'title': translate(_("access new version")),
                                   'href': absolute_url(target, request, 'admin'),
                                   'css_class': 'text-danger'})
-            elif waiting_versions:
-                target = waiting_versions[-1]
-                if target is not context:
-                    links.append({'title': translate(_("access waiting version")),
-                                  'href': absolute_url(target, request, 'admin'),
-                                  'css_class': 'text-danger'})
-            if published_versions:
-                target = published_versions[0]
-                if target is not context:
-                    links.append({'title': translate(_("access published version")),
-                                  'href': absolute_url(target, request, 'admin'),
-                                  'css_class': 'text-danger'})
-            if state.state not in (workflow.retired_states | workflow.archived_states):
-                retired_versions = sorted(versions.get_versions(workflow.retired_states),
+                    targets.add(target)
+            else:
+                waiting_versions = sorted(versions.get_versions(workflow.waiting_states),
+                                          key=lambda x: IWorkflowState(x).version_id)
+                if waiting_versions:
+                    target = waiting_versions[-1]
+                    if (target is not context) and (target not in targets):
+                        links.append({'title': translate(_("access waiting version")),
+                                      'href': absolute_url(target, request, 'admin'),
+                                      'css_class': 'text-danger'})
+                        targets.add(target)
+            if (state.version_id > 1) and (state.state not in (workflow.retired_states | workflow.archived_states)):
+                retired_versions = sorted(filter(lambda x: IWorkflowState(x).version_id < state.version_id,
+                                                 versions.get_versions(workflow.retired_states)),
                                           key=lambda x: IWorkflowState(x).version_id)
                 if retired_versions:
                     target = retired_versions[-1]
-                    if target is not context:
+                    if (target is not context) and (target not in targets):
                         links.append({'title': translate(_("access retired version")),
                                       'href': absolute_url(target, request, 'admin'),
                                       'css_class': 'text-danger'})
                 else:
-                    archived_versions = sorted(versions.get_versions(workflow.archived_states),
+                    archived_versions = sorted(filter(lambda x: IWorkflowState(x).version_id < state.version_id,
+                                                      versions.get_versions(workflow.archived_states)),
                                                key=lambda x: IWorkflowState(x).version_id)
                     if archived_versions:
                         target = archived_versions[-1]
-                        if target is not context:
+                        if (target is not context) and (target not in targets):
                             links.append({'title': translate(_("access archived version")),
                                           'href': absolute_url(target, request, 'admin'),
                                           'css_class': 'text-danger'})