pyramid_cubicweb/__init__.py
changeset 11480 79ac26923432
child 11482 151b8a4b9f3f
equal deleted inserted replaced
11479:a070f211b35c 11480:79ac26923432
       
     1 from cubicweb.web.request import CubicWebRequestBase
       
     2 from cubicweb.cwconfig import CubicWebConfiguration
       
     3 from cubicweb.web.application import CubicWebPublisher
       
     4 
       
     5 
       
     6 class CubicWebPyramidRequest(CubicWebRequestBase):
       
     7     def __init__(self, request):
       
     8         self._request = request
       
     9 
       
    10         self.path = request.upath_info
       
    11 
       
    12         vreg = request.registry['cubicweb.appli'].vreg
       
    13         https = request.scheme == 'https'
       
    14 
       
    15         post = request.params
       
    16         headers_in = request.headers
       
    17 
       
    18         super(CubicWebPyramidRequest, self).__init__(vreg, https, post,
       
    19                                                      headers=headers_in)
       
    20 
       
    21     def is_secure(self):
       
    22         return self._request.scheme == 'https'
       
    23 
       
    24     def relative_path(self, includeparams=True):
       
    25         path = self._request.path[1:]
       
    26         if includeparams and self._request.query_string:
       
    27             return '%s?%s' % (path, self._request.query_string)
       
    28         return path
       
    29 
       
    30     def instance_uri(self):
       
    31         return self._request.application_url
       
    32 
       
    33     def get_full_path(self):
       
    34         path = self._request.path
       
    35         if self._request.query_string:
       
    36             return '%s?%s' % (path, self._request.query_string)
       
    37         return path
       
    38 
       
    39     def http_method(self):
       
    40         return self._request.method
       
    41 
       
    42     def _set_status_out(self, value):
       
    43         self._request.response.status_int = value
       
    44 
       
    45     def _get_status_out(self):
       
    46         return self._request.response.status_int
       
    47 
       
    48     status_out = property(_get_status_out, _set_status_out)
       
    49 
       
    50 
       
    51 class CubicWebPyramidHandler(object):
       
    52     def __init__(self, appli):
       
    53         self.appli = appli
       
    54 
       
    55     def __call__(self, request):
       
    56         req = CubicWebPyramidRequest(request)
       
    57         request.response.body = self.appli.handle_request(req, req.path)
       
    58         request.response.headers.clear()
       
    59         for k, v in req.headers_out.getAllRawHeaders():
       
    60             for item in v:
       
    61                 request.response.headers.add(k, item)
       
    62         return request.response
       
    63 
       
    64 
       
    65 def includeme(config):
       
    66     appid = config.registry.settings['cubicweb.instance']
       
    67     cwconfig = CubicWebConfiguration.config_for(appid)
       
    68 
       
    69     cwappli = CubicWebPublisher(cwconfig.repository(), cwconfig)
       
    70     handler = CubicWebPyramidHandler(cwappli)
       
    71 
       
    72     config.registry['cubicweb.appli'] = cwappli
       
    73     config.registry['cubicweb.handler'] = handler
       
    74 
       
    75     config.add_notfound_view(handler)