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