17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """CubicWeb web client application object""" |
18 """CubicWeb web client application object""" |
19 |
19 |
20 |
20 |
21 import contextlib |
21 import contextlib |
22 from functools import wraps |
|
23 import json |
22 import json |
24 import sys |
23 import sys |
25 from time import clock, time |
24 from time import clock, time |
26 from contextlib import contextmanager |
25 from contextlib import contextmanager |
27 from warnings import warn |
26 from warnings import warn |
28 |
27 |
29 from six import PY2, text_type, binary_type |
28 from six import text_type, binary_type |
30 from six.moves import http_client |
29 from six.moves import http_client |
31 |
30 |
32 from rql import BadRQLQuery |
31 from rql import BadRQLQuery |
33 |
32 |
34 from cubicweb import set_log_methods |
33 from cubicweb import set_log_methods |
38 from cubicweb.repoapi import anonymous_cnx |
37 from cubicweb.repoapi import anonymous_cnx |
39 from cubicweb.web import cors |
38 from cubicweb.web import cors |
40 from cubicweb.web import ( |
39 from cubicweb.web import ( |
41 LOGGER, StatusResponse, DirectResponse, Redirect, NotFound, LogOut, |
40 LOGGER, StatusResponse, DirectResponse, Redirect, NotFound, LogOut, |
42 RemoteCallFailed, InvalidSession, RequestError, PublishException) |
41 RemoteCallFailed, InvalidSession, RequestError, PublishException) |
43 from cubicweb.web.request import CubicWebRequestBase |
|
44 |
42 |
45 # make session manager available through a global variable so the debug view can |
43 # make session manager available through a global variable so the debug view can |
46 # print information about web session |
44 # print information about web session |
47 SESSION_MANAGER = None |
45 SESSION_MANAGER = None |
48 |
|
49 |
|
50 def _deprecated_path_arg(func): |
|
51 @wraps(func) |
|
52 def wrapper(self, req, *args, **kwargs): |
|
53 if args or 'path' in kwargs: |
|
54 func_name = func.func_name if PY2 else func.__name__ |
|
55 warn('[3.24] path argument got removed from "%s" parameters' % func_name, |
|
56 DeprecationWarning) |
|
57 path = args[0] if args else kwargs['path'] |
|
58 assert path == req.relative_path(False), \ |
|
59 'mismatching path, {0} vs {1}'.format(path, req.relative_path(False)) |
|
60 return func(self, req) |
|
61 return wrapper |
|
62 |
|
63 |
|
64 def _deprecated_req_path_swapped(func): |
|
65 @wraps(func) |
|
66 def wrapper(self, req, *args, **kwargs): |
|
67 if not isinstance(req, CubicWebRequestBase): |
|
68 warn('[3.15] Application entry point arguments are now (req, path) ' |
|
69 'not (path, req)', DeprecationWarning, 2) |
|
70 path = req |
|
71 req = args[0] if args else kwargs.pop('req') |
|
72 args = (path, ) + args[1:] |
|
73 return func(self, req, *args, **kwargs) |
|
74 return wrapper |
|
75 |
46 |
76 |
47 |
77 @contextmanager |
48 @contextmanager |
78 def anonymized_request(req): |
49 def anonymized_request(req): |
79 from cubicweb.web.views.authentication import Session |
50 from cubicweb.web.views.authentication import Session |
225 """ |
196 """ |
226 return self.session_handler.get_session(req) |
197 return self.session_handler.get_session(req) |
227 |
198 |
228 # publish methods ######################################################### |
199 # publish methods ######################################################### |
229 |
200 |
230 @_deprecated_path_arg |
|
231 def log_handle_request(self, req): |
201 def log_handle_request(self, req): |
232 """wrapper around _publish to log all queries executed for a given |
202 """wrapper around _publish to log all queries executed for a given |
233 accessed path |
203 accessed path |
234 """ |
204 """ |
235 def wrap_set_cnx(func): |
205 def wrap_set_cnx(func): |
271 self._query_log.write('\n'.join(result)) |
241 self._query_log.write('\n'.join(result)) |
272 self._query_log.flush() |
242 self._query_log.flush() |
273 except Exception: |
243 except Exception: |
274 self.exception('error while logging queries') |
244 self.exception('error while logging queries') |
275 |
245 |
276 @_deprecated_req_path_swapped |
|
277 @_deprecated_path_arg |
|
278 def main_handle_request(self, req): |
246 def main_handle_request(self, req): |
279 """Process an HTTP request `req` |
247 """Process an HTTP request `req` |
280 |
248 |
281 :type req: `web.Request` |
249 :type req: `web.Request` |
282 :param req: the request object |
250 :param req: the request object |
350 if not content: |
318 if not content: |
351 content = self.need_login_content(req) |
319 content = self.need_login_content(req) |
352 assert isinstance(content, binary_type) |
320 assert isinstance(content, binary_type) |
353 return content |
321 return content |
354 |
322 |
355 @_deprecated_path_arg |
|
356 def core_handle(self, req): |
323 def core_handle(self, req): |
357 """method called by the main publisher to process <req> relative path |
324 """method called by the main publisher to process <req> relative path |
358 |
325 |
359 should return a string containing the resulting page or raise a |
326 should return a string containing the resulting page or raise a |
360 `NotFound` exception |
327 `NotFound` exception |