pyramid_cubicweb/__init__.py
changeset 11484 39768d122f97
parent 11482 151b8a4b9f3f
child 11485 3905c9f06d0e
equal deleted inserted replaced
11483:7b7ed56bf2fb 11484:39768d122f97
     1 from cubicweb.web.request import CubicWebRequestBase
     1 from cubicweb.web.request import CubicWebRequestBase
     2 from cubicweb.cwconfig import CubicWebConfiguration
     2 from cubicweb.cwconfig import CubicWebConfiguration
     3 from cubicweb.web.application import CubicWebPublisher
       
     4 from cubicweb import repoapi
     3 from cubicweb import repoapi
     5 
     4 
     6 import cubicweb
     5 import cubicweb
     7 import cubicweb.web
     6 import cubicweb.web
     8 
     7 
    59 
    58 
    60     def _get_status_out(self):
    59     def _get_status_out(self):
    61         return self._request.response.status_int
    60         return self._request.response.status_int
    62 
    61 
    63     status_out = property(_get_status_out, _set_status_out)
    62     status_out = property(_get_status_out, _set_status_out)
    64 
       
    65 
       
    66 class PyramidSessionHandler(object):
       
    67     """A CW Session handler that rely on the pyramid API to fetch the needed
       
    68     informations"""
       
    69 
       
    70     def __init__(self, appli):
       
    71         self.appli = appli
       
    72 
       
    73     def get_session(self, req):
       
    74         return req._request.cw_session
       
    75 
       
    76     def logout(self, req, goto_url):
       
    77         del req._request.session['cubicweb.sessionid']
       
    78         if not req.session.closed:
       
    79             req.session.repo.close(req.session.sessionid)
       
    80         for name, value in security.forget(req._request):
       
    81             req.headers_out.setHeader(name, value)
       
    82         raise cubicweb.web.LogOut(url=goto_url)
       
    83 
    63 
    84 
    64 
    85 def render_view(request, vid, **kwargs):
    65 def render_view(request, vid, **kwargs):
    86     vreg = request.registry['cubicweb.registry']
    66     vreg = request.registry['cubicweb.registry']
    87     # XXX The select() function could, know how to handle a pyramid
    67     # XXX The select() function could, know how to handle a pyramid
   124 
   104 
   125         response.headerlist.extend(headers)
   105         response.headerlist.extend(headers)
   126 
   106 
   127     response.text = render_view(request, 'login')
   107     response.text = render_view(request, 'login')
   128     return response
   108     return response
   129 
       
   130 
       
   131 class CubicWebPyramidHandler(object):
       
   132     def __init__(self, appli):
       
   133         self.appli = appli
       
   134 
       
   135     def __call__(self, request):
       
   136         req = request.cw_request()
       
   137         result = self.appli.handle_request(req, req.path)
       
   138         if result is not None:
       
   139             request.response.body = result
       
   140         request.response.headers.clear()
       
   141         for k, v in req.headers_out.getAllRawHeaders():
       
   142             for item in v:
       
   143                 request.response.headers.add(k, item)
       
   144         return request.response
       
   145 
   109 
   146 
   110 
   147 def _cw_cnx(request):
   111 def _cw_cnx(request):
   148     # XXX We should not need to use the session. A temporary one should be
   112     # XXX We should not need to use the session. A temporary one should be
   149     # enough. (by using repoapi.connect())
   113     # enough. (by using repoapi.connect())
   248     config.add_view(login, route_name='login')
   212     config.add_view(login, route_name='login')
   249 
   213 
   250     config.add_route('hello', '/hello')
   214     config.add_route('hello', '/hello')
   251     config.add_view(hello_world, route_name='hello')
   215     config.add_view(hello_world, route_name='hello')
   252 
   216 
   253     # Set up a defaut route to handle non-catched urls.
   217     config.include('pyramid_cubicweb.handler')
   254     # This is to keep legacy compatibility for cubes that makes use of the
       
   255     # cubicweb controllers.
       
   256     cwappli = CubicWebPublisher(
       
   257         cwconfig.repository(), cwconfig,
       
   258         session_handler_fact=PyramidSessionHandler)
       
   259     handler = CubicWebPyramidHandler(cwappli)
       
   260 
       
   261     config.registry['cubicweb.appli'] = cwappli
       
   262     config.registry['cubicweb.handler'] = handler
       
   263 
       
   264     config.add_notfound_view(handler)