src/pyams_content/skin/routes.py
changeset 0 7c0001cacf8e
child 378 747671dffc66
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/skin/routes.py	Thu Oct 08 13:37:29 2015 +0200
@@ -0,0 +1,57 @@
+#
+# 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 hypatia.interfaces import ICatalog
+from pyams_sequence.interfaces import ISequentialIntIds
+from pyams_workflow.interfaces import IWorkflowVersions
+
+# import packages
+from hypatia.catalog import CatalogQuery
+from hypatia.query import Eq, Any
+from pyams_catalog.query import CatalogResultSet
+from pyams_content.workflow import VISIBLE_STATES
+from pyams_utils.registry import get_utility
+from pyams_utils.url import absolute_url
+from pyramid.exceptions import NotFound
+from pyramid.response import Response
+from pyramid.view import view_config
+
+
+@view_config(route_name='oid_access')
+def get_oid_access(request):
+    """Get direct access to given OID"""
+    oid = request.matchdict.get('oid')
+    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:
+            if view_name:  # back-office access => last version
+                version = IWorkflowVersions(results[0]).get_last_versions()[0]
+            else:
+                version = results[0]
+            response = Response()
+            response.status_code = 302
+            response.location = absolute_url(version, request, '/'.join(view_name))
+            return response
+    raise NotFound()