##// END OF EJS Templates
Backport PR #6117: Remove / from route of TreeRedirectHandler....
MinRK -
Show More
@@ -1,103 +1,103 b''
1 """Tornado handlers for the tree view.
1 """Tornado handlers for the tree view.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2011 The IPython Development Team
9 # Copyright (C) 2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 import os
18 import os
19
19
20 from tornado import web
20 from tornado import web
21 from ..base.handlers import IPythonHandler, notebook_path_regex, path_regex
21 from ..base.handlers import IPythonHandler, notebook_path_regex, path_regex
22 from ..utils import url_path_join, path2url, url2path, url_escape, is_hidden
22 from ..utils import url_path_join, path2url, url2path, url_escape, is_hidden
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Handlers
25 # Handlers
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28
28
29 class TreeHandler(IPythonHandler):
29 class TreeHandler(IPythonHandler):
30 """Render the tree view, listing notebooks, clusters, etc."""
30 """Render the tree view, listing notebooks, clusters, etc."""
31
31
32 def generate_breadcrumbs(self, path):
32 def generate_breadcrumbs(self, path):
33 breadcrumbs = [(url_escape(url_path_join(self.base_url, 'tree')), '')]
33 breadcrumbs = [(url_escape(url_path_join(self.base_url, 'tree')), '')]
34 comps = path.split('/')
34 comps = path.split('/')
35 ncomps = len(comps)
35 ncomps = len(comps)
36 for i in range(ncomps):
36 for i in range(ncomps):
37 if comps[i]:
37 if comps[i]:
38 link = url_escape(url_path_join(self.base_url, 'tree', *comps[0:i+1]))
38 link = url_escape(url_path_join(self.base_url, 'tree', *comps[0:i+1]))
39 breadcrumbs.append((link, comps[i]))
39 breadcrumbs.append((link, comps[i]))
40 return breadcrumbs
40 return breadcrumbs
41
41
42 def generate_page_title(self, path):
42 def generate_page_title(self, path):
43 comps = path.split('/')
43 comps = path.split('/')
44 if len(comps) > 3:
44 if len(comps) > 3:
45 for i in range(len(comps)-2):
45 for i in range(len(comps)-2):
46 comps.pop(0)
46 comps.pop(0)
47 page_title = url_path_join(*comps)
47 page_title = url_path_join(*comps)
48 if page_title:
48 if page_title:
49 return page_title+'/'
49 return page_title+'/'
50 else:
50 else:
51 return 'Home'
51 return 'Home'
52
52
53 @web.authenticated
53 @web.authenticated
54 def get(self, path='', name=None):
54 def get(self, path='', name=None):
55 path = path.strip('/')
55 path = path.strip('/')
56 nbm = self.notebook_manager
56 nbm = self.notebook_manager
57 if name is not None:
57 if name is not None:
58 # is a notebook, redirect to notebook handler
58 # is a notebook, redirect to notebook handler
59 url = url_escape(url_path_join(
59 url = url_escape(url_path_join(
60 self.base_url, 'notebooks', path, name
60 self.base_url, 'notebooks', path, name
61 ))
61 ))
62 self.log.debug("Redirecting %s to %s", self.request.path, url)
62 self.log.debug("Redirecting %s to %s", self.request.path, url)
63 self.redirect(url)
63 self.redirect(url)
64 else:
64 else:
65 if not nbm.path_exists(path=path):
65 if not nbm.path_exists(path=path):
66 # Directory is hidden or does not exist.
66 # Directory is hidden or does not exist.
67 raise web.HTTPError(404)
67 raise web.HTTPError(404)
68 elif nbm.is_hidden(path):
68 elif nbm.is_hidden(path):
69 self.log.info("Refusing to serve hidden directory, via 404 Error")
69 self.log.info("Refusing to serve hidden directory, via 404 Error")
70 raise web.HTTPError(404)
70 raise web.HTTPError(404)
71 breadcrumbs = self.generate_breadcrumbs(path)
71 breadcrumbs = self.generate_breadcrumbs(path)
72 page_title = self.generate_page_title(path)
72 page_title = self.generate_page_title(path)
73 self.write(self.render_template('tree.html',
73 self.write(self.render_template('tree.html',
74 project=self.project_dir,
74 project=self.project_dir,
75 page_title=page_title,
75 page_title=page_title,
76 notebook_path=path,
76 notebook_path=path,
77 breadcrumbs=breadcrumbs
77 breadcrumbs=breadcrumbs
78 ))
78 ))
79
79
80
80
81 class TreeRedirectHandler(IPythonHandler):
81 class TreeRedirectHandler(IPythonHandler):
82 """Redirect a request to the corresponding tree URL"""
82 """Redirect a request to the corresponding tree URL"""
83
83
84 @web.authenticated
84 @web.authenticated
85 def get(self, path=''):
85 def get(self, path=''):
86 url = url_escape(url_path_join(
86 url = url_escape(url_path_join(
87 self.base_url, 'tree', path.strip('/')
87 self.base_url, 'tree', path.strip('/')
88 ))
88 ))
89 self.log.debug("Redirecting %s to %s", self.request.path, url)
89 self.log.debug("Redirecting %s to %s", self.request.path, url)
90 self.redirect(url)
90 self.redirect(url)
91
91
92
92
93 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
94 # URL to handler mappings
94 # URL to handler mappings
95 #-----------------------------------------------------------------------------
95 #-----------------------------------------------------------------------------
96
96
97
97
98 default_handlers = [
98 default_handlers = [
99 (r"/tree%s" % notebook_path_regex, TreeHandler),
99 (r"/tree%s" % notebook_path_regex, TreeHandler),
100 (r"/tree%s" % path_regex, TreeHandler),
100 (r"/tree%s" % path_regex, TreeHandler),
101 (r"/tree", TreeHandler),
101 (r"/tree", TreeHandler),
102 (r"/", TreeRedirectHandler),
102 (r"", TreeRedirectHandler),
103 ]
103 ]
General Comments 0
You need to be logged in to leave comments. Login now