src/pyams_content/skin/routes.py
branchdev-dc
changeset 1086 3d259e1718ef
parent 1079 a5e56749ca3d
parent 1084 6b6a884fa28a
child 1087 978a2b9123b9
equal deleted inserted replaced
1079:a5e56749ca3d 1086:3d259e1718ef
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from hypatia.interfaces import ICatalog
       
    20 from pyams_sequence.interfaces import ISequentialIntIds
       
    21 from pyams_workflow.interfaces import IWorkflowVersions
       
    22 
       
    23 # import packages
       
    24 from hypatia.catalog import CatalogQuery
       
    25 from hypatia.query import Eq, Any
       
    26 from pyams_catalog.query import CatalogResultSet
       
    27 from pyams_content.workflow import VISIBLE_STATES
       
    28 from pyams_utils.registry import get_utility
       
    29 from pyams_utils.url import absolute_url, canonical_url
       
    30 from pyramid.exceptions import NotFound
       
    31 from pyramid.response import Response
       
    32 from pyramid.view import view_config
       
    33 
       
    34 
       
    35 @view_config(route_name='oid_access')
       
    36 def get_oid_access(request):
       
    37     """Get direct access to given OID
       
    38 
       
    39     This route can be used to get a direct access to a given content,
       
    40     just by submitting an URL like /+/{oid}, where {oid} is the "short"
       
    41     sequence OID.
       
    42     """
       
    43     oid = request.matchdict.get('oid')
       
    44     if oid:
       
    45         view_name = request.matchdict.get('view')
       
    46         sequence = get_utility(ISequentialIntIds)
       
    47         hex_oid = sequence.get_full_oid(oid)
       
    48         catalog = get_utility(ICatalog)
       
    49         params = Eq(catalog['oid'], hex_oid)
       
    50         if not view_name:
       
    51             params &= Any(catalog['workflow_state'], VISIBLE_STATES)
       
    52         results = list(CatalogResultSet(CatalogQuery(catalog).query(params)))
       
    53         if results:
       
    54             response = Response()
       
    55             response.status_code = 302
       
    56             if view_name:  # back-office access => last version
       
    57                 version = IWorkflowVersions(results[0]).get_last_versions()[0]
       
    58                 response.location = absolute_url(version, request, '/'.join(view_name))
       
    59             else:
       
    60                 version = results[0]
       
    61                 response.location = canonical_url(version, request)
       
    62             return response
       
    63     raise NotFound()