2014-06-03 04:47:11 +08:00
|
|
|
"""Tornado handlers for the tree view."""
|
2013-05-03 02:15:32 +08:00
|
|
|
|
2014-06-03 04:47:11 +08:00
|
|
|
# Copyright (c) IPython Development Team.
|
|
|
|
# Distributed under the terms of the Modified BSD License.
|
2013-05-03 02:15:32 +08:00
|
|
|
|
2013-07-24 04:03:03 +08:00
|
|
|
from tornado import web
|
2014-10-23 04:13:36 +08:00
|
|
|
from ..base.handlers import IPythonHandler, path_regex
|
2014-05-02 06:42:46 +08:00
|
|
|
from ..utils import url_path_join, url_escape
|
2013-05-03 02:15:32 +08:00
|
|
|
|
|
|
|
|
2013-08-25 18:10:19 +08:00
|
|
|
class TreeHandler(IPythonHandler):
|
|
|
|
"""Render the tree view, listing notebooks, clusters, etc."""
|
2013-05-03 02:15:32 +08:00
|
|
|
|
2014-02-03 11:41:53 +08:00
|
|
|
def generate_breadcrumbs(self, path):
|
2014-02-06 08:16:32 +08:00
|
|
|
breadcrumbs = [(url_escape(url_path_join(self.base_url, 'tree')), '')]
|
2014-02-03 11:41:53 +08:00
|
|
|
comps = path.split('/')
|
|
|
|
ncomps = len(comps)
|
|
|
|
for i in range(ncomps):
|
|
|
|
if comps[i]:
|
2014-02-06 08:16:32 +08:00
|
|
|
link = url_escape(url_path_join(self.base_url, 'tree', *comps[0:i+1]))
|
2014-02-03 11:41:53 +08:00
|
|
|
breadcrumbs.append((link, comps[i]))
|
|
|
|
return breadcrumbs
|
|
|
|
|
2014-02-05 08:29:55 +08:00
|
|
|
def generate_page_title(self, path):
|
|
|
|
comps = path.split('/')
|
|
|
|
if len(comps) > 3:
|
|
|
|
for i in range(len(comps)-2):
|
|
|
|
comps.pop(0)
|
2014-05-02 06:12:32 +08:00
|
|
|
page_title = url_path_join(*comps)
|
2014-02-05 08:29:55 +08:00
|
|
|
if page_title:
|
2014-02-06 01:58:52 +08:00
|
|
|
return page_title+'/'
|
2014-02-05 08:29:55 +08:00
|
|
|
else:
|
2014-02-06 01:58:52 +08:00
|
|
|
return 'Home'
|
2014-02-05 08:29:55 +08:00
|
|
|
|
2013-07-24 04:03:03 +08:00
|
|
|
@web.authenticated
|
2014-10-23 04:13:36 +08:00
|
|
|
def get(self, path=''):
|
2013-10-11 15:22:27 +08:00
|
|
|
path = path.strip('/')
|
2014-05-31 04:01:21 +08:00
|
|
|
cm = self.contents_manager
|
2014-10-23 04:13:36 +08:00
|
|
|
if cm.file_exists(path):
|
2014-11-11 05:46:51 +08:00
|
|
|
# it's not a directory, we have redirecting to do
|
|
|
|
model = cm.get(path, content=False)
|
|
|
|
# redirect to /api/notebooks if it's a notebook, otherwise /api/files
|
|
|
|
service = 'notebooks' if model['type'] == 'notebook' else 'files'
|
2013-10-19 06:12:23 +08:00
|
|
|
url = url_escape(url_path_join(
|
2014-11-11 05:46:51 +08:00
|
|
|
self.base_url, service, path,
|
2013-10-19 06:12:23 +08:00
|
|
|
))
|
|
|
|
self.log.debug("Redirecting %s to %s", self.request.path, url)
|
2013-08-25 18:10:19 +08:00
|
|
|
self.redirect(url)
|
2013-07-04 01:47:39 +08:00
|
|
|
else:
|
2014-10-23 04:13:36 +08:00
|
|
|
if not cm.dir_exists(path=path):
|
2014-02-06 05:09:55 +08:00
|
|
|
# Directory is hidden or does not exist.
|
2013-08-25 18:10:19 +08:00
|
|
|
raise web.HTTPError(404)
|
2014-05-31 04:01:21 +08:00
|
|
|
elif cm.is_hidden(path):
|
2014-03-04 08:02:07 +08:00
|
|
|
self.log.info("Refusing to serve hidden directory, via 404 Error")
|
|
|
|
raise web.HTTPError(404)
|
2014-02-03 11:41:53 +08:00
|
|
|
breadcrumbs = self.generate_breadcrumbs(path)
|
2014-02-05 08:29:55 +08:00
|
|
|
page_title = self.generate_page_title(path)
|
2013-07-04 01:47:39 +08:00
|
|
|
self.write(self.render_template('tree.html',
|
2014-02-05 08:29:55 +08:00
|
|
|
page_title=page_title,
|
2013-07-04 01:47:39 +08:00
|
|
|
notebook_path=path,
|
2014-11-01 06:56:53 +08:00
|
|
|
breadcrumbs=breadcrumbs,
|
|
|
|
terminals_available=self.settings['terminals_available'],
|
2013-08-25 18:10:19 +08:00
|
|
|
))
|
2013-07-04 01:47:39 +08:00
|
|
|
|
|
|
|
|
2013-05-16 01:55:43 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# URL to handler mappings
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2013-07-04 01:47:39 +08:00
|
|
|
default_handlers = [
|
2013-12-20 05:01:00 +08:00
|
|
|
(r"/tree%s" % path_regex, TreeHandler),
|
2013-08-25 18:10:19 +08:00
|
|
|
(r"/tree", TreeHandler),
|
2013-07-04 01:47:39 +08:00
|
|
|
]
|