author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 03 Nov 2011 11:18:37 +0100 | |
changeset 8046 | bdd555df7a91 |
parent 8039 | 1fe90d4ec307 |
child 8272 | 18893bb969d2 |
permissions | -rw-r--r-- |
6961
686c59dfc401
[manage view] cleanup manage view and user menu
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6856
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5301
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
18 |
"""Associate url's path to view identifier / rql queries. |
0 | 19 |
|
8039
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
20 |
CubicWeb finds all registered URLPathEvaluators, orders them according |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
21 |
to their ``priority`` attribute and calls their ``evaluate_path()`` |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
22 |
method. The first that returns something and doesn't raise a |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
23 |
``PathDontMatch`` exception wins. |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
24 |
|
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
25 |
Here is the default evaluator chain: |
0 | 26 |
|
8039
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
27 |
1. :class:`cubicweb.web.views.urlpublishing.RawPathEvaluator` handles |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
28 |
unique url segments that match exactly one of the registered |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
29 |
controller's *__regid__*. Urls such as */view?*, */edit?*, */json?* |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
30 |
fall in that category; |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
31 |
|
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
32 |
2. :class:`cubicweb.web.views.urlpublishing.EidPathEvaluator` handles |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
33 |
unique url segments that are eids (e.g. */1234*); |
0 | 34 |
|
8039
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
35 |
3. :class:`cubicweb.web.views.urlpublishing.URLRewriteEvaluator` |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
36 |
selects all urlrewriter components, sorts them according to their |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
37 |
priorty, call their ``rewrite()`` method, the first one that |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
38 |
doesn't raise a ``KeyError`` wins. This is where the |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
39 |
:mod:`cubicweb.web.views.urlrewrite` and |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
40 |
:class:`cubicweb.web.views.urlrewrite.SimpleReqRewriter` comes into |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
41 |
play; |
0 | 42 |
|
8039
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
43 |
4. :class:`cubicweb.web.views.urlpublishing.RestPathEvaluator` handles |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
44 |
urls based on entity types and attributes : <etype>((/<attribute |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
45 |
name>])?/<attribute value>)? This is why ``cwuser/carlos`` works; |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
46 |
|
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
47 |
5. :class:`cubicweb.web.views.urlpublishing.ActionPathEvaluator` |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
48 |
handles any of the previous paths with an additional trailing |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
49 |
"/<action>" segment, <action> being one of the registered actions' |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
50 |
__regid__. |
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
51 |
|
0 | 52 |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
53 |
.. note:: |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
54 |
|
8039
1fe90d4ec307
[doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7828
diff
changeset
|
55 |
Actionpath executes a query whose results is lost |
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
56 |
because of redirecting instead of direct traversal. |
0 | 57 |
""" |
58 |
__docformat__ = "restructuredtext en" |
|
59 |
||
60 |
from rql import TypeResolverException |
|
61 |
||
62 |
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
|
63 |
from cubicweb.web import NotFound, Redirect, component |
0 | 64 |
|
65 |
||
66 |
class PathDontMatch(Exception): |
|
67 |
"""exception used by url evaluators to notify they can't evaluate |
|
68 |
a path |
|
69 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
70 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
71 |
class URLPublisherComponent(component.Component): |
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
72 |
"""Associate url path to view identifier / rql queries, by |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
73 |
applying a chain of urlpathevaluator components. |
0 | 74 |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
75 |
An evaluator is a URLPathEvaluator subclass with an .evaluate_path |
0 | 76 |
method taking the request object and the path to publish as |
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
77 |
argument. It will either return a publishing method identifier |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
78 |
and an rql query on success or raise a `PathDontMatch` exception |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
79 |
on failure. URL evaluators are called according to their |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
80 |
`priority` attribute, with 0 as the greatest priority and greater |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
81 |
values as lower priority. The first evaluator returning a result |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
82 |
or raising something else than `PathDontMatch` will stop the |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
83 |
handlers chain. |
0 | 84 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2890
diff
changeset
|
85 |
__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
|
86 |
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
|
87 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
88 |
def __init__(self, vreg, default_method='view'): |
0 | 89 |
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
|
90 |
self.vreg = vreg |
0 | 91 |
self.default_method = default_method |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
92 |
evaluators = [] |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
93 |
for evaluatorcls in vreg['components']['urlpathevaluator']: |
0 | 94 |
# instantiation needed |
95 |
evaluator = evaluatorcls(self) |
|
96 |
evaluators.append(evaluator) |
|
97 |
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
|
98 |
|
0 | 99 |
def process(self, req, path): |
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
100 |
"""Given an url (essentialy caracterized by a path on the |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
101 |
server, but additional information may be found in the request |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
102 |
object), return a publishing method identifier |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
103 |
(e.g. controller) and an optional result set. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
104 |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
105 |
:type req: `cubicweb.web.request.CubicWebRequestBase` |
0 | 106 |
:param req: the request object |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
107 |
|
0 | 108 |
:type path: str |
109 |
:param path: the path of the resource to publish |
|
110 |
||
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
111 |
:rtype: tuple(str, `cubicweb.rset.ResultSet` or None) |
0 | 112 |
: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
|
113 |
|
0 | 114 |
:raise NotFound: if no handler is able to decode the given path |
115 |
""" |
|
116 |
parts = [part for part in path.split('/') |
|
117 |
if part != ''] or (self.default_method,) |
|
118 |
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
|
119 |
if parts[0] in self.vreg['controllers']: |
0 | 120 |
return parts[0], None |
121 |
return 'view', None |
|
122 |
for evaluator in self.evaluators: |
|
123 |
try: |
|
124 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
125 |
break |
|
126 |
except PathDontMatch: |
|
127 |
continue |
|
128 |
else: |
|
129 |
raise NotFound(path) |
|
130 |
if pmid is None: |
|
131 |
pmid = self.default_method |
|
132 |
return pmid, rset |
|
133 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
134 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
135 |
class URLPathEvaluator(component.Component): |
0 | 136 |
__abstract__ = True |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2890
diff
changeset
|
137 |
__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
|
138 |
vreg = None # XXX necessary until property for deprecation warning is on appobject |
0 | 139 |
|
140 |
def __init__(self, urlpublisher): |
|
141 |
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
|
142 |
self.vreg = urlpublisher.vreg |
0 | 143 |
|
6175 | 144 |
|
0 | 145 |
class RawPathEvaluator(URLPathEvaluator): |
146 |
"""handle path of the form:: |
|
147 |
||
148 |
<publishing_method>?parameters... |
|
149 |
""" |
|
150 |
priority = 0 |
|
151 |
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
|
152 |
if len(parts) == 1 and parts[0] in self.vreg['controllers']: |
0 | 153 |
return parts[0], None |
154 |
raise PathDontMatch() |
|
155 |
||
156 |
||
157 |
class EidPathEvaluator(URLPathEvaluator): |
|
158 |
"""handle path with the form:: |
|
159 |
||
160 |
<eid> |
|
161 |
""" |
|
162 |
priority = 1 |
|
163 |
def evaluate_path(self, req, parts): |
|
164 |
if len(parts) != 1: |
|
165 |
raise PathDontMatch() |
|
166 |
try: |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
167 |
rset = req.execute('Any X WHERE X eid %(x)s', {'x': typed_eid(parts[0])}) |
0 | 168 |
except ValueError: |
169 |
raise PathDontMatch() |
|
170 |
if rset.rowcount == 0: |
|
171 |
raise NotFound() |
|
172 |
return None, rset |
|
173 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
174 |
|
0 | 175 |
class RestPathEvaluator(URLPathEvaluator): |
176 |
"""handle path with the form:: |
|
177 |
||
178 |
<etype>[[/<attribute name>]/<attribute value>]* |
|
179 |
""" |
|
6856
ac092197c099
[url publishing] evaluate url rewrite rules before rest path so one may easily override schema based urls
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6274
diff
changeset
|
180 |
priority = 3 |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
181 |
|
0 | 182 |
def evaluate_path(self, req, parts): |
183 |
if not (0 < len(parts) < 4): |
|
184 |
raise PathDontMatch() |
|
185 |
try: |
|
2273
daf6e178659f
new case_insensitive_etypes resource on the cubicweb registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
186 |
etype = self.vreg.case_insensitive_etypes[parts.pop(0).lower()] |
0 | 187 |
except KeyError: |
188 |
raise PathDontMatch() |
|
2664
1578e1a57828
[we, test] more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
189 |
cls = self.vreg['etypes'].etype_class(etype) |
0 | 190 |
if parts: |
191 |
if len(parts) == 2: |
|
192 |
attrname = parts.pop(0).lower() |
|
193 |
try: |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
194 |
cls.e_schema.subjrels[attrname] |
0 | 195 |
except KeyError: |
196 |
raise PathDontMatch() |
|
197 |
else: |
|
7828
749e1a67987f
[entity] rename Entity._rest_attr_info into cw_rest_attr_info (closes #1942759)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7794
diff
changeset
|
198 |
attrname = cls.cw_rest_attr_info()[0] |
0 | 199 |
value = req.url_unquote(parts.pop(0)) |
6253
30b6beb38820
[urlpublishing] refactor to ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
200 |
return self.handle_etype_attr(req, cls, attrname, value) |
30b6beb38820
[urlpublishing] refactor to ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
201 |
return self.handle_etype(req, cls) |
30b6beb38820
[urlpublishing] refactor to ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
202 |
|
6274
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
203 |
def set_vid_for_rset(self, req, cls, rset):# cls is there to ease overriding |
0 | 204 |
if rset.rowcount == 0: |
205 |
raise NotFound() |
|
6274
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
206 |
# we've to set a default vid here, since vid_from_rset may try to use a |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
207 |
# table view if fetch_rql include some non final relation |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
208 |
if rset.rowcount == 1: |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
209 |
req.form.setdefault('vid', 'primary') |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
210 |
else: # rset.rowcount >= 1 |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
211 |
req.form.setdefault('vid', 'sameetypelist') |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
212 |
|
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
213 |
def handle_etype(self, req, cls): |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
214 |
rset = req.execute(cls.fetch_rql(req.user)) |
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
215 |
self.set_vid_for_rset(req, cls, rset) |
0 | 216 |
return None, rset |
217 |
||
6253
30b6beb38820
[urlpublishing] refactor to ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
218 |
def handle_etype_attr(self, req, cls, attrname, value): |
7794
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7272
diff
changeset
|
219 |
st = cls.fetch_rqlst(req.user, ordermethod=None) |
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7272
diff
changeset
|
220 |
st.add_constant_restriction(st.get_variable('X'), attrname, |
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7272
diff
changeset
|
221 |
'x', 'Substitute') |
0 | 222 |
if attrname == 'eid': |
223 |
try: |
|
7794
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7272
diff
changeset
|
224 |
rset = req.execute(st.as_string(), {'x': typed_eid(value)}) |
0 | 225 |
except (ValueError, TypeResolverException): |
226 |
# conflicting eid/type |
|
227 |
raise PathDontMatch() |
|
228 |
else: |
|
7794
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7272
diff
changeset
|
229 |
rset = req.execute(st.as_string(), {'x': value}) |
6274
72bf3ec4068b
[url publishing] fix regression introduced by refactoring in 3.9.7, leading to table view being choosen when one would expect the primary view. Fix similar pb with /<etype> url and list view (this one has been there for a while)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6253
diff
changeset
|
230 |
self.set_vid_for_rset(req, cls, rset) |
6253
30b6beb38820
[urlpublishing] refactor to ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
231 |
return None, rset |
0 | 232 |
|
233 |
||
234 |
class URLRewriteEvaluator(URLPathEvaluator): |
|
235 |
"""tries to find a rewrite rule to apply |
|
236 |
||
237 |
URL rewrite rule definitions are stored in URLRewriter objects |
|
238 |
""" |
|
6856
ac092197c099
[url publishing] evaluate url rewrite rules before rest path so one may easily override schema based urls
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6274
diff
changeset
|
239 |
priority = 2 |
ac092197c099
[url publishing] evaluate url rewrite rules before rest path so one may easily override schema based urls
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6274
diff
changeset
|
240 |
|
0 | 241 |
def evaluate_path(self, req, parts): |
242 |
# uri <=> req._twreq.path or req._twreq.uri |
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
for rewritercls in evaluators: |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2887
diff
changeset
|
247 |
rewriter = rewritercls(req) |
0 | 248 |
try: |
249 |
# XXX we might want to chain url rewrites |
|
250 |
return rewriter.rewrite(req, uri) |
|
251 |
except KeyError: |
|
252 |
continue |
|
253 |
raise PathDontMatch() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
254 |
|
0 | 255 |
|
256 |
class ActionPathEvaluator(URLPathEvaluator): |
|
257 |
"""handle path with the form:: |
|
258 |
||
259 |
<any evaluator path>/<action> |
|
260 |
""" |
|
261 |
priority = 4 |
|
6856
ac092197c099
[url publishing] evaluate url rewrite rules before rest path so one may easily override schema based urls
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6274
diff
changeset
|
262 |
|
0 | 263 |
def evaluate_path(self, req, parts): |
264 |
if len(parts) < 2: |
|
265 |
raise PathDontMatch() |
|
266 |
# remove last part and see if this is something like an actions |
|
267 |
# if so, call |
|
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2664
diff
changeset
|
268 |
# XXX bad smell: refactor to simpler code |
0 | 269 |
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
|
270 |
actionsreg = self.vreg['actions'] |
0 | 271 |
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
|
272 |
actions = actionsreg[requested] |
0 | 273 |
except RegistryException: |
274 |
raise PathDontMatch() |
|
275 |
for evaluator in self.urlpublisher.evaluators: |
|
276 |
if evaluator is self or evaluator.priority == 0: |
|
277 |
continue |
|
278 |
try: |
|
279 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
280 |
except PathDontMatch: |
|
281 |
continue |
|
282 |
else: |
|
283 |
try: |
|
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2664
diff
changeset
|
284 |
action = actionsreg._select_best(actions, req, rset=rset) |
7272
771f594c12a2
[vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6961
diff
changeset
|
285 |
if action is not None: |
771f594c12a2
[vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6961
diff
changeset
|
286 |
raise Redirect(action.url()) |
0 | 287 |
except RegistryException: |
7272
771f594c12a2
[vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6961
diff
changeset
|
288 |
pass # continue searching |
0 | 289 |
raise PathDontMatch() |