Updated "++oid++" traverser and "/+/{oid}" route
authorThierry Florac <thierry.florac@onf.fr>
Mon, 10 Dec 2018 14:45:33 +0100
changeset 284 aa70cb77cf43
parent 283 b84939ee020e
child 285 f7a32a10787c
Updated "++oid++" traverser and "/+/{oid}" route
src/pyams_default_theme/routes.py
src/pyams_default_theme/shared/common/oid.py
--- a/src/pyams_default_theme/routes.py	Fri Dec 07 16:14:03 2018 +0100
+++ b/src/pyams_default_theme/routes.py	Mon Dec 10 14:45:33 2018 +0100
@@ -12,19 +12,15 @@
 
 __docformat__ = 'restructuredtext'
 
-from hypatia.catalog import CatalogQuery
-from hypatia.interfaces import ICatalog
-from hypatia.query import Any, Eq
 from pyramid.exceptions import NotFound
 from pyramid.response import Response
 from pyramid.view import view_config
 
-from pyams_catalog.query import CatalogResultSet
-from pyams_content.workflow import VISIBLE_STATES
 from pyams_sequence.interfaces import ISequentialIntIds
+from pyams_sequence.reference import get_reference_target
 from pyams_utils.registry import get_utility
 from pyams_utils.url import absolute_url, canonical_url
-from pyams_workflow.interfaces import IWorkflowVersions
+from pyams_workflow.interfaces import IWorkflow, IWorkflowPublicationInfo, IWorkflowVersions
 
 
 @view_config(route_name='oid_access')
@@ -39,20 +35,24 @@
     if oid:
         view_name = request.matchdict.get('view')
         sequence = get_utility(ISequentialIntIds)
-        hex_oid = sequence.get_full_oid(oid)
-        catalog = get_utility(ICatalog)
-        params = Eq(catalog['oid'], hex_oid)
-        if not view_name:
-            params &= Any(catalog['workflow_state'], VISIBLE_STATES)
-        results = list(CatalogResultSet(CatalogQuery(catalog).query(params)))
-        if results:
+        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, None)
+                if versions is not None:
+                    versions = versions.get_versions(workflow.published_states, sort=True)
+                    if versions:
+                        target = versions[-1]
+        if (target is not None) and not IWorkflowPublicationInfo(target).is_visible(request):
+            target = None
+        if target is not None:
             response = Response()
             response.status_code = 302
             if view_name:  # back-office access => last version
-                version = IWorkflowVersions(results[0]).get_last_versions()[0]
-                response.location = absolute_url(version, request, '/'.join(view_name))
+                response.location = absolute_url(target, request, '/'.join(view_name))
             else:
-                version = results[0]
-                response.location = canonical_url(version, request)
+                response.location = canonical_url(target, request)
             return response
     raise NotFound()
--- a/src/pyams_default_theme/shared/common/oid.py	Fri Dec 07 16:14:03 2018 +0100
+++ b/src/pyams_default_theme/shared/common/oid.py	Mon Dec 10 14:45:33 2018 +0100
@@ -22,7 +22,7 @@
 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
+from pyams_workflow.interfaces import IWorkflow, IWorkflowPublicationInfo, IWorkflowVersions
 
 
 @adapter_config(name='oid', context=(Interface, IPyAMSUserLayer), provides=ITraversable)
@@ -42,9 +42,13 @@
         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]
+                versions = IWorkflowVersions(target, None)
+                if versions is not None:
+                    versions = versions.get_versions(workflow.published_states, sort=True)
+                    if versions:
+                        target = versions[-1]
+        if (target is not None) and not IWorkflowPublicationInfo(target).is_visible(self.request):
+            target = None
         if target is not None:
             self.request.annotations[DISPLAY_CONTEXT] = self.context
             return target