src/pyams_content/skin/routes.py
changeset 0 7c0001cacf8e
child 378 747671dffc66
equal deleted inserted replaced
-1:000000000000 0:7c0001cacf8e
       
     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
       
    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     oid = request.matchdict.get('oid')
       
    39     if oid:
       
    40         view_name = request.matchdict.get('view')
       
    41         sequence = get_utility(ISequentialIntIds)
       
    42         hex_oid = sequence.get_full_oid(oid)
       
    43         catalog = get_utility(ICatalog)
       
    44         params = Eq(catalog['oid'], hex_oid)
       
    45         if not view_name:
       
    46             params &= Any(catalog['workflow_state'], VISIBLE_STATES)
       
    47         results = list(CatalogResultSet(CatalogQuery(catalog).query(params)))
       
    48         if results:
       
    49             if view_name:  # back-office access => last version
       
    50                 version = IWorkflowVersions(results[0]).get_last_versions()[0]
       
    51             else:
       
    52                 version = results[0]
       
    53             response = Response()
       
    54             response.status_code = 302
       
    55             response.location = absolute_url(version, request, '/'.join(view_name))
       
    56             return response
       
    57     raise NotFound()