src/pyams_default_theme/shared/common/oid.py
changeset 203 40bffafce365
child 284 aa70cb77cf43
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/shared/common/oid.py	Wed Nov 07 18:25:36 2018 +0100
@@ -0,0 +1,51 @@
+#
+# Copyright (c) 2008-2018 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'
+
+from pyramid.exceptions import NotFound
+from zope.interface import Interface
+from zope.traversing.interfaces import ITraversable
+
+from pyams_sequence.interfaces import ISequentialIntIds
+from pyams_sequence.reference import get_reference_target
+from pyams_skin.layer import IPyAMSUserLayer
+from pyams_utils.adapter import ContextRequestAdapter, adapter_config
+from pyams_utils.interfaces.url import DISPLAY_CONTEXT
+from pyams_utils.registry import get_utility
+from pyams_workflow.interfaces import IWorkflow, IWorkflowVersions
+
+
+@adapter_config(name='oid', context=(Interface, IPyAMSUserLayer), provides=ITraversable)
+class OidTraverser(ContextRequestAdapter):
+    """++oid++ traverser"""
+
+    def traverse(self, name, furtherpath=None):
+        if not name:
+            raise NotFound()
+        if '::' in name:
+            oid, label = name.split('::', 1)
+        else:
+            oid = name
+        sequence = get_utility(ISequentialIntIds)
+        reference = sequence.get_full_oid(oid)
+        target = get_reference_target(reference)
+        if target is not None:
+            workflow = IWorkflow(target, None)
+            if workflow is not None:
+                versions = IWorkflowVersions(target).get_versions(workflow.published_states, sort=True)
+                if versions:
+                    target = versions[-1]
+        if target is not None:
+            self.request.annotations[DISPLAY_CONTEXT] = self.context
+            return target
+        raise NotFound()