author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 08 Apr 2010 10:00:22 +0200 | |
branch | stable |
changeset 5182 | 3bc8250731fe |
parent 5000 | f1a10b41417a |
child 5174 | 78438ad513ca |
child 5283 | 9ad0eaa09d34 |
permissions | -rw-r--r-- |
0 | 1 |
"""CubicWeb web client application object |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2849
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
: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:
1426
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
11 |
from time import clock, time |
|
12 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
13 |
from logilab.common.deprecation import deprecated |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
14 |
|
0 | 15 |
from rql import BadRQLQuery |
16 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
17 |
from cubicweb import set_log_methods, cwvreg |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
18 |
from cubicweb import ( |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
19 |
ValidationError, Unauthorized, AuthenticationError, NoSelectableObject, |
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
20 |
RepositoryError, CW_EVENT_MANAGER) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
21 |
from cubicweb.web import LOGGER, component |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
from cubicweb.web import ( |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
23 |
StatusResponse, DirectResponse, Redirect, NotFound, |
2293 | 24 |
RemoteCallFailed, ExplicitLogin, InvalidSession, RequestError) |
0 | 25 |
|
26 |
# make session manager available through a global variable so the debug view can |
|
27 |
# print information about web session |
|
28 |
SESSION_MANAGER = None |
|
29 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
30 |
class AbstractSessionManager(component.Component): |
0 | 31 |
"""manage session data associated to a session identifier""" |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2887
diff
changeset
|
32 |
__regid__ = 'sessionmanager' |
1426 | 33 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
34 |
def __init__(self, vreg): |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
35 |
self.session_time = vreg.config['http-session-time'] or None |
0 | 36 |
assert self.session_time is None or self.session_time > 0 |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
37 |
self.cleanup_session_time = vreg.config['cleanup-session-time'] or 43200 |
0 | 38 |
assert self.cleanup_session_time > 0 |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
39 |
self.cleanup_anon_session_time = vreg.config['cleanup-anonymous-session-time'] or 120 |
0 | 40 |
assert self.cleanup_anon_session_time > 0 |
41 |
if self.session_time: |
|
42 |
assert self.cleanup_session_time < self.session_time |
|
43 |
assert self.cleanup_anon_session_time < self.session_time |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
44 |
self.authmanager = vreg['components'].select('authmanager', vreg=vreg) |
1426 | 45 |
|
0 | 46 |
def clean_sessions(self): |
47 |
"""cleanup sessions which has not been unused since a given amount of |
|
48 |
time. Return the number of sessions which have been closed. |
|
49 |
""" |
|
50 |
self.debug('cleaning http sessions') |
|
51 |
closed, total = 0, 0 |
|
52 |
for session in self.current_sessions(): |
|
53 |
no_use_time = (time() - session.last_usage_time) |
|
54 |
total += 1 |
|
55 |
if session.anonymous_connection: |
|
56 |
if no_use_time >= self.cleanup_anon_session_time: |
|
57 |
self.close_session(session) |
|
58 |
closed += 1 |
|
59 |
elif no_use_time >= self.cleanup_session_time: |
|
60 |
self.close_session(session) |
|
61 |
closed += 1 |
|
62 |
return closed, total - closed |
|
1426 | 63 |
|
0 | 64 |
def has_expired(self, session): |
65 |
"""return True if the web session associated to the session is expired |
|
66 |
""" |
|
67 |
return not (self.session_time is None or |
|
68 |
time() < session.last_usage_time + self.session_time) |
|
1426 | 69 |
|
0 | 70 |
def current_sessions(self): |
71 |
"""return currently open sessions""" |
|
72 |
raise NotImplementedError() |
|
1426 | 73 |
|
0 | 74 |
def get_session(self, req, sessionid): |
75 |
"""return existing session for the given session identifier""" |
|
76 |
raise NotImplementedError() |
|
77 |
||
78 |
def open_session(self, req): |
|
79 |
"""open and return a new session for the given request |
|
1426 | 80 |
|
0 | 81 |
:raise ExplicitLogin: if authentication is required |
82 |
""" |
|
83 |
raise NotImplementedError() |
|
1426 | 84 |
|
0 | 85 |
def close_session(self, session): |
86 |
"""close session on logout or on invalid session detected (expired out, |
|
87 |
corrupted...) |
|
88 |
""" |
|
89 |
raise NotImplementedError() |
|
90 |
||
91 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
92 |
class AbstractAuthenticationManager(component.Component): |
0 | 93 |
"""authenticate user associated to a request and check session validity""" |
94 |
id = 'authmanager' |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
95 |
vreg = None # XXX necessary until property for deprecation warning is on appobject |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
96 |
|
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
97 |
def __init__(self, vreg): |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
98 |
self.vreg = vreg |
0 | 99 |
|
100 |
def authenticate(self, req): |
|
101 |
"""authenticate user and return corresponding user object |
|
1426 | 102 |
|
0 | 103 |
:raise ExplicitLogin: if authentication is required (no authentication |
104 |
info found or wrong user/password) |
|
105 |
""" |
|
106 |
raise NotImplementedError() |
|
107 |
||
1426 | 108 |
|
0 | 109 |
class CookieSessionHandler(object): |
110 |
"""a session handler using a cookie to store the session identifier |
|
111 |
||
112 |
:cvar SESSION_VAR: |
|
113 |
string giving the name of the variable used to store the session |
|
114 |
identifier |
|
115 |
""" |
|
116 |
SESSION_VAR = '__session' |
|
1426 | 117 |
|
0 | 118 |
def __init__(self, appli): |
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
119 |
self.vreg = appli.vreg |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
120 |
self.session_manager = self.vreg['components'].select('sessionmanager', |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
121 |
vreg=self.vreg) |
0 | 122 |
global SESSION_MANAGER |
123 |
SESSION_MANAGER = self.session_manager |
|
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
124 |
if not 'last_login_time' in self.vreg.schema: |
0 | 125 |
self._update_last_login_time = lambda x: None |
5000
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
126 |
if self.vreg.config.mode != 'test': |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
127 |
# don't try to reset session manager during test, this leads to |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
128 |
# weird failures when running multiple tests |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
129 |
CW_EVENT_MANAGER.bind('after-registry-reload', |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
130 |
self.reset_session_manager) |
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
131 |
|
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
132 |
def reset_session_manager(self): |
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
133 |
data = self.session_manager.dump_data() |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
134 |
self.session_manager = self.vreg['components'].select('sessionmanager', |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
135 |
vreg=self.vreg) |
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
136 |
self.session_manager.restore_data(data) |
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
137 |
global SESSION_MANAGER |
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
138 |
SESSION_MANAGER = self.session_manager |
0 | 139 |
|
140 |
def clean_sessions(self): |
|
141 |
"""cleanup sessions which has not been unused since a given amount of |
|
142 |
time |
|
143 |
""" |
|
144 |
self.session_manager.clean_sessions() |
|
1426 | 145 |
|
0 | 146 |
def set_session(self, req): |
147 |
"""associate a session to the request |
|
148 |
||
149 |
Session id is searched from : |
|
150 |
- # form variable |
|
151 |
- cookie |
|
152 |
||
153 |
if no session id is found, open a new session for the connected user |
|
154 |
or request authentification as needed |
|
155 |
||
1426 | 156 |
:raise Redirect: if authentication has occured and succeed |
0 | 157 |
""" |
158 |
assert req.cnx is None # at this point no cnx should be set on the request |
|
159 |
cookie = req.get_cookie() |
|
160 |
try: |
|
161 |
sessionid = str(cookie[self.SESSION_VAR].value) |
|
162 |
except KeyError: # no session cookie |
|
163 |
session = self.open_session(req) |
|
164 |
else: |
|
165 |
try: |
|
166 |
session = self.get_session(req, sessionid) |
|
167 |
except InvalidSession: |
|
168 |
try: |
|
169 |
session = self.open_session(req) |
|
170 |
except ExplicitLogin: |
|
171 |
req.remove_cookie(cookie, self.SESSION_VAR) |
|
172 |
raise |
|
173 |
# remember last usage time for web session tracking |
|
174 |
session.last_usage_time = time() |
|
175 |
||
176 |
def get_session(self, req, sessionid): |
|
177 |
return self.session_manager.get_session(req, sessionid) |
|
1426 | 178 |
|
0 | 179 |
def open_session(self, req): |
180 |
session = self.session_manager.open_session(req) |
|
181 |
cookie = req.get_cookie() |
|
182 |
cookie[self.SESSION_VAR] = session.sessionid |
|
183 |
req.set_cookie(cookie, self.SESSION_VAR, maxage=None) |
|
184 |
# remember last usage time for web session tracking |
|
185 |
session.last_usage_time = time() |
|
186 |
if not session.anonymous_connection: |
|
187 |
self._postlogin(req) |
|
188 |
return session |
|
189 |
||
190 |
def _update_last_login_time(self, req): |
|
191 |
try: |
|
192 |
req.execute('SET X last_login_time NOW WHERE X eid %(x)s', |
|
193 |
{'x' : req.user.eid}, 'x') |
|
194 |
req.cnx.commit() |
|
195 |
except (RepositoryError, Unauthorized): |
|
196 |
# ldap user are not writeable for instance |
|
197 |
req.cnx.rollback() |
|
198 |
except: |
|
199 |
req.cnx.rollback() |
|
200 |
raise |
|
1426 | 201 |
|
0 | 202 |
def _postlogin(self, req): |
203 |
"""postlogin: the user has been authenticated, redirect to the original |
|
204 |
page (index by default) with a welcome message |
|
205 |
""" |
|
206 |
# Update last connection date |
|
207 |
# XXX: this should be in a post login hook in the repository, but there |
|
208 |
# we can't differentiate actual login of automatic session |
|
209 |
# reopening. Is it actually a problem? |
|
210 |
self._update_last_login_time(req) |
|
211 |
args = req.form |
|
4639
82afdc7d8cd8
cleanup internal forms parameters in postlogin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
212 |
for forminternal_key in ('__form_id', '__domid', '__errorurl'): |
82afdc7d8cd8
cleanup internal forms parameters in postlogin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
213 |
args.pop(forminternal_key, None) |
0 | 214 |
args['__message'] = req._('welcome %s !') % req.user.login |
215 |
if 'vid' in req.form: |
|
216 |
args['vid'] = req.form['vid'] |
|
217 |
if 'rql' in req.form: |
|
218 |
args['rql'] = req.form['rql'] |
|
219 |
path = req.relative_path(False) |
|
220 |
if path == 'login': |
|
221 |
path = 'view' |
|
222 |
raise Redirect(req.build_url(path, **args)) |
|
1426 | 223 |
|
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4709
diff
changeset
|
224 |
def logout(self, req, goto_url): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
225 |
"""logout from the instance by cleaning the session and raising |
0 | 226 |
`AuthenticationError` |
227 |
""" |
|
228 |
self.session_manager.close_session(req.cnx) |
|
229 |
req.remove_cookie(req.get_cookie(), self.SESSION_VAR) |
|
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4709
diff
changeset
|
230 |
raise AuthenticationError(url=goto_url) |
0 | 231 |
|
232 |
||
233 |
class CubicWebPublisher(object): |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
234 |
"""the publisher is a singleton hold by the web frontend, and is responsible |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
235 |
to publish HTTP request. |
0 | 236 |
""" |
1426 | 237 |
|
0 | 238 |
def __init__(self, config, debug=None, |
239 |
session_handler_fact=CookieSessionHandler, |
|
240 |
vreg=None): |
|
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
241 |
self.info('starting web instance from %s', config.apphome) |
0 | 242 |
if vreg is None: |
2666
c6c832d32936
[webapp] missing renaming
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
243 |
vreg = cwvreg.CubicWebVRegistry(config, debug=debug) |
0 | 244 |
self.vreg = vreg |
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
245 |
# connect to the repository and get instance's schema |
0 | 246 |
self.repo = config.repository(vreg) |
247 |
if not vreg.initialized: |
|
248 |
self.config.init_cubes(self.repo.get_cubes()) |
|
249 |
vreg.init_properties(self.repo.properties()) |
|
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
250 |
vreg.set_schema(self.repo.get_schema()) |
0 | 251 |
# set the correct publish method |
252 |
if config['query-log-file']: |
|
253 |
from threading import Lock |
|
254 |
self._query_log = open(config['query-log-file'], 'a') |
|
255 |
self.publish = self.log_publish |
|
1426 | 256 |
self._logfile_lock = Lock() |
0 | 257 |
else: |
258 |
self._query_log = None |
|
259 |
self.publish = self.main_publish |
|
260 |
# instantiate session and url resolving helpers |
|
261 |
self.session_handler = session_handler_fact(self) |
|
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
262 |
self.set_urlresolver() |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2685
diff
changeset
|
263 |
CW_EVENT_MANAGER.bind('after-registry-reload', self.set_urlresolver) |
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
264 |
|
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
265 |
def set_urlresolver(self): |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
266 |
self.url_resolver = self.vreg['components'].select('urlpublisher', |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
267 |
vreg=self.vreg) |
1426 | 268 |
|
0 | 269 |
def connect(self, req): |
270 |
"""return a connection for a logged user object according to existing |
|
271 |
sessions (i.e. a new connection may be created or an already existing |
|
272 |
one may be reused |
|
273 |
""" |
|
274 |
self.session_handler.set_session(req) |
|
275 |
||
276 |
# publish methods ######################################################### |
|
1426 | 277 |
|
0 | 278 |
def log_publish(self, path, req): |
279 |
"""wrapper around _publish to log all queries executed for a given |
|
280 |
accessed path |
|
281 |
""" |
|
282 |
try: |
|
283 |
return self.main_publish(path, req) |
|
284 |
finally: |
|
285 |
cnx = req.cnx |
|
286 |
self._logfile_lock.acquire() |
|
287 |
try: |
|
288 |
try: |
|
289 |
result = ['\n'+'*'*80] |
|
290 |
result.append(req.url()) |
|
291 |
result += ['%s %s -- (%.3f sec, %.3f CPU sec)' % q for q in cnx.executed_queries] |
|
292 |
cnx.executed_queries = [] |
|
293 |
self._query_log.write('\n'.join(result).encode(req.encoding)) |
|
294 |
self._query_log.flush() |
|
295 |
except Exception: |
|
296 |
self.exception('error while logging queries') |
|
297 |
finally: |
|
298 |
self._logfile_lock.release() |
|
299 |
||
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2706
diff
changeset
|
300 |
@deprecated("[3.4] use vreg['controllers'].select(...)") |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
301 |
def select_controller(self, oid, req): |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
302 |
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:
2613
diff
changeset
|
303 |
return self.vreg['controllers'].select(oid, req=req, appli=self) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
304 |
except NoSelectableObject: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
305 |
raise Unauthorized(req._('not authorized')) |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
306 |
|
0 | 307 |
def main_publish(self, path, req): |
308 |
"""method called by the main publisher to process <path> |
|
1426 | 309 |
|
0 | 310 |
should return a string containing the resulting page or raise a |
311 |
`NotFound` exception |
|
312 |
||
313 |
:type path: str |
|
314 |
:param path: the path part of the url to publish |
|
1426 | 315 |
|
0 | 316 |
:type req: `web.Request` |
317 |
:param req: the request object |
|
318 |
||
319 |
:rtype: str |
|
320 |
:return: the result of the pusblished url |
|
321 |
""" |
|
322 |
path = path or 'view' |
|
323 |
# don't log form values they may contains sensitive information |
|
324 |
self.info('publish "%s" (form params: %s)', path, req.form.keys()) |
|
325 |
# remove user callbacks on a new request (except for json controllers |
|
326 |
# to avoid callbacks being unregistered before they could be called) |
|
327 |
tstart = clock() |
|
328 |
try: |
|
329 |
try: |
|
330 |
ctrlid, rset = self.url_resolver.process(req, path) |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
331 |
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:
2613
diff
changeset
|
332 |
controller = self.vreg['controllers'].select(ctrlid, req, |
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:
2613
diff
changeset
|
333 |
appli=self) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
334 |
except NoSelectableObject: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
335 |
raise Unauthorized(req._('not authorized')) |
581
09f87f2c535e
update_search_state in the publisher since it should be done whatever the controller
sylvain.thenault@logilab.fr
parents:
168
diff
changeset
|
336 |
req.update_search_state() |
0 | 337 |
result = controller.publish(rset=rset) |
338 |
if req.cnx is not None: |
|
339 |
# req.cnx is None if anonymous aren't allowed and we are |
|
340 |
# displaying the cookie authentication form |
|
341 |
req.cnx.commit() |
|
342 |
except (StatusResponse, DirectResponse): |
|
343 |
req.cnx.commit() |
|
344 |
raise |
|
345 |
except Redirect: |
|
346 |
# redirect is raised by edit controller when everything went fine, |
|
347 |
# so try to commit |
|
348 |
try: |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
349 |
txuuid = req.cnx.commit() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
350 |
if txuuid is not None: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
351 |
msg = u'<span class="undo">[<a href="%s">%s</a>]</span>' %( |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
352 |
req.build_url('undo', txuuid=txuuid), req._('undo')) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
353 |
req.append_to_redirect_message(msg) |
0 | 354 |
except ValidationError, ex: |
355 |
self.validation_error_handler(req, ex) |
|
356 |
except Unauthorized, ex: |
|
357 |
req.data['errmsg'] = req._('You\'re not authorized to access this page. ' |
|
358 |
'If you think you should, please contact the site administrator.') |
|
359 |
self.error_handler(req, ex, tb=False) |
|
360 |
except Exception, ex: |
|
361 |
self.error_handler(req, ex, tb=True) |
|
362 |
else: |
|
363 |
# delete validation errors which may have been previously set |
|
364 |
if '__errorurl' in req.form: |
|
365 |
req.del_session_data(req.form['__errorurl']) |
|
366 |
raise |
|
367 |
except (AuthenticationError, NotFound, RemoteCallFailed): |
|
368 |
raise |
|
369 |
except ValidationError, ex: |
|
370 |
self.validation_error_handler(req, ex) |
|
2272
f27a3a75be0d
no tb for RequestError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2263
diff
changeset
|
371 |
except (Unauthorized, BadRQLQuery, RequestError), ex: |
0 | 372 |
self.error_handler(req, ex, tb=False) |
373 |
except Exception, ex: |
|
374 |
self.error_handler(req, ex, tb=True) |
|
375 |
finally: |
|
376 |
if req.cnx is not None: |
|
377 |
try: |
|
378 |
req.cnx.rollback() |
|
379 |
except: |
|
380 |
pass # ignore rollback error at this point |
|
381 |
self.info('query %s executed in %s sec', req.relative_path(), clock() - tstart) |
|
382 |
return result |
|
383 |
||
384 |
def validation_error_handler(self, req, ex): |
|
385 |
ex.errors = dict((k, v) for k, v in ex.errors.items()) |
|
386 |
if '__errorurl' in req.form: |
|
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3408
diff
changeset
|
387 |
forminfo = {'error': ex, |
0 | 388 |
'values': req.form, |
389 |
'eidmap': req.data.get('eidmap', {}) |
|
390 |
} |
|
391 |
req.set_session_data(req.form['__errorurl'], forminfo) |
|
4679
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
392 |
# XXX form session key / __error_url should be differentiated: |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
393 |
# session key is 'url + #<form dom id', though we usually don't want |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
394 |
# the browser to move to the form since it hides the global |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
395 |
# messages. |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
396 |
raise Redirect(req.form['__errorurl'].rsplit('#', 1)[0]) |
0 | 397 |
self.error_handler(req, ex, tb=False) |
1426 | 398 |
|
0 | 399 |
def error_handler(self, req, ex, tb=False): |
400 |
excinfo = sys.exc_info() |
|
401 |
self.exception(repr(ex)) |
|
402 |
req.set_header('Cache-Control', 'no-cache') |
|
403 |
req.remove_header('Etag') |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4709
diff
changeset
|
404 |
req.reset_message() |
0 | 405 |
req.reset_headers() |
4709
6a71fc0b4274
[web] fix #724769: Use RemoteCallFailed in the publisher's error_handler
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4679
diff
changeset
|
406 |
if req.json_request: |
6a71fc0b4274
[web] fix #724769: Use RemoteCallFailed in the publisher's error_handler
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4679
diff
changeset
|
407 |
raise RemoteCallFailed(unicode(ex)) |
0 | 408 |
try: |
409 |
req.data['ex'] = ex |
|
410 |
if tb: |
|
411 |
req.data['excinfo'] = excinfo |
|
412 |
req.form['vid'] = 'error' |
|
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:
2613
diff
changeset
|
413 |
errview = self.vreg['views'].select('error', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
414 |
template = self.main_template_id(req) |
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:
2613
diff
changeset
|
415 |
content = self.vreg['views'].main_template(req, template, view=errview) |
0 | 416 |
except: |
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:
2613
diff
changeset
|
417 |
content = self.vreg['views'].main_template(req, 'error-template') |
0 | 418 |
raise StatusResponse(500, content) |
1426 | 419 |
|
0 | 420 |
def need_login_content(self, req): |
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:
2613
diff
changeset
|
421 |
return self.vreg['views'].main_template(req, 'login') |
1426 | 422 |
|
0 | 423 |
def loggedout_content(self, req): |
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:
2613
diff
changeset
|
424 |
return self.vreg['views'].main_template(req, 'loggedout') |
1426 | 425 |
|
0 | 426 |
def notfound_content(self, req): |
427 |
req.form['vid'] = '404' |
|
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:
2613
diff
changeset
|
428 |
view = self.vreg['views'].select('404', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
429 |
template = self.main_template_id(req) |
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:
2613
diff
changeset
|
430 |
return self.vreg['views'].main_template(req, template, view=view) |
0 | 431 |
|
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
432 |
def main_template_id(self, req): |
2263
1f59cd5b710f
accept a __template parameter that specifies a different (main) template
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1977
diff
changeset
|
433 |
template = req.form.get('__template', req.property_value('ui.main-template')) |
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:
2613
diff
changeset
|
434 |
if template not in self.vreg['views']: |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
435 |
template = 'main-template' |
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
436 |
return template |
1426 | 437 |
|
0 | 438 |
|
439 |
set_log_methods(CubicWebPublisher, LOGGER) |
|
440 |
set_log_methods(CookieSessionHandler, LOGGER) |