[doc/book] complete the urlrewrite sections with examples stable
authorStephanie Marcu <stephanie.marcu@logilab.fr>
Fri, 16 Apr 2010 16:43:41 +0200
branchstable
changeset 5313 c4fe397379c7
parent 5312 d2dbba898a96
child 5314 86e5abbebfaf
[doc/book] complete the urlrewrite sections with examples
doc/book/en/development/devweb/views/urlpublish.rst
web/views/urlrewrite.py
--- a/doc/book/en/development/devweb/views/urlpublish.rst	Fri Apr 16 16:40:25 2010 +0200
+++ b/doc/book/en/development/devweb/views/urlpublish.rst	Fri Apr 16 16:43:41 2010 +0200
@@ -25,3 +25,49 @@
    :members:
 
 
+``SimpleReqRewriter`` is enough for a certain number of simple cases. If it is not sufficient, ``SchemaBasedRewriter`` allows to do more elaborate things.
+
+Here is an example of ``SimpleReqRewriter`` usage with plain string:
+
+.. sourcecode:: python
+
+   from cubicweb.web.views.urlrewrite import SimpleReqRewriter
+   class TrackerSimpleReqRewriter(SimpleReqRewriter):
+       rules = [
+        ('/versions', dict(vid='versionsinfo')),
+        ]
+
+When the url is `<base_url>/versions`, the view with the __regid__ `versionsinfo` is displayed.
+
+Here is an example of ``SimpleReqRewriter`` usage with regular expressions:
+
+.. sourcecode:: python
+
+    from cubicweb.web.views.urlrewrite import (
+        SimpleReqRewriter, rgx)
+
+    class BlogReqRewriter(SimpleReqRewriter):
+        rules = [
+            (rgx('/blogentry/([a-z_]+)\.rss'),
+             dict(rql=('Any X ORDERBY CD DESC LIMIT 20 WHERE X is BlogEntry,'
+                       'X creation_date CD, X created_by U, '
+                       'U login "%(user)s"'
+                       % {'user': r'\1'}, vid='rss'))),
+            ]
+
+When a url matches the regular expression, the view with the __regid__
+`rss` which match the result set is displayed.
+
+Here is an example of ``SchemaBasedRewriter`` usage:
+
+.. sourcecode:: python
+
+    from cubicweb.web.views.urlrewrite import (
+        SchemaBasedRewriter, rgx, build_rset)
+
+    class TrackerURLRewriter(SchemaBasedRewriter):
+        rules = [
+            (rgx('/project/([^/]+)/([^/]+)/tests'),
+             build_rset(rql='Version X WHERE X version_of P, P name %(project)s, X num %(num)s',
+                        rgxgroups=[('project', 1), ('num', 2)], vid='versiontests')),
+            ]
--- a/web/views/urlrewrite.py	Fri Apr 16 16:40:25 2010 +0200
+++ b/web/views/urlrewrite.py	Fri Apr 16 16:43:41 2010 +0200
@@ -188,8 +188,8 @@
 
 
 class SchemaBasedRewriter(URLRewriter):
-    """Here, the rules dict maps regexps or plain strings to
-    callbacks that will be called with (input, uri, req, schema)
+    """Here, the rules dict maps regexps or plain strings to callbacks
+    that will be called with inputurl, uri, req, schema as parameters.
     """
     __regid__ = 'schemabased'
     rules = [