author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 09 May 2012 17:15:44 +0200 | |
changeset 8394 | b8b23af60019 |
parent 8238 | 087bb529035c |
child 8632 | fa044b9157d7 |
permissions | -rw-r--r-- |
6962
220e32f058be
[management ui] add views to manage sources similarly to users
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6961
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
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:
5313
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5786
7c16a4e7a592
[test] fix stupd name error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
18 |
"""Rules based url rewriter component, to get configurable RESTful urls""" |
0 | 19 |
|
20 |
import re |
|
21 |
||
3856
1c9589e46b16
fix rgx_action to ensure eids given to .execute are correctly typed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3592
diff
changeset
|
22 |
from cubicweb import typed_eid |
7800
ea496a3ed703
[user management] users and groups management now really lead to that (closes #1931478)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7388
diff
changeset
|
23 |
from cubicweb.uilib import domid |
722 | 24 |
from cubicweb.appobject import AppObject |
0 | 25 |
|
26 |
||
27 |
def rgx(pattern, flags=0): |
|
6175 | 28 |
"""this is just a convenient shortcut to add the $ sign""" |
0 | 29 |
return re.compile(pattern+'$', flags) |
30 |
||
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
688
diff
changeset
|
31 |
class metarewriter(type): |
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7994
diff
changeset
|
32 |
"""auto-extend rules dictionary""" |
0 | 33 |
def __new__(mcs, name, bases, classdict): |
34 |
# collect baseclass' rules |
|
35 |
rules = [] |
|
36 |
ignore_baseclass_rules = classdict.get('ignore_baseclass_rules', False) |
|
37 |
if not ignore_baseclass_rules: |
|
38 |
for base in bases: |
|
39 |
rules[0:0] = getattr(base, 'rules', []) |
|
40 |
rules[0:0] = classdict.get('rules', []) |
|
41 |
inputs = set() |
|
42 |
for data in rules[:]: |
|
43 |
try: |
|
44 |
input, output, groups = data |
|
45 |
except ValueError: |
|
46 |
input, output = data |
|
47 |
if input in inputs: |
|
48 |
rules.remove( (input, output) ) |
|
49 |
else: |
|
50 |
inputs.add(input) |
|
51 |
classdict['rules'] = rules |
|
52 |
return super(metarewriter, mcs).__new__(mcs, name, bases, classdict) |
|
53 |
||
54 |
||
55 |
class URLRewriter(AppObject): |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
56 |
"""Base class for URL rewriters. |
0 | 57 |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
58 |
Url rewriters should have a `rules` dict that maps an input URI |
0 | 59 |
to something that should be used for rewriting. |
60 |
||
61 |
The actual logic that defines how the rules dict is used is implemented |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
62 |
in the `rewrite` method. |
0 | 63 |
|
64 |
A `priority` attribute might be used to indicate which rewriter |
|
65 |
should be tried first. The higher the priority is, the earlier the |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
66 |
rewriter will be tried. |
0 | 67 |
""" |
68 |
__metaclass__ = metarewriter |
|
69 |
__registry__ = 'urlrewriting' |
|
70 |
__abstract__ = True |
|
71 |
priority = 1 |
|
72 |
||
73 |
def rewrite(self, req, uri): |
|
74 |
raise NotImplementedError |
|
75 |
||
76 |
||
77 |
class SimpleReqRewriter(URLRewriter): |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
78 |
"""The SimpleReqRewriters uses a `rules` dict that maps input URI |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
79 |
(regexp or plain string) to a dictionary to update the request's |
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
80 |
form. |
0 | 81 |
|
5301
f4219a6e62e3
[doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4706
diff
changeset
|
82 |
If the input uri is a regexp, group substitution is allowed. |
0 | 83 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2748
diff
changeset
|
84 |
__regid__ = 'simple' |
0 | 85 |
|
86 |
rules = [ |
|
2748
d2fcf19bfb34
F [registry views] add _registry startup view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
87 |
('/_', dict(vid='manage')), |
d2fcf19bfb34
F [registry views] add _registry startup view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
88 |
('/_registry', dict(vid='registry')), |
d2fcf19bfb34
F [registry views] add _registry startup view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
89 |
# (rgx('/_([^/]+?)/?'), dict(vid=r'\1')), |
0 | 90 |
('/schema', dict(vid='schema')), |
91 |
('/index', dict(vid='index')), |
|
1879
cb3466e08d81
eproperty -> cwproperty, cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1433
diff
changeset
|
92 |
('/myprefs', dict(vid='propertiesform')), |
cb3466e08d81
eproperty -> cwproperty, cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1433
diff
changeset
|
93 |
('/siteconfig', dict(vid='systempropertiesform')), |
6961
686c59dfc401
[manage view] cleanup manage view and user menu
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6950
diff
changeset
|
94 |
('/siteinfo', dict(vid='siteinfo')), |
0 | 95 |
('/manage', dict(vid='manage')), |
96 |
('/notfound', dict(vid='404')), |
|
97 |
('/error', dict(vid='error')), |
|
2422
96da7dc42eb5
quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
('/sparql', dict(vid='sparql')), |
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4436
diff
changeset
|
99 |
('/processinfo', dict(vid='processinfo')), |
7800
ea496a3ed703
[user management] users and groups management now really lead to that (closes #1931478)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7388
diff
changeset
|
100 |
(rgx('/cwuser', re.I), dict(vid='cw.users-and-groups-management', |
ea496a3ed703
[user management] users and groups management now really lead to that (closes #1931478)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7388
diff
changeset
|
101 |
tab=domid('cw.users-management'))), |
ea496a3ed703
[user management] users and groups management now really lead to that (closes #1931478)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7388
diff
changeset
|
102 |
(rgx('/cwgroup', re.I), dict(vid='cw.users-and-groups-management', |
ea496a3ed703
[user management] users and groups management now really lead to that (closes #1931478)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7388
diff
changeset
|
103 |
tab=domid('cw.groups-management'))), |
7994
af3fb709c061
[management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7800
diff
changeset
|
104 |
(rgx('/cwsource', re.I), dict(vid='cw.sources-management')), |
4405
fe31bb5dada5
eschema view is gone away
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
105 |
# XXX should be case insensitive as 'create', but I would like to find another way than |
fe31bb5dada5
eschema view is gone away
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
106 |
# relying on the etype_selector |
fe31bb5dada5
eschema view is gone away
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
107 |
(rgx('/schema/([^/]+?)/?'), dict(vid='primary', rql=r'Any X WHERE X is CWEType, X name "\1"')), |
0 | 108 |
(rgx('/add/([^/]+?)/?'), dict(vid='creation', etype=r'\1')), |
109 |
(rgx('/doc/images/(.+?)/?'), dict(vid='wdocimages', fid=r'\1')), |
|
110 |
(rgx('/doc/?'), dict(vid='wdoc', fid=r'main')), |
|
111 |
(rgx('/doc/(.+?)/?'), dict(vid='wdoc', fid=r'\1')), |
|
112 |
(rgx('/changelog/?'), dict(vid='changelog')), |
|
113 |
] |
|
1433 | 114 |
|
0 | 115 |
def rewrite(self, req, uri): |
116 |
"""for each `input`, `output `in rules, if `uri` matches `input`, |
|
117 |
req's form is updated with `output` |
|
118 |
""" |
|
119 |
for data in self.rules: |
|
120 |
try: |
|
121 |
inputurl, infos, required_groups = data |
|
122 |
except ValueError: |
|
123 |
inputurl, infos = data |
|
124 |
required_groups = None |
|
125 |
if required_groups and not req.user.matching_groups(required_groups): |
|
126 |
continue |
|
127 |
if isinstance(inputurl, basestring): |
|
128 |
if inputurl == uri: |
|
129 |
req.form.update(infos) |
|
130 |
break |
|
131 |
elif inputurl.match(uri): # it's a regexp |
|
132 |
# XXX what about i18n ? (vtitle for instance) |
|
133 |
for param, value in infos.items(): |
|
134 |
if isinstance(value, basestring): |
|
1132 | 135 |
req.form[param] = inputurl.sub(value, uri) |
0 | 136 |
else: |
137 |
req.form[param] = value |
|
138 |
break |
|
139 |
else: |
|
140 |
self.debug("no simple rewrite rule found for %s", uri) |
|
141 |
raise KeyError(uri) |
|
142 |
return None, None |
|
143 |
||
144 |
||
145 |
def build_rset(rql, rgxgroups=None, cachekey=None, setuser=False, |
|
146 |
vid=None, vtitle=None, form={}, **kwargs): |
|
147 |
||
7388
dc319ece0bd6
[url rewriting] fix nasty bug in build_rset inner function: the same dictionary is used by all queries, hence triggering potentially important bugs on concurrent requests (this dictionary goes back and forth the repository)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6962
diff
changeset
|
148 |
def do_build_rset(inputurl, uri, req, schema, kwargs=kwargs): |
dc319ece0bd6
[url rewriting] fix nasty bug in build_rset inner function: the same dictionary is used by all queries, hence triggering potentially important bugs on concurrent requests (this dictionary goes back and forth the repository)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6962
diff
changeset
|
149 |
kwargs = kwargs.copy() |
0 | 150 |
if rgxgroups: |
151 |
match = inputurl.match(uri) |
|
152 |
for arg, group in rgxgroups: |
|
153 |
kwargs[arg] = match.group(group) |
|
154 |
req.form.update(form) |
|
155 |
if setuser: |
|
156 |
kwargs['u'] = req.user.eid |
|
157 |
if vid: |
|
158 |
req.form['vid'] = vid |
|
159 |
if vtitle: |
|
160 |
req.form['vtitle'] = req._(vtitle) % kwargs |
|
161 |
return None, req.execute(rql, kwargs, cachekey) |
|
162 |
return do_build_rset |
|
163 |
||
164 |
def update_form(**kwargs): |
|
165 |
def do_build_rset(inputurl, uri, req, schema): |
|
166 |
match = inputurl.match(uri) |
|
167 |
kwargs.update(match.groupdict()) |
|
168 |
req.form.update(kwargs) |
|
169 |
return None, None |
|
170 |
return do_build_rset |
|
171 |
||
172 |
def rgx_action(rql=None, args=None, cachekey=None, argsgroups=(), setuser=False, |
|
6393
7372100062e8
include fcayre's patch #1060507 to get extra rql query parameters from form content in rgx_action
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
173 |
form=None, formgroups=(), transforms={}, rqlformparams=(), controller=None): |
3865
f9778a00e47f
actually fix rgx_action
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3864
diff
changeset
|
174 |
def do_build_rset(inputurl, uri, req, schema, |
f9778a00e47f
actually fix rgx_action
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3864
diff
changeset
|
175 |
cachekey=cachekey # necessary to avoid UnboundLocalError |
f9778a00e47f
actually fix rgx_action
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3864
diff
changeset
|
176 |
): |
0 | 177 |
if rql: |
178 |
kwargs = args and args.copy() or {} |
|
179 |
if argsgroups: |
|
3856
1c9589e46b16
fix rgx_action to ensure eids given to .execute are correctly typed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3592
diff
changeset
|
180 |
if cachekey is not None and isinstance(cachekey, basestring): |
1c9589e46b16
fix rgx_action to ensure eids given to .execute are correctly typed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3592
diff
changeset
|
181 |
cachekey = (cachekey,) |
0 | 182 |
match = inputurl.match(uri) |
183 |
for key in argsgroups: |
|
184 |
value = match.group(key) |
|
185 |
try: |
|
186 |
kwargs[key] = transforms[key](value) |
|
187 |
except KeyError: |
|
188 |
kwargs[key] = value |
|
3865
f9778a00e47f
actually fix rgx_action
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3864
diff
changeset
|
189 |
if cachekey is not None and key in cachekey: |
3856
1c9589e46b16
fix rgx_action to ensure eids given to .execute are correctly typed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3592
diff
changeset
|
190 |
kwargs[key] = typed_eid(value) |
0 | 191 |
if setuser: |
192 |
kwargs['u'] = req.user.eid |
|
6393
7372100062e8
include fcayre's patch #1060507 to get extra rql query parameters from form content in rgx_action
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
193 |
for param in rqlformparams: |
7372100062e8
include fcayre's patch #1060507 to get extra rql query parameters from form content in rgx_action
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6175
diff
changeset
|
194 |
kwargs.setdefault(param, req.form.get(param)) |
0 | 195 |
rset = req.execute(rql, kwargs, cachekey) |
196 |
else: |
|
197 |
rset = None |
|
198 |
form2 = form and form.copy() or {} |
|
199 |
if formgroups: |
|
200 |
match = inputurl.match(uri) |
|
201 |
for key in formgroups: |
|
202 |
form2[key] = match.group(key) |
|
3537
73b5bec579ba
[B] urlrewite: basic patch for i18n vtitle
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
2748
diff
changeset
|
203 |
if "vtitle" in form2: |
3592
4c08780e54ed
[i18n] msgid tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3537
diff
changeset
|
204 |
form2['vtitle'] = req.__(form2['vtitle']) |
0 | 205 |
if form2: |
206 |
req.form.update(form2) |
|
207 |
return controller, rset |
|
208 |
return do_build_rset |
|
209 |
||
210 |
||
211 |
class SchemaBasedRewriter(URLRewriter): |
|
5313
c4fe397379c7
[doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
5301
diff
changeset
|
212 |
"""Here, the rules dict maps regexps or plain strings to callbacks |
c4fe397379c7
[doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
5301
diff
changeset
|
213 |
that will be called with inputurl, uri, req, schema as parameters. |
0 | 214 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2748
diff
changeset
|
215 |
__regid__ = 'schemabased' |
0 | 216 |
rules = [ |
217 |
# rgxp : callback |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
218 |
(rgx('/search/(.+)'), build_rset(rql=r'Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s', |
1433 | 219 |
rgxgroups=[('text', 1)])), |
0 | 220 |
] |
221 |
||
222 |
def rewrite(self, req, uri): |
|
223 |
# XXX this could be refacted with SimpleReqRewriter |
|
224 |
for data in self.rules: |
|
225 |
try: |
|
226 |
inputurl, callback, required_groups = data |
|
227 |
except ValueError: |
|
228 |
inputurl, callback = data |
|
229 |
required_groups = None |
|
230 |
if required_groups and not req.user.matching_groups(required_groups): |
|
231 |
continue |
|
232 |
if isinstance(inputurl, basestring): |
|
233 |
if inputurl == uri: |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
234 |
return callback(inputurl, uri, req, self._cw.vreg.schema) |
0 | 235 |
elif inputurl.match(uri): # it's a regexp |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
236 |
return callback(inputurl, uri, req, self._cw.vreg.schema) |
0 | 237 |
else: |
238 |
self.debug("no schemabased rewrite rule found for %s", uri) |
|
239 |
raise KeyError(uri) |