pyramid_cubicweb/__init__.py
changeset 11487 04252e9ff549
parent 11485 3905c9f06d0e
child 11489 433fd3af7386
equal deleted inserted replaced
11486:cadcedf11b7e 11487:04252e9ff549
       
     1 from contextlib import contextmanager
       
     2 from warnings import warn
       
     3 
       
     4 import rql
       
     5 
     1 from cubicweb.web.request import CubicWebRequestBase
     6 from cubicweb.web.request import CubicWebRequestBase
     2 from cubicweb.cwconfig import CubicWebConfiguration
     7 from cubicweb.cwconfig import CubicWebConfiguration
     3 from cubicweb import repoapi
     8 from cubicweb import repoapi
     4 
     9 
     5 import cubicweb
    10 import cubicweb
     6 import cubicweb.web
    11 import cubicweb.web
     7 
    12 
     8 from pyramid import security
    13 from pyramid import security, httpexceptions
     9 from pyramid.httpexceptions import HTTPSeeOther
    14 from pyramid.httpexceptions import HTTPSeeOther
    10 
    15 
    11 from pyramid_cubicweb import authplugin
    16 from pyramid_cubicweb import authplugin
    12 
    17 
    13 import weakref
       
    14 
       
    15 import logging
    18 import logging
    16 
    19 
    17 log = logging.getLogger(__name__)
    20 log = logging.getLogger(__name__)
       
    21 
       
    22 
       
    23 @contextmanager
       
    24 def cw_to_pyramid(request):
       
    25     """Wrap a call to the cubicweb API.
       
    26 
       
    27     All CW exceptions will be transformed into their pyramid equivalent.
       
    28     When needed, some CW reponse bits may be converted too (mainly headers)"""
       
    29     try:
       
    30         yield
       
    31     except cubicweb.web.Redirect as ex:
       
    32         assert 300 <= ex.status < 400
       
    33         raise httpexceptions.status_map[ex.status](ex.location)
       
    34     except cubicweb.web.StatusResponse as ex:
       
    35         warn('[3.16] StatusResponse is deprecated use req.status_out',
       
    36              DeprecationWarning, stacklevel=2)
       
    37         request.body = ex.content
       
    38         request.status_int = ex.status
       
    39     except cubicweb.web.Unauthorized as ex:
       
    40         raise httpexceptions.HTTPForbidden(
       
    41             request.cw_request._(
       
    42                 'You\'re not authorized to access this page. '
       
    43                 'If you think you should, please contact the site '
       
    44                 'administrator.'))
       
    45     except cubicweb.web.Forbidden:
       
    46         raise httpexceptions.HTTPForbidden(
       
    47             request.cw_request._(
       
    48                 'This action is forbidden. '
       
    49                 'If you think it should be allowed, please contact the site '
       
    50                 'administrator.'))
       
    51     except (rql.BadRQLQuery, cubicweb.web.RequestError) as ex:
       
    52         raise
    18 
    53 
    19 
    54 
    20 class CubicWebPyramidRequest(CubicWebRequestBase):
    55 class CubicWebPyramidRequest(CubicWebRequestBase):
    21     def __init__(self, request):
    56     def __init__(self, request):
    22         self._request = request
    57         self._request = request
    67     # XXX The select() function could, know how to handle a pyramid
   102     # XXX The select() function could, know how to handle a pyramid
    68     # request, and feed it directly to the views that supports it.
   103     # request, and feed it directly to the views that supports it.
    69     # On the other hand, we could refine the View concept and decide it works
   104     # On the other hand, we could refine the View concept and decide it works
    70     # with a cnx, and never with a WebRequest
   105     # with a cnx, and never with a WebRequest
    71 
   106 
    72     view = vreg['views'].select(vid, request.cw_request, **kwargs)
   107     with cw_to_pyramid(request):
    73 
   108         view = vreg['views'].select(vid, request.cw_request, **kwargs)
    74     view.set_stream()
   109         view.set_stream()
    75     view.render()
   110         view.render()
    76     return view._stream.getvalue()
   111         return view._stream.getvalue()
    77 
   112 
    78 
   113 
    79 def login(request):
   114 def login(request):
    80     repo = request.registry['cubicweb.repository']
   115     repo = request.registry['cubicweb.repository']
    81 
   116