author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 20 Aug 2009 17:52:22 +0200 | |
branch | 3.5 |
changeset 2924 | b5aadbd3fc5b |
parent 2664 | 1578e1a57828 |
child 2770 | 356e9d7c356d |
child 3689 | deb13e88e037 |
permissions | -rw-r--r-- |
0 | 1 |
"""associate url's path to view identifier / rql queries |
2 |
||
3 |
It currently handle url's path with the forms |
|
4 |
||
5 |
* <publishing_method> |
|
6 |
||
7 |
* minimal REST publishing: |
|
8 |
* <eid> |
|
9 |
* <etype>[/<attribute name>/<attribute value>]* |
|
10 |
||
11 |
* folder navigation |
|
12 |
||
13 |
||
14 |
You can actually control URL (more exactly path) resolution using URL path |
|
15 |
evaluator. |
|
16 |
||
17 |
XXX actionpath and folderpath execute a query whose results is lost |
|
18 |
because of redirecting instead of direct traversal |
|
19 |
||
20 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
21 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 22 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
23 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 24 |
""" |
25 |
||
26 |
__docformat__ = "restructuredtext en" |
|
27 |
||
28 |
from rql import TypeResolverException |
|
29 |
||
30 |
from cubicweb import RegistryException, typed_eid |
|
31 |
from cubicweb.web import NotFound, Redirect |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
32 |
from cubicweb.web.component import Component, Component |
0 | 33 |
|
34 |
||
35 |
class PathDontMatch(Exception): |
|
36 |
"""exception used by url evaluators to notify they can't evaluate |
|
37 |
a path |
|
38 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
39 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
40 |
class URLPublisherComponent(Component): |
0 | 41 |
"""associate url's path to view identifier / rql queries, |
42 |
by applying a chain of urlpathevaluator components. |
|
43 |
||
44 |
An evaluator is a URLPathEvaluator subclass with a .evaluate_path |
|
45 |
method taking the request object and the path to publish as |
|
46 |
argument. It will either returns a publishing method identifier |
|
47 |
and a rql query on success or raises a `PathDontMatch` exception |
|
48 |
on failure. URL evaluators are called according to their `priority` |
|
49 |
attribute, with 0 as the greatest priority and greater values as |
|
50 |
lower priority. The first evaluator returning a result or raising |
|
51 |
something else than `PathDontMatch` will stop the handlers chain. |
|
52 |
""" |
|
53 |
id = 'urlpublisher' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
54 |
|
0 | 55 |
def __init__(self, default_method='view'): |
56 |
super(URLPublisherComponent, self).__init__() |
|
57 |
self.default_method = default_method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
58 |
evaluators = [] |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
59 |
for evaluatorcls in self.vreg['components']['urlpathevaluator']: |
0 | 60 |
# instantiation needed |
61 |
evaluator = evaluatorcls(self) |
|
62 |
evaluators.append(evaluator) |
|
63 |
self.evaluators = sorted(evaluators, key=lambda x: x.priority) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
64 |
|
0 | 65 |
def process(self, req, path): |
66 |
"""given an url (essentialy caracterized by a path on the server, |
|
67 |
but additional information may be found in the request object), return |
|
68 |
a publishing method identifier (eg controller) and an optional result |
|
69 |
set |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
70 |
|
0 | 71 |
:type req: `cubicweb.web.Request` |
72 |
:param req: the request object |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
73 |
|
0 | 74 |
:type path: str |
75 |
:param path: the path of the resource to publish |
|
76 |
||
77 |
:rtype: tuple(str, `cubicweb.common.utils.ResultSet` or None) |
|
78 |
:return: the publishing method identifier and an optional result set |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
79 |
|
0 | 80 |
:raise NotFound: if no handler is able to decode the given path |
81 |
""" |
|
82 |
parts = [part for part in path.split('/') |
|
83 |
if part != ''] or (self.default_method,) |
|
84 |
if req.form.get('rql'): |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
85 |
if parts[0] in self.vreg['controllers']: |
0 | 86 |
return parts[0], None |
87 |
return 'view', None |
|
88 |
for evaluator in self.evaluators: |
|
89 |
try: |
|
90 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
91 |
break |
|
92 |
except PathDontMatch: |
|
93 |
continue |
|
94 |
else: |
|
95 |
raise NotFound(path) |
|
96 |
if pmid is None: |
|
97 |
pmid = self.default_method |
|
98 |
return pmid, rset |
|
99 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
100 |
|
0 | 101 |
class URLPathEvaluator(Component): |
102 |
__abstract__ = True |
|
103 |
id = 'urlpathevaluator' |
|
104 |
||
105 |
def __init__(self, urlpublisher): |
|
106 |
self.urlpublisher = urlpublisher |
|
107 |
||
108 |
||
109 |
class RawPathEvaluator(URLPathEvaluator): |
|
110 |
"""handle path of the form:: |
|
111 |
||
112 |
<publishing_method>?parameters... |
|
113 |
""" |
|
114 |
priority = 0 |
|
115 |
def evaluate_path(self, req, parts): |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
116 |
if len(parts) == 1 and parts[0] in self.vreg['controllers']: |
0 | 117 |
return parts[0], None |
118 |
raise PathDontMatch() |
|
119 |
||
120 |
||
121 |
class EidPathEvaluator(URLPathEvaluator): |
|
122 |
"""handle path with the form:: |
|
123 |
||
124 |
<eid> |
|
125 |
""" |
|
126 |
priority = 1 |
|
127 |
def evaluate_path(self, req, parts): |
|
128 |
if len(parts) != 1: |
|
129 |
raise PathDontMatch() |
|
130 |
try: |
|
131 |
rset = req.execute('Any X WHERE X eid %(x)s', |
|
132 |
{'x': typed_eid(parts[0])}, 'x') |
|
133 |
except ValueError: |
|
134 |
raise PathDontMatch() |
|
135 |
if rset.rowcount == 0: |
|
136 |
raise NotFound() |
|
137 |
return None, rset |
|
138 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
139 |
|
0 | 140 |
class RestPathEvaluator(URLPathEvaluator): |
141 |
"""handle path with the form:: |
|
142 |
||
143 |
<etype>[[/<attribute name>]/<attribute value>]* |
|
144 |
""" |
|
145 |
priority = 2 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
146 |
|
0 | 147 |
def evaluate_path(self, req, parts): |
148 |
if not (0 < len(parts) < 4): |
|
149 |
raise PathDontMatch() |
|
150 |
try: |
|
2273
daf6e178659f
new case_insensitive_etypes resource on the cubicweb registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
151 |
etype = self.vreg.case_insensitive_etypes[parts.pop(0).lower()] |
0 | 152 |
except KeyError: |
153 |
raise PathDontMatch() |
|
2664
1578e1a57828
[we, test] more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
154 |
cls = self.vreg['etypes'].etype_class(etype) |
0 | 155 |
if parts: |
156 |
if len(parts) == 2: |
|
157 |
attrname = parts.pop(0).lower() |
|
158 |
try: |
|
159 |
cls.e_schema.subject_relation(attrname) |
|
160 |
except KeyError: |
|
161 |
raise PathDontMatch() |
|
162 |
else: |
|
163 |
attrname = cls._rest_attr_info()[0] |
|
164 |
value = req.url_unquote(parts.pop(0)) |
|
165 |
rset = self.attr_rset(req, etype, attrname, value) |
|
166 |
else: |
|
167 |
rset = self.cls_rset(req, cls) |
|
168 |
if rset.rowcount == 0: |
|
169 |
raise NotFound() |
|
170 |
return None, rset |
|
171 |
||
172 |
def cls_rset(self, req, cls): |
|
173 |
return req.execute(cls.fetch_rql(req.user)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
174 |
|
0 | 175 |
def attr_rset(self, req, etype, attrname, value): |
176 |
rql = u'Any X WHERE X is %s, X %s %%(x)s' % (etype, attrname) |
|
177 |
if attrname == 'eid': |
|
178 |
try: |
|
179 |
rset = req.execute(rql, {'x': typed_eid(value)}, 'x') |
|
180 |
except (ValueError, TypeResolverException): |
|
181 |
# conflicting eid/type |
|
182 |
raise PathDontMatch() |
|
183 |
else: |
|
184 |
rset = req.execute(rql, {'x': value}) |
|
185 |
return rset |
|
186 |
||
187 |
||
188 |
class URLRewriteEvaluator(URLPathEvaluator): |
|
189 |
"""tries to find a rewrite rule to apply |
|
190 |
||
191 |
URL rewrite rule definitions are stored in URLRewriter objects |
|
192 |
""" |
|
193 |
priority = 3 |
|
194 |
def evaluate_path(self, req, parts): |
|
195 |
# uri <=> req._twreq.path or req._twreq.uri |
|
196 |
uri = req.url_unquote('/' + '/'.join(parts)) |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
197 |
evaluators = sorted(self.vreg['urlrewriting'].all_objects(), |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
198 |
key=lambda x: x.priority, reverse=True) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
199 |
for rewritercls in evaluators: |
0 | 200 |
rewriter = rewritercls() |
201 |
try: |
|
202 |
# XXX we might want to chain url rewrites |
|
203 |
return rewriter.rewrite(req, uri) |
|
204 |
except KeyError: |
|
205 |
continue |
|
206 |
raise PathDontMatch() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
207 |
|
0 | 208 |
|
209 |
class ActionPathEvaluator(URLPathEvaluator): |
|
210 |
"""handle path with the form:: |
|
211 |
||
212 |
<any evaluator path>/<action> |
|
213 |
""" |
|
214 |
priority = 4 |
|
215 |
def evaluate_path(self, req, parts): |
|
216 |
if len(parts) < 2: |
|
217 |
raise PathDontMatch() |
|
218 |
# remove last part and see if this is something like an actions |
|
219 |
# if so, call |
|
220 |
try: |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
221 |
actionsreg = self.vreg['actions'] |
0 | 222 |
requested = parts.pop(-1) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
223 |
actions = actionsreg[requested] |
0 | 224 |
except RegistryException: |
225 |
raise PathDontMatch() |
|
226 |
for evaluator in self.urlpublisher.evaluators: |
|
227 |
if evaluator is self or evaluator.priority == 0: |
|
228 |
continue |
|
229 |
try: |
|
230 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
231 |
except PathDontMatch: |
|
232 |
continue |
|
233 |
else: |
|
234 |
try: |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
235 |
action = actionsreg.select_best(actions, req, rset=rset) |
0 | 236 |
except RegistryException: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
237 |
continue |
0 | 238 |
else: |
239 |
# XXX avoid redirect |
|
240 |
raise Redirect(action.url()) |
|
241 |
raise PathDontMatch() |