add a rql directive to restructuredtext stable
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Mon, 13 Dec 2010 19:56:59 +0100
branchstable
changeset 6938 6c1a960735f5
parent 6937 a0d626cc538d
child 6939 8fa55cf2a8cb
add a rql directive to restructuredtext
ext/rest.py
ext/test/unittest_rest.py
--- a/ext/rest.py	Thu Feb 03 18:18:31 2011 +0100
+++ b/ext/rest.py	Mon Dec 13 19:56:59 2010 +0100
@@ -47,6 +47,8 @@
 from cubicweb import UnknownEid
 from cubicweb.ext.html4zope import Writer
 
+from cubicweb.web.views import vid_from_rset # XXX better not to import c.w.views here...
+
 # We provide our own parser as an attempt to get rid of
 # state machine reinstanciation
 
@@ -93,6 +95,23 @@
     return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
                             **options)], []
 
+def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
+    """:rql:`Any X,Y WHERE X is CWUser, X login Y:table`"""
+    _cw = inliner.document.settings.context._cw
+    text = text.strip()
+    if ':' in text:
+        rql, vid = text.rsplit(u':', 1)
+        rql = rql.strip()
+    else:
+        rql, vid = text, None
+    _cw.ensure_ro_rql(rql)
+    rset = _cw.execute(rql, {'userid': _cw.user.eid})
+    if vid is None:
+        vid = vid_from_rset(_cw, rset, _cw.vreg.schema)
+    view = _cw.vreg['views'].select(vid, _cw, rset=rset)
+    content = view.render()
+    set_classes(options)
+    return [nodes.raw('', content, format='html')], []
 
 def winclude_directive(name, arguments, options, content, lineno,
                        content_offset, block_text, state, state_machine):
@@ -288,6 +307,7 @@
         return
     _INITIALIZED = True
     register_canonical_role('eid', eid_reference_role)
+    register_canonical_role('rql', rql_role)
     directives.register_directive('winclude', winclude_directive)
     if pygments_directive is not None:
         directives.register_directive('sourcecode', pygments_directive)
--- a/ext/test/unittest_rest.py	Thu Feb 03 18:18:31 2011 +0100
+++ b/ext/test/unittest_rest.py	Mon Dec 13 19:56:59 2010 +0100
@@ -56,5 +56,17 @@
 
 ''')
 
+
+    def test_rql_role_with_vid(self):
+        context = self.context()
+        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:table`')
+        self.assert_(out.endswith('<a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a>'
+                                  '</td></tr></tbody></table></div>\n</div>\n</p>\n'))
+
+    def test_rql_role_without_vid(self):
+        context = self.context()
+        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser`')
+        self.assertEqual(out, u'<p><h1>cwuser_plural</h1><div class="section"><a href="http://testing.fr/cubicweb/cwuser/admin" title="">admin</a></div><div class="section"><a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a></div></p>\n')
+
 if __name__ == '__main__':
     unittest_main()