1 .. -*- coding: utf-8 -*- |
|
2 |
|
3 URL publishing |
|
4 -------------- |
|
5 |
|
6 (:mod:`cubicweb.web.views.urlpublishing`) |
|
7 |
|
8 .. automodule:: cubicweb.web.views.urlpublishing |
|
9 |
|
10 .. autoclass:: cubicweb.web.views.urlpublishing.URLPublisherComponent |
|
11 :members: |
|
12 |
|
13 URL rewriting |
|
14 ------------- |
|
15 |
|
16 (:mod:`cubicweb.web.views.urlrewrite`) |
|
17 |
|
18 .. autoclass:: cubicweb.web.views.urlrewrite.URLRewriter |
|
19 :members: |
|
20 |
|
21 .. autoclass:: cubicweb.web.views.urlrewrite.SimpleReqRewriter |
|
22 :members: |
|
23 |
|
24 .. autoclass:: cubicweb.web.views.urlrewrite.SchemaBasedRewriter |
|
25 :members: |
|
26 |
|
27 |
|
28 ``SimpleReqRewriter`` is enough for a certain number of simple cases. If it is not sufficient, ``SchemaBasedRewriter`` allows to do more elaborate things. |
|
29 |
|
30 Here is an example of ``SimpleReqRewriter`` usage with plain string: |
|
31 |
|
32 .. sourcecode:: python |
|
33 |
|
34 from cubicweb.web.views.urlrewrite import SimpleReqRewriter |
|
35 class TrackerSimpleReqRewriter(SimpleReqRewriter): |
|
36 rules = [ |
|
37 ('/versions', dict(vid='versionsinfo')), |
|
38 ] |
|
39 |
|
40 When the url is `<base_url>/versions`, the view with the __regid__ `versionsinfo` is displayed. |
|
41 |
|
42 Here is an example of ``SimpleReqRewriter`` usage with regular expressions: |
|
43 |
|
44 .. sourcecode:: python |
|
45 |
|
46 from cubicweb.web.views.urlrewrite import ( |
|
47 SimpleReqRewriter, rgx) |
|
48 |
|
49 class BlogReqRewriter(SimpleReqRewriter): |
|
50 rules = [ |
|
51 (rgx('/blogentry/([a-z_]+)\.rss'), |
|
52 dict(rql=('Any X ORDERBY CD DESC LIMIT 20 WHERE X is BlogEntry,' |
|
53 'X creation_date CD, X created_by U, ' |
|
54 'U login "%(user)s"' |
|
55 % {'user': r'\1'}, vid='rss'))), |
|
56 ] |
|
57 |
|
58 When a url matches the regular expression, the view with the __regid__ |
|
59 `rss` which match the result set is displayed. |
|
60 |
|
61 Here is an example of ``SchemaBasedRewriter`` usage: |
|
62 |
|
63 .. sourcecode:: python |
|
64 |
|
65 from cubicweb.web.views.urlrewrite import ( |
|
66 SchemaBasedRewriter, rgx, build_rset) |
|
67 |
|
68 class TrackerURLRewriter(SchemaBasedRewriter): |
|
69 rules = [ |
|
70 (rgx('/project/([^/]+)/([^/]+)/tests'), |
|
71 build_rset(rql='Version X WHERE X version_of P, P name %(project)s, X num %(num)s', |
|
72 rgxgroups=[('project', 1), ('num', 2)], vid='versiontests')), |
|
73 ] |
|