author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 18 Jan 2010 19:21:30 +0100 | |
changeset 4252 | 6c4f109c2b03 |
parent 4023 | eae23c40627a |
parent 4212 | ab6573088b4a |
child 5174 | 78438ad513ca |
child 5301 | f4219a6e62e3 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
21 |
:copyright: 2001-2010 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 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
31 |
from cubicweb.web import NotFound, Redirect, component |
0 | 32 |
|
33 |
||
34 |
class PathDontMatch(Exception): |
|
35 |
"""exception used by url evaluators to notify they can't evaluate |
|
36 |
a path |
|
37 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
38 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
39 |
class URLPublisherComponent(component.Component): |
0 | 40 |
"""associate url's path to view identifier / rql queries, |
41 |
by applying a chain of urlpathevaluator components. |
|
42 |
||
43 |
An evaluator is a URLPathEvaluator subclass with a .evaluate_path |
|
44 |
method taking the request object and the path to publish as |
|
45 |
argument. It will either returns a publishing method identifier |
|
46 |
and a rql query on success or raises a `PathDontMatch` exception |
|
47 |
on failure. URL evaluators are called according to their `priority` |
|
48 |
attribute, with 0 as the greatest priority and greater values as |
|
49 |
lower priority. The first evaluator returning a result or raising |
|
50 |
something else than `PathDontMatch` will stop the handlers chain. |
|
51 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2890
diff
changeset
|
52 |
__regid__ = 'urlpublisher' |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
53 |
vreg = None # XXX necessary until property for deprecation warning is on appobject |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
54 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
55 |
def __init__(self, vreg, default_method='view'): |
0 | 56 |
super(URLPublisherComponent, self).__init__() |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
57 |
self.vreg = vreg |
0 | 58 |
self.default_method = default_method |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
59 |
evaluators = [] |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
60 |
for evaluatorcls in vreg['components']['urlpathevaluator']: |
0 | 61 |
# instantiation needed |
62 |
evaluator = evaluatorcls(self) |
|
63 |
evaluators.append(evaluator) |
|
64 |
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
|
65 |
|
0 | 66 |
def process(self, req, path): |
67 |
"""given an url (essentialy caracterized by a path on the server, |
|
68 |
but additional information may be found in the request object), return |
|
69 |
a publishing method identifier (eg controller) and an optional result |
|
70 |
set |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
71 |
|
0 | 72 |
:type req: `cubicweb.web.Request` |
73 |
:param req: the request object |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
74 |
|
0 | 75 |
:type path: str |
76 |
:param path: the path of the resource to publish |
|
77 |
||
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
78 |
:rtype: tuple(str, `cubicweb.utils.ResultSet` or None) |
0 | 79 |
: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
|
80 |
|
0 | 81 |
:raise NotFound: if no handler is able to decode the given path |
82 |
""" |
|
83 |
parts = [part for part in path.split('/') |
|
84 |
if part != ''] or (self.default_method,) |
|
85 |
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
|
86 |
if parts[0] in self.vreg['controllers']: |
0 | 87 |
return parts[0], None |
88 |
return 'view', None |
|
89 |
for evaluator in self.evaluators: |
|
90 |
try: |
|
91 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
92 |
break |
|
93 |
except PathDontMatch: |
|
94 |
continue |
|
95 |
else: |
|
96 |
raise NotFound(path) |
|
97 |
if pmid is None: |
|
98 |
pmid = self.default_method |
|
99 |
return pmid, rset |
|
100 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
101 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
102 |
class URLPathEvaluator(component.Component): |
0 | 103 |
__abstract__ = True |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2890
diff
changeset
|
104 |
__regid__ = 'urlpathevaluator' |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
105 |
vreg = None # XXX necessary until property for deprecation warning is on appobject |
0 | 106 |
|
107 |
def __init__(self, urlpublisher): |
|
108 |
self.urlpublisher = urlpublisher |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
109 |
self.vreg = urlpublisher.vreg |
0 | 110 |
|
111 |
class RawPathEvaluator(URLPathEvaluator): |
|
112 |
"""handle path of the form:: |
|
113 |
||
114 |
<publishing_method>?parameters... |
|
115 |
""" |
|
116 |
priority = 0 |
|
117 |
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
|
118 |
if len(parts) == 1 and parts[0] in self.vreg['controllers']: |
0 | 119 |
return parts[0], None |
120 |
raise PathDontMatch() |
|
121 |
||
122 |
||
123 |
class EidPathEvaluator(URLPathEvaluator): |
|
124 |
"""handle path with the form:: |
|
125 |
||
126 |
<eid> |
|
127 |
""" |
|
128 |
priority = 1 |
|
129 |
def evaluate_path(self, req, parts): |
|
130 |
if len(parts) != 1: |
|
131 |
raise PathDontMatch() |
|
132 |
try: |
|
133 |
rset = req.execute('Any X WHERE X eid %(x)s', |
|
134 |
{'x': typed_eid(parts[0])}, 'x') |
|
135 |
except ValueError: |
|
136 |
raise PathDontMatch() |
|
137 |
if rset.rowcount == 0: |
|
138 |
raise NotFound() |
|
139 |
return None, rset |
|
140 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
141 |
|
0 | 142 |
class RestPathEvaluator(URLPathEvaluator): |
143 |
"""handle path with the form:: |
|
144 |
||
145 |
<etype>[[/<attribute name>]/<attribute value>]* |
|
146 |
""" |
|
147 |
priority = 2 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
148 |
|
0 | 149 |
def evaluate_path(self, req, parts): |
150 |
if not (0 < len(parts) < 4): |
|
151 |
raise PathDontMatch() |
|
152 |
try: |
|
2273
daf6e178659f
new case_insensitive_etypes resource on the cubicweb registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
153 |
etype = self.vreg.case_insensitive_etypes[parts.pop(0).lower()] |
0 | 154 |
except KeyError: |
155 |
raise PathDontMatch() |
|
2664
1578e1a57828
[we, test] more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
156 |
cls = self.vreg['etypes'].etype_class(etype) |
0 | 157 |
if parts: |
158 |
if len(parts) == 2: |
|
159 |
attrname = parts.pop(0).lower() |
|
160 |
try: |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
161 |
cls.e_schema.subjrels[attrname] |
0 | 162 |
except KeyError: |
163 |
raise PathDontMatch() |
|
164 |
else: |
|
165 |
attrname = cls._rest_attr_info()[0] |
|
166 |
value = req.url_unquote(parts.pop(0)) |
|
167 |
rset = self.attr_rset(req, etype, attrname, value) |
|
168 |
else: |
|
169 |
rset = self.cls_rset(req, cls) |
|
170 |
if rset.rowcount == 0: |
|
171 |
raise NotFound() |
|
172 |
return None, rset |
|
173 |
||
174 |
def cls_rset(self, req, cls): |
|
175 |
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
|
176 |
|
0 | 177 |
def attr_rset(self, req, etype, attrname, value): |
178 |
rql = u'Any X WHERE X is %s, X %s %%(x)s' % (etype, attrname) |
|
179 |
if attrname == 'eid': |
|
180 |
try: |
|
181 |
rset = req.execute(rql, {'x': typed_eid(value)}, 'x') |
|
182 |
except (ValueError, TypeResolverException): |
|
183 |
# conflicting eid/type |
|
184 |
raise PathDontMatch() |
|
185 |
else: |
|
186 |
rset = req.execute(rql, {'x': value}) |
|
187 |
return rset |
|
188 |
||
189 |
||
190 |
class URLRewriteEvaluator(URLPathEvaluator): |
|
191 |
"""tries to find a rewrite rule to apply |
|
192 |
||
193 |
URL rewrite rule definitions are stored in URLRewriter objects |
|
194 |
""" |
|
195 |
priority = 3 |
|
196 |
def evaluate_path(self, req, parts): |
|
197 |
# uri <=> req._twreq.path or req._twreq.uri |
|
198 |
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
|
199 |
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
|
200 |
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
|
201 |
for rewritercls in evaluators: |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2887
diff
changeset
|
202 |
rewriter = rewritercls(req) |
0 | 203 |
try: |
204 |
# XXX we might want to chain url rewrites |
|
205 |
return rewriter.rewrite(req, uri) |
|
206 |
except KeyError: |
|
207 |
continue |
|
208 |
raise PathDontMatch() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
209 |
|
0 | 210 |
|
211 |
class ActionPathEvaluator(URLPathEvaluator): |
|
212 |
"""handle path with the form:: |
|
213 |
||
214 |
<any evaluator path>/<action> |
|
215 |
""" |
|
216 |
priority = 4 |
|
217 |
def evaluate_path(self, req, parts): |
|
218 |
if len(parts) < 2: |
|
219 |
raise PathDontMatch() |
|
220 |
# remove last part and see if this is something like an actions |
|
221 |
# if so, call |
|
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2664
diff
changeset
|
222 |
# XXX bad smell: refactor to simpler code |
0 | 223 |
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
|
224 |
actionsreg = self.vreg['actions'] |
0 | 225 |
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
|
226 |
actions = actionsreg[requested] |
0 | 227 |
except RegistryException: |
228 |
raise PathDontMatch() |
|
229 |
for evaluator in self.urlpublisher.evaluators: |
|
230 |
if evaluator is self or evaluator.priority == 0: |
|
231 |
continue |
|
232 |
try: |
|
233 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
234 |
except PathDontMatch: |
|
235 |
continue |
|
236 |
else: |
|
237 |
try: |
|
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2664
diff
changeset
|
238 |
action = actionsreg._select_best(actions, req, rset=rset) |
0 | 239 |
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
|
240 |
continue |
0 | 241 |
else: |
242 |
# XXX avoid redirect |
|
243 |
raise Redirect(action.url()) |
|
244 |
raise PathDontMatch() |