Initial implementation
authorChristophe de Vienne <christophe@unlish.com>
Thu, 28 Aug 2014 11:20:57 +0200
changeset 11480 79ac26923432
parent 11479 a070f211b35c
child 11481 10df276abb78
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
TODO.rst
pyramid_cubicweb/__init__.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TODO.rst	Thu Aug 28 11:20:57 2014 +0200
@@ -0,0 +1,44 @@
+Next steps
+----------
+
+Provide a ctl command
+~~~~~~~~~~~~~~~~~~~~~
+
+Add a 'pyramid' command for cubicweb-ctl that starts a cubicweb instance within
+a pyramid container.
+
+Transactions
+~~~~~~~~~~~~
+
+A common transaction handling mechanism should be used so that the connexion
+can be safely used in both pyramid and cubicweb.
+
+Authentication
+~~~~~~~~~~~~~~
+
+- Use cw as an authentication provider for the pyramid application.
+- allow the cw application to use pyramid for getting user identity.
+
+Cubicweb views
+~~~~~~~~~~~~~~
+
+Provide a simple api to call cubicweb views within pyramid views.
+
+Error handling
+~~~~~~~~~~~~~~
+
+Have pyramid handle errors (with cubicweb views if wanted) so that we can use
+the debuging tools.
+
+Reimplement the base controllers of cw
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+-   rest
+-   static
+-   data
+
+Bypass cw.handle_request in most case
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Use it only when no other mean works, which should provide backward compat of
+old cubes for a while.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyramid_cubicweb/__init__.py	Thu Aug 28 11:20:57 2014 +0200
@@ -0,0 +1,75 @@
+from cubicweb.web.request import CubicWebRequestBase
+from cubicweb.cwconfig import CubicWebConfiguration
+from cubicweb.web.application import CubicWebPublisher
+
+
+class CubicWebPyramidRequest(CubicWebRequestBase):
+    def __init__(self, request):
+        self._request = request
+
+        self.path = request.upath_info
+
+        vreg = request.registry['cubicweb.appli'].vreg
+        https = request.scheme == 'https'
+
+        post = request.params
+        headers_in = request.headers
+
+        super(CubicWebPyramidRequest, self).__init__(vreg, https, post,
+                                                     headers=headers_in)
+
+    def is_secure(self):
+        return self._request.scheme == 'https'
+
+    def relative_path(self, includeparams=True):
+        path = self._request.path[1:]
+        if includeparams and self._request.query_string:
+            return '%s?%s' % (path, self._request.query_string)
+        return path
+
+    def instance_uri(self):
+        return self._request.application_url
+
+    def get_full_path(self):
+        path = self._request.path
+        if self._request.query_string:
+            return '%s?%s' % (path, self._request.query_string)
+        return path
+
+    def http_method(self):
+        return self._request.method
+
+    def _set_status_out(self, value):
+        self._request.response.status_int = value
+
+    def _get_status_out(self):
+        return self._request.response.status_int
+
+    status_out = property(_get_status_out, _set_status_out)
+
+
+class CubicWebPyramidHandler(object):
+    def __init__(self, appli):
+        self.appli = appli
+
+    def __call__(self, request):
+        req = CubicWebPyramidRequest(request)
+        request.response.body = self.appli.handle_request(req, req.path)
+        request.response.headers.clear()
+        for k, v in req.headers_out.getAllRawHeaders():
+            for item in v:
+                request.response.headers.add(k, item)
+        return request.response
+
+
+def includeme(config):
+    appid = config.registry.settings['cubicweb.instance']
+    cwconfig = CubicWebConfiguration.config_for(appid)
+
+    cwappli = CubicWebPublisher(cwconfig.repository(), cwconfig)
+    handler = CubicWebPyramidHandler(cwappli)
+
+    config.registry['cubicweb.appli'] = cwappli
+    config.registry['cubicweb.handler'] = handler
+
+    config.add_notfound_view(handler)