author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 05 Aug 2009 09:10:32 +0200 | |
changeset 3385 | 68953ecddd2b |
parent 2685 | 0518ca8f63e3 |
child 2705 | 30bcdbd92820 |
permissions | -rw-r--r-- |
0 | 1 |
"""CubicWeb web client application object |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1426
diff
changeset
|
4 |
:copyright: 2001-2009 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""" |
32 |
id = 'sessionmanager' |
|
1426 | 33 |
|
0 | 34 |
def __init__(self): |
35 |
self.session_time = self.vreg.config['http-session-time'] or None |
|
36 |
assert self.session_time is None or self.session_time > 0 |
|
168
f6be0c92fc38
fix default values
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
37 |
self.cleanup_session_time = self.vreg.config['cleanup-session-time'] or 43200 |
0 | 38 |
assert self.cleanup_session_time > 0 |
168
f6be0c92fc38
fix default values
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
39 |
self.cleanup_anon_session_time = self.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 |
|
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
44 |
self.set_authmanager() |
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
45 |
CW_EVENT_MANAGER.bind('after-source-reload', self.set_authmanager) |
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
46 |
|
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
47 |
def set_authmanager(self): |
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
|
48 |
self.authmanager = self.vreg['components'].select('authmanager') |
1426 | 49 |
|
0 | 50 |
def clean_sessions(self): |
51 |
"""cleanup sessions which has not been unused since a given amount of |
|
52 |
time. Return the number of sessions which have been closed. |
|
53 |
""" |
|
54 |
self.debug('cleaning http sessions') |
|
55 |
closed, total = 0, 0 |
|
56 |
for session in self.current_sessions(): |
|
57 |
no_use_time = (time() - session.last_usage_time) |
|
58 |
total += 1 |
|
59 |
if session.anonymous_connection: |
|
60 |
if no_use_time >= self.cleanup_anon_session_time: |
|
61 |
self.close_session(session) |
|
62 |
closed += 1 |
|
63 |
elif no_use_time >= self.cleanup_session_time: |
|
64 |
self.close_session(session) |
|
65 |
closed += 1 |
|
66 |
return closed, total - closed |
|
1426 | 67 |
|
0 | 68 |
def has_expired(self, session): |
69 |
"""return True if the web session associated to the session is expired |
|
70 |
""" |
|
71 |
return not (self.session_time is None or |
|
72 |
time() < session.last_usage_time + self.session_time) |
|
1426 | 73 |
|
0 | 74 |
def current_sessions(self): |
75 |
"""return currently open sessions""" |
|
76 |
raise NotImplementedError() |
|
1426 | 77 |
|
0 | 78 |
def get_session(self, req, sessionid): |
79 |
"""return existing session for the given session identifier""" |
|
80 |
raise NotImplementedError() |
|
81 |
||
82 |
def open_session(self, req): |
|
83 |
"""open and return a new session for the given request |
|
1426 | 84 |
|
0 | 85 |
:raise ExplicitLogin: if authentication is required |
86 |
""" |
|
87 |
raise NotImplementedError() |
|
1426 | 88 |
|
0 | 89 |
def close_session(self, session): |
90 |
"""close session on logout or on invalid session detected (expired out, |
|
91 |
corrupted...) |
|
92 |
""" |
|
93 |
raise NotImplementedError() |
|
94 |
||
95 |
||
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
|
96 |
class AbstractAuthenticationManager(component.Component): |
0 | 97 |
"""authenticate user associated to a request and check session validity""" |
98 |
id = 'authmanager' |
|
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): |
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
|
119 |
self.session_manager = appli.vreg['components'].select('sessionmanager') |
0 | 120 |
global SESSION_MANAGER |
121 |
SESSION_MANAGER = self.session_manager |
|
122 |
if not 'last_login_time' in appli.vreg.schema: |
|
123 |
self._update_last_login_time = lambda x: None |
|
124 |
||
125 |
def clean_sessions(self): |
|
126 |
"""cleanup sessions which has not been unused since a given amount of |
|
127 |
time |
|
128 |
""" |
|
129 |
self.session_manager.clean_sessions() |
|
1426 | 130 |
|
0 | 131 |
def set_session(self, req): |
132 |
"""associate a session to the request |
|
133 |
||
134 |
Session id is searched from : |
|
135 |
- # form variable |
|
136 |
- cookie |
|
137 |
||
138 |
if no session id is found, open a new session for the connected user |
|
139 |
or request authentification as needed |
|
140 |
||
1426 | 141 |
:raise Redirect: if authentication has occured and succeed |
0 | 142 |
""" |
143 |
assert req.cnx is None # at this point no cnx should be set on the request |
|
144 |
cookie = req.get_cookie() |
|
145 |
try: |
|
146 |
sessionid = str(cookie[self.SESSION_VAR].value) |
|
147 |
except KeyError: # no session cookie |
|
148 |
session = self.open_session(req) |
|
149 |
else: |
|
150 |
try: |
|
151 |
session = self.get_session(req, sessionid) |
|
152 |
except InvalidSession: |
|
153 |
try: |
|
154 |
session = self.open_session(req) |
|
155 |
except ExplicitLogin: |
|
156 |
req.remove_cookie(cookie, self.SESSION_VAR) |
|
157 |
raise |
|
158 |
# remember last usage time for web session tracking |
|
159 |
session.last_usage_time = time() |
|
160 |
||
161 |
def get_session(self, req, sessionid): |
|
162 |
return self.session_manager.get_session(req, sessionid) |
|
1426 | 163 |
|
0 | 164 |
def open_session(self, req): |
165 |
session = self.session_manager.open_session(req) |
|
166 |
cookie = req.get_cookie() |
|
167 |
cookie[self.SESSION_VAR] = session.sessionid |
|
168 |
req.set_cookie(cookie, self.SESSION_VAR, maxage=None) |
|
169 |
# remember last usage time for web session tracking |
|
170 |
session.last_usage_time = time() |
|
171 |
if not session.anonymous_connection: |
|
172 |
self._postlogin(req) |
|
173 |
return session |
|
174 |
||
175 |
def _update_last_login_time(self, req): |
|
176 |
try: |
|
177 |
req.execute('SET X last_login_time NOW WHERE X eid %(x)s', |
|
178 |
{'x' : req.user.eid}, 'x') |
|
179 |
req.cnx.commit() |
|
180 |
except (RepositoryError, Unauthorized): |
|
181 |
# ldap user are not writeable for instance |
|
182 |
req.cnx.rollback() |
|
183 |
except: |
|
184 |
req.cnx.rollback() |
|
185 |
raise |
|
1426 | 186 |
|
0 | 187 |
def _postlogin(self, req): |
188 |
"""postlogin: the user has been authenticated, redirect to the original |
|
189 |
page (index by default) with a welcome message |
|
190 |
""" |
|
191 |
# Update last connection date |
|
192 |
# XXX: this should be in a post login hook in the repository, but there |
|
193 |
# we can't differentiate actual login of automatic session |
|
194 |
# reopening. Is it actually a problem? |
|
195 |
self._update_last_login_time(req) |
|
196 |
args = req.form |
|
197 |
args['__message'] = req._('welcome %s !') % req.user.login |
|
198 |
if 'vid' in req.form: |
|
199 |
args['vid'] = req.form['vid'] |
|
200 |
if 'rql' in req.form: |
|
201 |
args['rql'] = req.form['rql'] |
|
202 |
path = req.relative_path(False) |
|
203 |
if path == 'login': |
|
204 |
path = 'view' |
|
205 |
raise Redirect(req.build_url(path, **args)) |
|
1426 | 206 |
|
0 | 207 |
def logout(self, req): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
208 |
"""logout from the instance by cleaning the session and raising |
0 | 209 |
`AuthenticationError` |
210 |
""" |
|
211 |
self.session_manager.close_session(req.cnx) |
|
212 |
req.remove_cookie(req.get_cookie(), self.SESSION_VAR) |
|
213 |
raise AuthenticationError() |
|
214 |
||
215 |
||
216 |
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
|
217 |
"""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
|
218 |
to publish HTTP request. |
0 | 219 |
""" |
1426 | 220 |
|
0 | 221 |
def __init__(self, config, debug=None, |
222 |
session_handler_fact=CookieSessionHandler, |
|
223 |
vreg=None): |
|
224 |
super(CubicWebPublisher, self).__init__() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
225 |
# connect to the repository and get instance's schema |
0 | 226 |
if vreg is None: |
2666
c6c832d32936
[webapp] missing renaming
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
227 |
vreg = cwvreg.CubicWebVRegistry(config, debug=debug) |
0 | 228 |
self.vreg = vreg |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
229 |
self.info('starting web instance from %s', config.apphome) |
0 | 230 |
self.repo = config.repository(vreg) |
231 |
if not vreg.initialized: |
|
232 |
self.config.init_cubes(self.repo.get_cubes()) |
|
233 |
vreg.init_properties(self.repo.properties()) |
|
234 |
vreg.set_schema(self.repo.get_schema()) |
|
235 |
# set the correct publish method |
|
236 |
if config['query-log-file']: |
|
237 |
from threading import Lock |
|
238 |
self._query_log = open(config['query-log-file'], 'a') |
|
239 |
self.publish = self.log_publish |
|
1426 | 240 |
self._logfile_lock = Lock() |
0 | 241 |
else: |
242 |
self._query_log = None |
|
243 |
self.publish = self.main_publish |
|
244 |
# instantiate session and url resolving helpers |
|
245 |
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
|
246 |
self.set_urlresolver() |
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
247 |
CW_EVENT_MANAGER.bind('after-source-reload', self.set_urlresolver) |
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
248 |
|
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
249 |
def set_urlresolver(self): |
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
250 |
self.url_resolver = self.vreg['components'].select('urlpublisher') |
1426 | 251 |
|
0 | 252 |
def connect(self, req): |
253 |
"""return a connection for a logged user object according to existing |
|
254 |
sessions (i.e. a new connection may be created or an already existing |
|
255 |
one may be reused |
|
256 |
""" |
|
257 |
self.session_handler.set_session(req) |
|
258 |
||
259 |
# publish methods ######################################################### |
|
1426 | 260 |
|
0 | 261 |
def log_publish(self, path, req): |
262 |
"""wrapper around _publish to log all queries executed for a given |
|
263 |
accessed path |
|
264 |
""" |
|
265 |
try: |
|
266 |
return self.main_publish(path, req) |
|
267 |
finally: |
|
268 |
cnx = req.cnx |
|
269 |
self._logfile_lock.acquire() |
|
270 |
try: |
|
271 |
try: |
|
272 |
result = ['\n'+'*'*80] |
|
273 |
result.append(req.url()) |
|
274 |
result += ['%s %s -- (%.3f sec, %.3f CPU sec)' % q for q in cnx.executed_queries] |
|
275 |
cnx.executed_queries = [] |
|
276 |
self._query_log.write('\n'.join(result).encode(req.encoding)) |
|
277 |
self._query_log.flush() |
|
278 |
except Exception: |
|
279 |
self.exception('error while logging queries') |
|
280 |
finally: |
|
281 |
self._logfile_lock.release() |
|
282 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
283 |
@deprecated("use vreg.select('controllers', ...)") |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
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
|
288 |
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
|
289 |
|
0 | 290 |
def main_publish(self, path, req): |
291 |
"""method called by the main publisher to process <path> |
|
1426 | 292 |
|
0 | 293 |
should return a string containing the resulting page or raise a |
294 |
`NotFound` exception |
|
295 |
||
296 |
:type path: str |
|
297 |
:param path: the path part of the url to publish |
|
1426 | 298 |
|
0 | 299 |
:type req: `web.Request` |
300 |
:param req: the request object |
|
301 |
||
302 |
:rtype: str |
|
303 |
:return: the result of the pusblished url |
|
304 |
""" |
|
305 |
path = path or 'view' |
|
306 |
# don't log form values they may contains sensitive information |
|
307 |
self.info('publish "%s" (form params: %s)', path, req.form.keys()) |
|
308 |
# remove user callbacks on a new request (except for json controllers |
|
309 |
# to avoid callbacks being unregistered before they could be called) |
|
310 |
tstart = clock() |
|
311 |
try: |
|
312 |
try: |
|
313 |
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
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
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
|
318 |
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
|
319 |
req.update_search_state() |
0 | 320 |
result = controller.publish(rset=rset) |
321 |
if req.cnx is not None: |
|
322 |
# req.cnx is None if anonymous aren't allowed and we are |
|
323 |
# displaying the cookie authentication form |
|
324 |
req.cnx.commit() |
|
325 |
except (StatusResponse, DirectResponse): |
|
326 |
req.cnx.commit() |
|
327 |
raise |
|
328 |
except Redirect: |
|
329 |
# redirect is raised by edit controller when everything went fine, |
|
330 |
# so try to commit |
|
331 |
try: |
|
332 |
req.cnx.commit() |
|
333 |
except ValidationError, ex: |
|
334 |
self.validation_error_handler(req, ex) |
|
335 |
except Unauthorized, ex: |
|
336 |
req.data['errmsg'] = req._('You\'re not authorized to access this page. ' |
|
337 |
'If you think you should, please contact the site administrator.') |
|
338 |
self.error_handler(req, ex, tb=False) |
|
339 |
except Exception, ex: |
|
340 |
self.error_handler(req, ex, tb=True) |
|
341 |
else: |
|
342 |
# delete validation errors which may have been previously set |
|
343 |
if '__errorurl' in req.form: |
|
344 |
req.del_session_data(req.form['__errorurl']) |
|
345 |
raise |
|
346 |
except (AuthenticationError, NotFound, RemoteCallFailed): |
|
347 |
raise |
|
348 |
except ValidationError, ex: |
|
349 |
self.validation_error_handler(req, ex) |
|
2272
f27a3a75be0d
no tb for RequestError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2263
diff
changeset
|
350 |
except (Unauthorized, BadRQLQuery, RequestError), ex: |
0 | 351 |
self.error_handler(req, ex, tb=False) |
352 |
except Exception, ex: |
|
353 |
self.error_handler(req, ex, tb=True) |
|
354 |
finally: |
|
355 |
if req.cnx is not None: |
|
356 |
try: |
|
357 |
req.cnx.rollback() |
|
358 |
except: |
|
359 |
pass # ignore rollback error at this point |
|
360 |
self.info('query %s executed in %s sec', req.relative_path(), clock() - tstart) |
|
361 |
return result |
|
362 |
||
363 |
def validation_error_handler(self, req, ex): |
|
364 |
ex.errors = dict((k, v) for k, v in ex.errors.items()) |
|
365 |
if '__errorurl' in req.form: |
|
366 |
forminfo = {'errors': ex, |
|
367 |
'values': req.form, |
|
368 |
'eidmap': req.data.get('eidmap', {}) |
|
369 |
} |
|
370 |
req.set_session_data(req.form['__errorurl'], forminfo) |
|
371 |
raise Redirect(req.form['__errorurl']) |
|
372 |
self.error_handler(req, ex, tb=False) |
|
1426 | 373 |
|
0 | 374 |
def error_handler(self, req, ex, tb=False): |
375 |
excinfo = sys.exc_info() |
|
376 |
self.exception(repr(ex)) |
|
377 |
req.set_header('Cache-Control', 'no-cache') |
|
378 |
req.remove_header('Etag') |
|
379 |
req.message = None |
|
380 |
req.reset_headers() |
|
381 |
try: |
|
382 |
req.data['ex'] = ex |
|
383 |
if tb: |
|
384 |
req.data['excinfo'] = excinfo |
|
385 |
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
|
386 |
errview = self.vreg['views'].select('error', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
387 |
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
|
388 |
content = self.vreg['views'].main_template(req, template, view=errview) |
0 | 389 |
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
|
390 |
content = self.vreg['views'].main_template(req, 'error-template') |
0 | 391 |
raise StatusResponse(500, content) |
1426 | 392 |
|
0 | 393 |
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
|
394 |
return self.vreg['views'].main_template(req, 'login') |
1426 | 395 |
|
0 | 396 |
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
|
397 |
return self.vreg['views'].main_template(req, 'loggedout') |
1426 | 398 |
|
0 | 399 |
def notfound_content(self, req): |
400 |
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
|
401 |
view = self.vreg['views'].select('404', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
402 |
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
|
403 |
return self.vreg['views'].main_template(req, template, view=view) |
0 | 404 |
|
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
405 |
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
|
406 |
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
|
407 |
if template not in self.vreg['views']: |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
408 |
template = 'main-template' |
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
409 |
return template |
1426 | 410 |
|
0 | 411 |
|
412 |
set_log_methods(CubicWebPublisher, LOGGER) |
|
413 |
set_log_methods(CookieSessionHandler, LOGGER) |