pyramid_cubicweb/__init__.py
author Christophe de Vienne <christophe@unlish.com>
Thu, 28 Aug 2014 11:20:57 +0200
changeset 11480 79ac26923432
child 11482 151b8a4b9f3f
permissions -rw-r--r--
Initial implementation Set up a default route that passes requests to a cubicweb instance. The requests are wrapped in an adequate adapter so that cubicweb works with no change. Related to #4291173
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11480
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
from cubicweb.web.request import CubicWebRequestBase
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     2
from cubicweb.cwconfig import CubicWebConfiguration
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
from cubicweb.web.application import CubicWebPublisher
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
class CubicWebPyramidRequest(CubicWebRequestBase):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
    def __init__(self, request):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
        self._request = request
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
        self.path = request.upath_info
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
        vreg = request.registry['cubicweb.appli'].vreg
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
        https = request.scheme == 'https'
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
        post = request.params
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
        headers_in = request.headers
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
        super(CubicWebPyramidRequest, self).__init__(vreg, https, post,
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
                                                     headers=headers_in)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
    def is_secure(self):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
        return self._request.scheme == 'https'
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
    def relative_path(self, includeparams=True):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
        path = self._request.path[1:]
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
        if includeparams and self._request.query_string:
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
            return '%s?%s' % (path, self._request.query_string)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
        return path
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
    def instance_uri(self):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
        return self._request.application_url
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
    def get_full_path(self):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
        path = self._request.path
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
        if self._request.query_string:
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
            return '%s?%s' % (path, self._request.query_string)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
        return path
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
    def http_method(self):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
        return self._request.method
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    42
    def _set_status_out(self, value):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    43
        self._request.response.status_int = value
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    45
    def _get_status_out(self):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
        return self._request.response.status_int
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
    status_out = property(_get_status_out, _set_status_out)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
class CubicWebPyramidHandler(object):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
    def __init__(self, appli):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
        self.appli = appli
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
    def __call__(self, request):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
        req = CubicWebPyramidRequest(request)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
        request.response.body = self.appli.handle_request(req, req.path)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
        request.response.headers.clear()
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    59
        for k, v in req.headers_out.getAllRawHeaders():
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
            for item in v:
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    61
                request.response.headers.add(k, item)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    62
        return request.response
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    64
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    65
def includeme(config):
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    66
    appid = config.registry.settings['cubicweb.instance']
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    67
    cwconfig = CubicWebConfiguration.config_for(appid)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    68
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    69
    cwappli = CubicWebPublisher(cwconfig.repository(), cwconfig)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    70
    handler = CubicWebPyramidHandler(cwappli)
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
    config.registry['cubicweb.appli'] = cwappli
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    73
    config.registry['cubicweb.handler'] = handler
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    74
79ac26923432 Initial implementation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    75
    config.add_notfound_view(handler)