[dbapi] enhanced web compatible request when using dbapi
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 21 Oct 2010 18:38:36 +0200
changeset 6581 4a3b264589dc
parent 6580 798200432f50
child 6582 8eb7883b4223
[dbapi] enhanced web compatible request when using dbapi
dbapi.py
test/unittest_dbapi.py
web/request.py
--- a/dbapi.py	Thu Oct 21 18:07:44 2010 +0200
+++ b/dbapi.py	Thu Oct 21 18:38:36 2010 +0200
@@ -48,6 +48,9 @@
     except KeyError:
         return ''
 
+def fake(*args, **kwargs):
+    return None
+
 def multiple_connections_fix():
     """some monkey patching necessary when an application has to deal with
     several connections to different repositories. It tries to hide buggy class
@@ -480,6 +483,7 @@
         self.sessionid = cnxid
         self._close_on_del = getattr(cnxprops, 'close_on_del', True)
         self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro')
+        self._web_request = False
         if cnxprops and cnxprops.log_queries:
             self.executed_queries = []
             self.cursor_class = LogCursor
@@ -544,35 +548,32 @@
 
         You should call `load_appobjects` at some point to register those views.
         """
-        from cubicweb.web.request import CubicWebRequestBase as cwrb
-        DBAPIRequest.build_ajax_replace_url = cwrb.build_ajax_replace_url.im_func
-        DBAPIRequest.ajax_replace_url = cwrb.ajax_replace_url.im_func
-        DBAPIRequest.list_form_param = cwrb.list_form_param.im_func
         DBAPIRequest.property_value = _fake_property_value
         DBAPIRequest.next_tabindex = count().next
-        DBAPIRequest.form = {}
-        DBAPIRequest.data = {}
-        fake = lambda *args, **kwargs: None
         DBAPIRequest.relative_path = fake
         DBAPIRequest.url = fake
-        DBAPIRequest.next_tabindex = fake
         DBAPIRequest.get_page_data = fake
         DBAPIRequest.set_page_data = fake
-        DBAPIRequest.add_js = fake #cwrb.add_js.im_func
-        DBAPIRequest.add_css = fake #cwrb.add_css.im_func
         # XXX could ask the repo for it's base-url configuration
         self.vreg.config.set_option('base-url', baseurl)
+        self.vreg.config.uiprops = {}
+        self.vreg.config.datadir_url = baseurl + '/data'
         # XXX why is this needed? if really needed, could be fetched by a query
         if sitetitle is not None:
             self.vreg['propertydefs']['ui.site-title'] = {'default': sitetitle}
+        self._web_request = True
 
-    @check_not_closed
-    def source_defs(self):
-        """Return the definition of sources used by the repository.
-
-        This is NOT part of the DB-API.
-        """
-        return self._repo.source_defs()
+    def request(self):
+        if self._web_request:
+            from cubicweb.web.request import CubicWebRequestBase
+            req = CubicWebRequestBase(self.vreg, False)
+            req.get_header = lambda x, default=None: default
+            req.set_session = lambda session, user=None: DBAPIRequest.set_session(
+                req, session, user)
+        else:
+            req = DBAPIRequest(self.vreg)
+        req.set_session(DBAPISession(self))
+        return req
 
     @check_not_closed
     def user(self, req=None, props=None):
@@ -604,9 +605,6 @@
         # return a dict as bw compat trick
         return {'txid': currentThread().getName()}
 
-    def request(self):
-        return DBAPIRequest(self.vreg, DBAPISession(self))
-
     # session data methods #####################################################
 
     @check_not_closed
@@ -639,6 +637,11 @@
     # meta-data accessors ######################################################
 
     @check_not_closed
+    def source_defs(self):
+        """Return the definition of sources used by the repository."""
+        return self._repo.source_defs()
+
+    @check_not_closed
     def get_schema(self):
         """Return the schema currently used by the repository."""
         return self._repo.get_schema()
--- a/test/unittest_dbapi.py	Thu Oct 21 18:07:44 2010 +0200
+++ b/test/unittest_dbapi.py	Thu Oct 21 18:38:36 2010 +0200
@@ -18,9 +18,12 @@
 """unittest for cubicweb.dbapi"""
 
 from __future__ import with_statement
+
 from copy import copy
 
-from cubicweb import ConnectionError
+from logilab.common import tempattr
+
+from cubicweb import ConnectionError, cwconfig
 from cubicweb.dbapi import ProgrammingError
 from cubicweb.devtools.testlib import CubicWebTC
 
@@ -67,6 +70,13 @@
         self.assertRaises(ProgrammingError, cnx.set_shared_data, 'data', 0)
         self.assertRaises(ProgrammingError, cnx.get_shared_data, 'data')
 
+    def test_web_compatible_request(self):
+        config = cwconfig.CubicWebNoAppConfiguration()
+        with tempattr(self.cnx.vreg, 'config', config):
+            self.cnx.use_web_compatible_requests('http://perdu.com')
+            req = self.cnx.request()
+            self.assertEqual(req.base_url(), 'http://perdu.com')
+            req.ajax_replace_url('domid') # don't crash
 
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
--- a/web/request.py	Thu Oct 21 18:07:44 2010 +0200
+++ b/web/request.py	Thu Oct 21 18:38:36 2010 +0200
@@ -84,7 +84,6 @@
 
     def __init__(self, vreg, https, form=None):
         super(CubicWebRequestBase, self).__init__(vreg)
-        self.authmode = vreg.config['auth-mode']
         self.https = https
         if https:
             self.uiprops = vreg.config.https_uiprops
@@ -121,6 +120,10 @@
         self.html_headers.define_var('pageid', pid, override=False)
 
     @property
+    def authmode(self):
+        return self.vreg.config['auth-mode']
+
+    @property
     def varmaker(self):
         """the rql varmaker is exposed both as a property and as the
         set_varmaker function since we've two use cases: