# HG changeset patch # User Nicolas Chauvat # Date 1292266619 -3600 # Node ID 6c1a960735f50d7efc4c19f49a4434e70e2d41ac # Parent a0d626cc538d58fbab68017b37eb9f6f12d3b553 add a rql directive to restructuredtext diff -r a0d626cc538d -r 6c1a960735f5 ext/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) diff -r a0d626cc538d -r 6c1a960735f5 ext/test/unittest_rest.py --- 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('anon' + '\n\n

\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'

cwuser_plural

admin
anon

\n') + if __name__ == '__main__': unittest_main()