pyramid_cubicweb/bwcompat.py
changeset 11578 fcba04437236
parent 11575 97110b4af42f
child 11588 50e1fda83837
equal deleted inserted replaced
11577:694d043dabc1 11578:fcba04437236
       
     1 import sys
       
     2 
     1 from pyramid import security
     3 from pyramid import security
     2 from pyramid import tweens
     4 from pyramid import tweens
     3 from pyramid.httpexceptions import HTTPSeeOther
     5 from pyramid.httpexceptions import HTTPSeeOther
     4 from pyramid import httpexceptions
     6 from pyramid import httpexceptions
     5 
     7 
     6 import cubicweb
     8 import cubicweb
     7 import cubicweb.web
     9 import cubicweb.web
     8 
    10 
     9 from cubicweb.web.application import CubicWebPublisher
    11 from cubicweb.web.application import CubicWebPublisher
    10 
    12 
    11 from cubicweb.web import LogOut
    13 from cubicweb.web import LogOut, PublishException
    12 
    14 
    13 from pyramid_cubicweb.core import cw_to_pyramid
    15 from pyramid_cubicweb.core import cw_to_pyramid
    14 
    16 
    15 
    17 
    16 class PyramidSessionHandler(object):
    18 class PyramidSessionHandler(object):
    90             except cubicweb.web.RemoteCallFailed as ex:
    92             except cubicweb.web.RemoteCallFailed as ex:
    91                 # XXX The default pyramid error handler (or one that we provide
    93                 # XXX The default pyramid error handler (or one that we provide
    92                 # for this exception) should be enough
    94                 # for this exception) should be enough
    93                 # content = self.appli.ajax_error_handler(req, ex)
    95                 # content = self.appli.ajax_error_handler(req, ex)
    94                 raise
    96                 raise
    95             except cubicweb.web.NotFound as ex:
       
    96                 raise httpexceptions.HTTPNotFound(ex.message)
       
    97 
       
    98             if content is not None:
    97             if content is not None:
    99                 request.response.body = content
    98                 request.response.body = content
   100 
    99 
   101             # XXX CubicWebPyramidRequest.headers_out should
   100             # XXX CubicWebPyramidRequest.headers_out should
   102             # access directly the pyramid response headers.
   101             # access directly the pyramid response headers.
   117                 content = vreg['views'].main_template(req, 'login')
   116                 content = vreg['views'].main_template(req, 'login')
   118                 request.response.body = content
   117                 request.response.body = content
   119 
   118 
   120         return request.response
   119         return request.response
   121 
   120 
       
   121     def error_handler(self, exc, request):
       
   122         req = request.cw_request
       
   123         if isinstance(exc, httpexceptions.HTTPException):
       
   124             request.response = exc
       
   125         elif isinstance(exc, PublishException) and exc.status is not None:
       
   126             request.response = httpexceptions.exception_response(exc.status)
       
   127         else:
       
   128             request.response = httpexceptions.HTTPInternalServerError()
       
   129         request.response.cache_control = 'no-cache'
       
   130         vreg = request.registry['cubicweb.registry']
       
   131         excinfo = sys.exc_info()
       
   132         req.reset_message()
       
   133         if req.ajax_request:
       
   134             content = self.appli.ajax_error_handler(req, exc)
       
   135         else:
       
   136             try:
       
   137                 req.data['ex'] = exc
       
   138                 errview = vreg['views'].select('error', req)
       
   139                 template = self.appli.main_template_id(req)
       
   140                 content = vreg['views'].main_template(req, template, view=errview)
       
   141             except Exception:
       
   142                 content = vreg['views'].main_template(req, 'error-template')
       
   143         request.response.body = content
       
   144         return request.response
       
   145 
   122 
   146 
   123 class TweenHandler(object):
   147 class TweenHandler(object):
   124     """ A Pyramid tween handler that submit unhandled requests to a Cubicweb
   148     """ A Pyramid tween handler that submit unhandled requests to a Cubicweb
   125     handler.
   149     handler.
   126 
   150 
   170     config.registry['cubicweb.appli'] = cwappli
   194     config.registry['cubicweb.appli'] = cwappli
   171     config.registry['cubicweb.handler'] = cwhandler
   195     config.registry['cubicweb.handler'] = cwhandler
   172 
   196 
   173     config.add_tween(
   197     config.add_tween(
   174         'pyramid_cubicweb.bwcompat.TweenHandler', under=tweens.EXCVIEW)
   198         'pyramid_cubicweb.bwcompat.TweenHandler', under=tweens.EXCVIEW)
       
   199     config.add_view(cwhandler.error_handler, context=Exception)
       
   200     # XXX why do i need this?
       
   201     config.add_view(cwhandler.error_handler, context=httpexceptions.HTTPForbidden)