# HG changeset patch # User Sylvain Thénault # Date 1248452791 -7200 # Node ID fbd1fd2ca3128e38390717d679a80c24ee35e2cd # Parent abcbadcc110ae3ba18ed68eb6c3d6bab3b254da1 #343624: access to ORM and views outside web engine diff -r abcbadcc110a -r fbd1fd2ca312 dbapi.py --- a/dbapi.py Fri Jul 24 17:59:49 2009 +0200 +++ b/dbapi.py Fri Jul 24 18:26:31 2009 +0200 @@ -13,6 +13,7 @@ from logging import getLogger from time import time, clock +from itertools import count from logilab.common.logging_ext import set_log_methods from cubicweb import ETYPE_NAME_MAP, ConnectionError, RequestSessionMixIn @@ -21,6 +22,12 @@ _MARKER = object() +def _fake_property_value(self, name): + try: + return super(dbapi.DBAPIRequest, self).property_value(name) + except KeyError: + return '' + class ConnectionProperties(object): def __init__(self, cnxtype=None, lang=None, close=True, log=False): self.cnxtype = cnxtype or 'pyro' @@ -394,7 +401,8 @@ raise ProgrammingError('Closed connection') return self._repo.get_schema() - def load_vobjects(self, cubes=_MARKER, subpath=None, expand=True, force_reload=None): + def load_vobjects(self, cubes=_MARKER, subpath=None, expand=True, + force_reload=None): config = self.vreg.config if cubes is _MARKER: cubes = self._repo.get_cubes() @@ -426,6 +434,31 @@ if self._repo.config.instance_hooks: hm.register_hooks(config.load_hooks(self.vreg)) + def use_web_compatible_requests(self, baseurl, sitetitle=None): + """monkey patch DBAPIRequest to fake a cw.web.request, so you should + able to call html views using rset from a simple dbapi connection. + + You should call `load_vobjects` 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.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.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) + # 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} + def source_defs(self): """Return the definition of sources used by the repository.