Moved custom routes from PyAMS_content package
authorThierry Florac <thierry.florac@onf.fr>
Thu, 08 Nov 2018 08:47:18 +0100
changeset 205 1b2e282d3cf2
parent 204 713edde7dbf1
child 206 b3ba586c9e21
Moved custom routes from PyAMS_content package
src/pyams_default_theme/include.py
src/pyams_default_theme/routes.py
--- a/src/pyams_default_theme/include.py	Wed Nov 07 19:02:46 2018 +0100
+++ b/src/pyams_default_theme/include.py	Thu Nov 08 08:47:18 2018 +0100
@@ -26,5 +26,8 @@
     # add translations
     config.add_translation_dirs('pyams_default_theme:locales')
 
+    # add custom routes
+    config.add_route('oid_access', '/+/{oid}*view')
+
     # load registry components
     config.scan()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/routes.py	Thu Nov 08 08:47:18 2018 +0100
@@ -0,0 +1,58 @@
+#
+# 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 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_utils.registry import get_utility
+from pyams_utils.url import absolute_url, canonical_url
+from pyams_workflow.interfaces import IWorkflowVersions
+
+
+@view_config(route_name='oid_access')
+def get_oid_access(request):
+    """Get direct access to given OID
+
+    This route can be used to get a direct access to a given content,
+    just by submitting an URL like /+/{oid}, where {oid} is the "short"
+    sequence 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:
+            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))
+            else:
+                version = results[0]
+                response.location = canonical_url(version, request)
+            return response
+    raise NotFound()