# HG changeset patch # User Sylvain Thénault # Date 1310055914 -7200 # Node ID 7dce83bc2df1ae1980d1113f2a7a84648e25896c # Parent c75a46918933a539a624bc2bbd6066fbd438942f [book, forms] talk about cnx.use_web_compatible_request instead of custom monkey-patching diff -r c75a46918933 -r 7dce83bc2df1 doc/book/en/devweb/edition/dissection.rst --- a/doc/book/en/devweb/edition/dissection.rst Thu Jul 07 18:25:07 2011 +0200 +++ b/doc/book/en/devweb/edition/dissection.rst Thu Jul 07 18:25:14 2011 +0200 @@ -10,44 +10,6 @@ .. _`tracker`: http://www.cubicweb.org/project/cubicweb-tracker -Patching the session object -~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -In order to play interactively with web side application objects, we -have to cheat a bit: we will decorate the session object with some -missing artifacts that should belong to a web request object. With -that we can instantiate and render forms interactively. - -The function below does the minimum to allow going through this -exercice. Some attributes or methods may be missing for other -purposes. It is nevertheless not complicated to enhance it if need -arises. - -.. sourcecode:: python - - def monkey_patch_session(session): - """ useful to use the cw shell session object - with web appobjects, which expect more than a plain - data repository session - """ - # for autoform selection - session.json_request = False - session.url = lambda: u'http://perdu.com' - session.session = session - session.form = {} - session.list_form_param = lambda *args: [] - # for render - session.use_fckeditor = lambda: False - session._ressources = [] - session.add_js = session.add_css = lambda *args: session._ressources.append(args) - session.external_resource = lambda x:{} - session._tabcount = 0 - def next_tabindex(): - session._tabcount += 1 - return session._tabcount - session.next_tabindex = next_tabindex - return session - Populating the database ~~~~~~~~~~~~~~~~~~~~~~~ @@ -71,10 +33,17 @@ .. sourcecode:: python - >>> monkey_patch_session(session) - >>> form = session.vreg['forms'].select('edition', session, rset=rql('Ticket T')) + >>> cnx.use_web_compatible_requests('http://fakeurl.com') + >>> req = cnx.request() + >>> form = req.vreg['forms'].select('edition', req, rset=rql('Ticket T')) >>> html = form.render() +.. note:: + + In order to play interactively with web side application objects, we have to + cheat a bit to have request object that will looks like HTTP request object, by + calling :meth:`use_web_compatible_requests()` on the connection. + This creates an automatic entity form. The ``.render()`` call yields an html (unicode) string. The html output is shown below (with internal fieldset omitted).