author | Florent Cayre <florent.cayre@logilab.fr> |
Thu, 09 Jan 2014 10:53:45 +0100 | |
changeset 9384 | 430859af32c6 |
parent 8748 | f5027f8d2478 |
child 9700 | da7d341cca76 |
child 9719 | d02c8d77ac56 |
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 |
||
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8272
diff
changeset
|
62 |
from cubicweb import RegistryException |
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 |
8272
18893bb969d2
[web application] move default path logic inside url publisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8039
diff
changeset
|
109 |
:param path: the path of the resource to publish. If empty, None or "/" |
18893bb969d2
[web application] move default path logic inside url publisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8039
diff
changeset
|
110 |
"view" is used as the default path. |
0 | 111 |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4252
diff
changeset
|
112 |
:rtype: tuple(str, `cubicweb.rset.ResultSet` or None) |
0 | 113 |
: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
|
114 |
|
0 | 115 |
:raise NotFound: if no handler is able to decode the given path |
116 |
""" |
|
117 |
parts = [part for part in path.split('/') |
|
118 |
if part != ''] or (self.default_method,) |
|
119 |
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
|
120 |
if parts[0] in self.vreg['controllers']: |
0 | 121 |
return parts[0], None |
122 |
return 'view', None |
|
123 |
for evaluator in self.evaluators: |
|
124 |
try: |
|
125 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
126 |
break |
|
127 |
except PathDontMatch: |
|
128 |
continue |
|
129 |
else: |
|
130 |
raise NotFound(path) |
|
131 |
if pmid is None: |
|
132 |
pmid = self.default_method |
|
133 |
return pmid, rset |
|
134 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
135 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
136 |
class URLPathEvaluator(component.Component): |
0 | 137 |
__abstract__ = True |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2890
diff
changeset
|
138 |
__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
|
139 |
vreg = None # XXX necessary until property for deprecation warning is on appobject |
0 | 140 |
|
141 |
def __init__(self, urlpublisher): |
|
142 |
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
|
143 |
self.vreg = urlpublisher.vreg |
0 | 144 |
|
6175 | 145 |
|
0 | 146 |
class RawPathEvaluator(URLPathEvaluator): |
147 |
"""handle path of the form:: |
|
148 |
||
149 |
<publishing_method>?parameters... |
|
150 |
""" |
|
151 |
priority = 0 |
|
152 |
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
|
153 |
if len(parts) == 1 and parts[0] in self.vreg['controllers']: |
0 | 154 |
return parts[0], None |
155 |
raise PathDontMatch() |
|
156 |
||
157 |
||
158 |
class EidPathEvaluator(URLPathEvaluator): |
|
159 |
"""handle path with the form:: |
|
160 |
||
161 |
<eid> |
|
162 |
""" |
|
163 |
priority = 1 |
|
164 |
def evaluate_path(self, req, parts): |
|
165 |
if len(parts) != 1: |
|
166 |
raise PathDontMatch() |
|
167 |
try: |
|
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8272
diff
changeset
|
168 |
rset = req.execute('Any X WHERE X eid %(x)s', {'x': int(parts[0])}) |
0 | 169 |
except ValueError: |
170 |
raise PathDontMatch() |
|
171 |
if rset.rowcount == 0: |
|
172 |
raise NotFound() |
|
173 |
return None, rset |
|
174 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
175 |
|
0 | 176 |
class RestPathEvaluator(URLPathEvaluator): |
177 |
"""handle path with the form:: |
|
178 |
||
179 |
<etype>[[/<attribute name>]/<attribute value>]* |
|
180 |
""" |
|
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
|
181 |
priority = 3 |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
182 |
|
0 | 183 |
def evaluate_path(self, req, parts): |
184 |
if not (0 < len(parts) < 4): |
|
185 |
raise PathDontMatch() |
|
186 |
try: |
|
2273
daf6e178659f
new case_insensitive_etypes resource on the cubicweb registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
187 |
etype = self.vreg.case_insensitive_etypes[parts.pop(0).lower()] |
0 | 188 |
except KeyError: |
189 |
raise PathDontMatch() |
|
2664
1578e1a57828
[we, test] more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
190 |
cls = self.vreg['etypes'].etype_class(etype) |
0 | 191 |
if parts: |
192 |
if len(parts) == 2: |
|
193 |
attrname = parts.pop(0).lower() |
|
194 |
try: |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
195 |
cls.e_schema.subjrels[attrname] |
0 | 196 |
except KeyError: |
197 |
raise PathDontMatch() |
|
198 |
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
|
199 |
attrname = cls.cw_rest_attr_info()[0] |
0 | 200 |
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
|
201 |
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
|
202 |
return self.handle_etype(req, cls) |
30b6beb38820
[urlpublishing] refactor to ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
203 |
|
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
|
204 |
def set_vid_for_rset(self, req, cls, rset):# cls is there to ease overriding |
0 | 205 |
if rset.rowcount == 0: |
206 |
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
|
207 |
# 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
|
208 |
# 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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
|
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 |
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
|
215 |
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
|
216 |
self.set_vid_for_rset(req, cls, rset) |
0 | 217 |
return None, rset |
218 |
||
6253
30b6beb38820
[urlpublishing] refactor to ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
'x', 'Substitute') |
0 | 223 |
if attrname == 'eid': |
224 |
try: |
|
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8272
diff
changeset
|
225 |
rset = req.execute(st.as_string(), {'x': int(value)}) |
0 | 226 |
except (ValueError, TypeResolverException): |
227 |
# conflicting eid/type |
|
228 |
raise PathDontMatch() |
|
229 |
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
|
230 |
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
|
231 |
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
|
232 |
return None, rset |
0 | 233 |
|
234 |
||
235 |
class URLRewriteEvaluator(URLPathEvaluator): |
|
236 |
"""tries to find a rewrite rule to apply |
|
237 |
||
238 |
URL rewrite rule definitions are stored in URLRewriter objects |
|
239 |
""" |
|
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
|
240 |
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
|
241 |
|
0 | 242 |
def evaluate_path(self, req, parts): |
243 |
# uri <=> req._twreq.path or req._twreq.uri |
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
for rewritercls in evaluators: |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2887
diff
changeset
|
248 |
rewriter = rewritercls(req) |
0 | 249 |
try: |
250 |
# XXX we might want to chain url rewrites |
|
251 |
return rewriter.rewrite(req, uri) |
|
252 |
except KeyError: |
|
253 |
continue |
|
254 |
raise PathDontMatch() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
255 |
|
0 | 256 |
|
257 |
class ActionPathEvaluator(URLPathEvaluator): |
|
258 |
"""handle path with the form:: |
|
259 |
||
260 |
<any evaluator path>/<action> |
|
261 |
""" |
|
262 |
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
|
263 |
|
0 | 264 |
def evaluate_path(self, req, parts): |
265 |
if len(parts) < 2: |
|
266 |
raise PathDontMatch() |
|
267 |
# remove last part and see if this is something like an actions |
|
268 |
# if so, call |
|
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2664
diff
changeset
|
269 |
# XXX bad smell: refactor to simpler code |
0 | 270 |
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
|
271 |
actionsreg = self.vreg['actions'] |
0 | 272 |
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
|
273 |
actions = actionsreg[requested] |
0 | 274 |
except RegistryException: |
275 |
raise PathDontMatch() |
|
276 |
for evaluator in self.urlpublisher.evaluators: |
|
277 |
if evaluator is self or evaluator.priority == 0: |
|
278 |
continue |
|
279 |
try: |
|
280 |
pmid, rset = evaluator.evaluate_path(req, parts[:]) |
|
281 |
except PathDontMatch: |
|
282 |
continue |
|
283 |
else: |
|
284 |
try: |
|
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2664
diff
changeset
|
285 |
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
|
286 |
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
|
287 |
raise Redirect(action.url()) |
0 | 288 |
except RegistryException: |
7272
771f594c12a2
[vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6961
diff
changeset
|
289 |
pass # continue searching |
0 | 290 |
raise PathDontMatch() |