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 |
|
14 You can write your own *URLPathEvaluator* class to handle custom paths. |
|
15 For instance, if you want */my-card-id* to redirect to the corresponding |
|
16 card's primary view, you would write: |
|
17 |
|
18 .. sourcecode:: python |
|
19 |
|
20 class CardWikiidEvaluator(URLPathEvaluator): |
|
21 priority = 3 # make it be evaluated *before* RestPathEvaluator |
|
22 |
|
23 def evaluate_path(self, req, segments): |
|
24 if len(segments) != 1: |
|
25 raise PathDontMatch() |
|
26 rset = req.execute('Any C WHERE C wikiid %(w)s', |
|
27 {'w': segments[0]}) |
|
28 if len(rset) == 0: |
|
29 # Raise NotFound if no card is found |
|
30 raise PathDontMatch() |
|
31 return None, rset |
|
32 |
|
33 On the other hand, you can also deactivate some of the standard |
|
34 evaluators in your final application. The only thing you have to |
|
35 do is to unregister them, for instance in a *registration_callback* |
|
36 in your cube: |
|
37 |
|
38 .. sourcecode:: python |
|
39 |
|
40 def registration_callback(vreg): |
|
41 vreg.unregister(RestPathEvaluator) |
|
42 |
|
43 You can even replace the :class:`cubicweb.web.views.urlpublishing.URLPublisherComponent` |
|
44 class if you want to customize the whole toolchain process or if you want |
|
45 to plug into an early enough extension point to control your request |
|
46 parameters: |
|
47 |
|
48 .. sourcecode:: python |
|
49 |
|
50 class SanitizerPublisherComponent(URLPublisherComponent): |
|
51 """override default publisher component to explicitly ignore |
|
52 unauthorized request parameters in anonymous mode. |
|
53 """ |
|
54 unauthorized_form_params = ('rql', 'vid', '__login', '__password') |
|
55 |
|
56 def process(self, req, path): |
|
57 if req.session.anonymous_session: |
|
58 self._remove_unauthorized_params(req) |
|
59 return super(SanitizerPublisherComponent, self).process(req, path) |
|
60 |
|
61 def _remove_unauthorized_params(self, req): |
|
62 for param in req.form.keys(): |
|
63 if param in self.unauthorized_form_params: |
|
64 req.form.pop(param) |
|
65 |
|
66 |
|
67 def registration_callback(vreg): |
|
68 vreg.register_and_replace(SanitizerPublisherComponent, URLPublisherComponent) |
|
69 |
|
70 |
|
71 .. autoclass:: cubicweb.web.views.urlpublishing.RawPathEvaluator |
|
72 .. autoclass:: cubicweb.web.views.urlpublishing.EidPathEvaluator |
|
73 .. autoclass:: cubicweb.web.views.urlpublishing.URLRewriteEvaluator |
|
74 .. autoclass:: cubicweb.web.views.urlpublishing.RestPathEvaluator |
|
75 .. autoclass:: cubicweb.web.views.urlpublishing.ActionPathEvaluator |
|
76 |
|
77 URL rewriting |
|
78 ------------- |
|
79 |
|
80 (:mod:`cubicweb.web.views.urlrewrite`) |
|
81 |
|
82 .. autoclass:: cubicweb.web.views.urlrewrite.URLRewriter |
|
83 :members: |
|
84 |
|
85 .. autoclass:: cubicweb.web.views.urlrewrite.SimpleReqRewriter |
|
86 :members: |
|
87 |
|
88 .. autoclass:: cubicweb.web.views.urlrewrite.SchemaBasedRewriter |
|
89 :members: |
|
90 |
|
91 |
|
92 ``SimpleReqRewriter`` is enough for a certain number of simple cases. If it is not sufficient, ``SchemaBasedRewriter`` allows to do more elaborate things. |
|
93 |
|
94 Here is an example of ``SimpleReqRewriter`` usage with plain string: |
|
95 |
|
96 .. sourcecode:: python |
|
97 |
|
98 from cubicweb.web.views.urlrewrite import SimpleReqRewriter |
|
99 class TrackerSimpleReqRewriter(SimpleReqRewriter): |
|
100 rules = [ |
|
101 ('/versions', dict(vid='versionsinfo')), |
|
102 ] |
|
103 |
|
104 When the url is `<base_url>/versions`, the view with the __regid__ `versionsinfo` is displayed. |
|
105 |
|
106 Here is an example of ``SimpleReqRewriter`` usage with regular expressions: |
|
107 |
|
108 .. sourcecode:: python |
|
109 |
|
110 from cubicweb.web.views.urlrewrite import ( |
|
111 SimpleReqRewriter, rgx) |
|
112 |
|
113 class BlogReqRewriter(SimpleReqRewriter): |
|
114 rules = [ |
|
115 (rgx('/blogentry/([a-z_]+)\.rss'), |
|
116 dict(rql=('Any X ORDERBY CD DESC LIMIT 20 WHERE X is BlogEntry,' |
|
117 'X creation_date CD, X created_by U, ' |
|
118 'U login "%(user)s"' |
|
119 % {'user': r'\1'}), vid='rss')) |
|
120 ] |
|
121 |
|
122 When a url matches the regular expression, the view with the __regid__ |
|
123 `rss` which match the result set is displayed. |
|
124 |
|
125 Here is an example of ``SchemaBasedRewriter`` usage: |
|
126 |
|
127 .. sourcecode:: python |
|
128 |
|
129 from cubicweb.web.views.urlrewrite import ( |
|
130 SchemaBasedRewriter, rgx, build_rset) |
|
131 |
|
132 class TrackerURLRewriter(SchemaBasedRewriter): |
|
133 rules = [ |
|
134 (rgx('/project/([^/]+)/([^/]+)/tests'), |
|
135 build_rset(rql='Version X WHERE X version_of P, P name %(project)s, X num %(num)s', |
|
136 rgxgroups=[('project', 1), ('num', 2)], vid='versiontests')), |
|
137 ] |
|