Show More
@@ -27,7 +27,7 b' except ImportError:' | |||||
27 | from IPython.config import Application |
|
27 | from IPython.config import Application | |
28 | from IPython.utils.path import filefind |
|
28 | from IPython.utils.path import filefind | |
29 | from IPython.utils.py3compat import string_types |
|
29 | from IPython.utils.py3compat import string_types | |
30 | from IPython.html.utils import is_hidden |
|
30 | from IPython.html.utils import is_hidden, url_path_join, url_escape | |
31 |
|
31 | |||
32 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
33 | # Top-level handlers |
|
33 | # Top-level handlers | |
@@ -409,6 +409,37 b' class TrailingSlashHandler(web.RequestHandler):' | |||||
409 | def get(self): |
|
409 | def get(self): | |
410 | self.redirect(self.request.uri.rstrip('/')) |
|
410 | self.redirect(self.request.uri.rstrip('/')) | |
411 |
|
411 | |||
|
412 | ||||
|
413 | class FilesRedirectHandler(IPythonHandler): | |||
|
414 | """Handler for redirecting relative URLs to the /files/ handler""" | |||
|
415 | def get(self, path=''): | |||
|
416 | cm = self.contents_manager | |||
|
417 | if cm.path_exists(path): | |||
|
418 | # it's a *directory*, redirect to /tree | |||
|
419 | url = url_path_join(self.base_url, 'tree', path) | |||
|
420 | else: | |||
|
421 | orig_path = path | |||
|
422 | # otherwise, redirect to /files | |||
|
423 | parts = path.split('/') | |||
|
424 | path = '/'.join(parts[:-1]) | |||
|
425 | name = parts[-1] | |||
|
426 | ||||
|
427 | if not cm.file_exists(name=name, path=path) and 'files' in parts: | |||
|
428 | # redirect without files/ iff it would 404 | |||
|
429 | # this preserves pre-2.0-style 'files/' links | |||
|
430 | self.log.warn("Deprecated files/ URL: %s", orig_path) | |||
|
431 | parts.remove('files') | |||
|
432 | path = '/'.join(parts[:-1]) | |||
|
433 | ||||
|
434 | if not cm.file_exists(name=name, path=path): | |||
|
435 | raise web.HTTPError(404) | |||
|
436 | ||||
|
437 | url = url_path_join(self.base_url, 'files', path, name) | |||
|
438 | url = url_escape(url) | |||
|
439 | self.log.debug("Redirecting %s to %s", self.request.path, url) | |||
|
440 | self.redirect(url) | |||
|
441 | ||||
|
442 | ||||
412 | #----------------------------------------------------------------------------- |
|
443 | #----------------------------------------------------------------------------- | |
413 | # URL pattern fragments for re-use |
|
444 | # URL pattern fragments for re-use | |
414 | #----------------------------------------------------------------------------- |
|
445 | #----------------------------------------------------------------------------- |
@@ -9,7 +9,10 b' import zipfile' | |||||
9 |
|
9 | |||
10 | from tornado import web |
|
10 | from tornado import web | |
11 |
|
11 | |||
12 |
from ..base.handlers import |
|
12 | from ..base.handlers import ( | |
|
13 | IPythonHandler, FilesRedirectHandler, | |||
|
14 | notebook_path_regex, path_regex, | |||
|
15 | ) | |||
13 | from IPython.nbformat.current import to_notebook_json |
|
16 | from IPython.nbformat.current import to_notebook_json | |
14 |
|
17 | |||
15 | from IPython.utils.py3compat import cast_bytes |
|
18 | from IPython.utils.py3compat import cast_bytes | |
@@ -128,6 +131,7 b' class NbconvertPostHandler(IPythonHandler):' | |||||
128 |
|
131 | |||
129 | self.finish(output) |
|
132 | self.finish(output) | |
130 |
|
133 | |||
|
134 | ||||
131 | #----------------------------------------------------------------------------- |
|
135 | #----------------------------------------------------------------------------- | |
132 | # URL to handler mappings |
|
136 | # URL to handler mappings | |
133 | #----------------------------------------------------------------------------- |
|
137 | #----------------------------------------------------------------------------- | |
@@ -139,4 +143,5 b' default_handlers = [' | |||||
139 | (r"/nbconvert/%s%s" % (_format_regex, notebook_path_regex), |
|
143 | (r"/nbconvert/%s%s" % (_format_regex, notebook_path_regex), | |
140 | NbconvertFileHandler), |
|
144 | NbconvertFileHandler), | |
141 | (r"/nbconvert/%s" % _format_regex, NbconvertPostHandler), |
|
145 | (r"/nbconvert/%s" % _format_regex, NbconvertPostHandler), | |
|
146 | (r"/nbconvert/html%s" % path_regex, FilesRedirectHandler), | |||
142 | ] |
|
147 | ] |
@@ -1,31 +1,17 b'' | |||||
1 | """Tornado handlers for the live notebook view. |
|
1 | """Tornado handlers for the live notebook view.""" | |
2 |
|
2 | |||
3 | Authors: |
|
3 | # Copyright (c) IPython Development Team. | |
4 |
|
4 | # Distributed under the terms of the Modified BSD License. | ||
5 | * Brian Granger |
|
|||
6 | """ |
|
|||
7 |
|
||||
8 | #----------------------------------------------------------------------------- |
|
|||
9 | # Copyright (C) 2011 The IPython Development Team |
|
|||
10 | # |
|
|||
11 | # Distributed under the terms of the BSD License. The full license is in |
|
|||
12 | # the file COPYING, distributed as part of this software. |
|
|||
13 | #----------------------------------------------------------------------------- |
|
|||
14 |
|
||||
15 | #----------------------------------------------------------------------------- |
|
|||
16 | # Imports |
|
|||
17 | #----------------------------------------------------------------------------- |
|
|||
18 |
|
5 | |||
19 | import os |
|
6 | import os | |
20 | from tornado import web |
|
7 | from tornado import web | |
21 | HTTPError = web.HTTPError |
|
8 | HTTPError = web.HTTPError | |
22 |
|
9 | |||
23 | from ..base.handlers import IPythonHandler, notebook_path_regex, path_regex |
|
10 | from ..base.handlers import ( | |
24 | from ..utils import url_path_join, url_escape |
|
11 | IPythonHandler, FilesRedirectHandler, | |
25 |
|
12 | notebook_path_regex, path_regex, | ||
26 | #----------------------------------------------------------------------------- |
|
13 | ) | |
27 | # Handlers |
|
14 | from ..utils import url_escape | |
28 | #----------------------------------------------------------------------------- |
|
|||
29 |
|
15 | |||
30 |
|
16 | |||
31 | class NotebookHandler(IPythonHandler): |
|
17 | class NotebookHandler(IPythonHandler): | |
@@ -53,33 +39,6 b' class NotebookHandler(IPythonHandler):' | |||||
53 | ) |
|
39 | ) | |
54 | ) |
|
40 | ) | |
55 |
|
41 | |||
56 | class NotebookRedirectHandler(IPythonHandler): |
|
|||
57 | def get(self, path=''): |
|
|||
58 | cm = self.contents_manager |
|
|||
59 | if cm.path_exists(path): |
|
|||
60 | # it's a *directory*, redirect to /tree |
|
|||
61 | url = url_path_join(self.base_url, 'tree', path) |
|
|||
62 | else: |
|
|||
63 | orig_path = path |
|
|||
64 | # otherwise, redirect to /files |
|
|||
65 | parts = path.split('/') |
|
|||
66 | path = '/'.join(parts[:-1]) |
|
|||
67 | name = parts[-1] |
|
|||
68 |
|
||||
69 | if not cm.file_exists(name=name, path=path) and 'files' in parts: |
|
|||
70 | # redirect without files/ iff it would 404 |
|
|||
71 | # this preserves pre-2.0-style 'files/' links |
|
|||
72 | self.log.warn("Deprecated files/ URL: %s", orig_path) |
|
|||
73 | parts.remove('files') |
|
|||
74 | path = '/'.join(parts[:-1]) |
|
|||
75 |
|
||||
76 | if not cm.file_exists(name=name, path=path): |
|
|||
77 | raise web.HTTPError(404) |
|
|||
78 |
|
||||
79 | url = url_path_join(self.base_url, 'files', path, name) |
|
|||
80 | url = url_escape(url) |
|
|||
81 | self.log.debug("Redirecting %s to %s", self.request.path, url) |
|
|||
82 | self.redirect(url) |
|
|||
83 |
|
42 | |||
84 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
85 | # URL to handler mappings |
|
44 | # URL to handler mappings | |
@@ -88,6 +47,6 b' class NotebookRedirectHandler(IPythonHandler):' | |||||
88 |
|
47 | |||
89 | default_handlers = [ |
|
48 | default_handlers = [ | |
90 | (r"/notebooks%s" % notebook_path_regex, NotebookHandler), |
|
49 | (r"/notebooks%s" % notebook_path_regex, NotebookHandler), | |
91 |
(r"/notebooks%s" % path_regex, |
|
50 | (r"/notebooks%s" % path_regex, FilesRedirectHandler), | |
92 | ] |
|
51 | ] | |
93 |
|
52 |
General Comments 0
You need to be logged in to leave comments.
Login now