handlers.py
84 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
MinRK
|
r17525 | """Tornado handlers for the tree view.""" | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r17525 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r10641 | |||
MinRK
|
r11644 | from tornado import web | ||
Thomas Kluyver
|
r13916 | from ..base.handlers import IPythonHandler, notebook_path_regex, path_regex | ||
Spencer Nelson
|
r16525 | from ..utils import url_path_join, url_escape | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r13055 | class TreeHandler(IPythonHandler): | ||
"""Render the tree view, listing notebooks, clusters, etc.""" | ||||
Brian E. Granger
|
r10641 | |||
Brian E. Granger
|
r15073 | def generate_breadcrumbs(self, path): | ||
MinRK
|
r15238 | breadcrumbs = [(url_escape(url_path_join(self.base_url, 'tree')), '')] | ||
Brian E. Granger
|
r15073 | comps = path.split('/') | ||
ncomps = len(comps) | ||||
for i in range(ncomps): | ||||
if comps[i]: | ||||
MinRK
|
r15238 | link = url_escape(url_path_join(self.base_url, 'tree', *comps[0:i+1])) | ||
Brian E. Granger
|
r15073 | breadcrumbs.append((link, comps[i])) | ||
return breadcrumbs | ||||
Brian E. Granger
|
r15085 | def generate_page_title(self, path): | ||
comps = path.split('/') | ||||
if len(comps) > 3: | ||||
for i in range(len(comps)-2): | ||||
comps.pop(0) | ||||
Spencer Nelson
|
r16524 | page_title = url_path_join(*comps) | ||
Brian E. Granger
|
r15085 | if page_title: | ||
Brian E. Granger
|
r15089 | return page_title+'/' | ||
Brian E. Granger
|
r15085 | else: | ||
Brian E. Granger
|
r15089 | return 'Home' | ||
Brian E. Granger
|
r15085 | |||
MinRK
|
r11644 | @web.authenticated | ||
MinRK
|
r13067 | def get(self, path='', name=None): | ||
MinRK
|
r13117 | path = path.strip('/') | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
Zachary Sailer
|
r13033 | if name is not None: | ||
MinRK
|
r13055 | # is a notebook, redirect to notebook handler | ||
MinRK
|
r13135 | url = url_escape(url_path_join( | ||
MinRK
|
r15238 | self.base_url, 'notebooks', path, name | ||
MinRK
|
r13135 | )) | ||
self.log.debug("Redirecting %s to %s", self.request.path, url) | ||||
MinRK
|
r13055 | self.redirect(url) | ||
Zachary Sailer
|
r12990 | else: | ||
MinRK
|
r17524 | if not cm.path_exists(path=path): | ||
Brian E. Granger
|
r15097 | # Directory is hidden or does not exist. | ||
MinRK
|
r13055 | raise web.HTTPError(404) | ||
MinRK
|
r17524 | elif cm.is_hidden(path): | ||
Paul Ivanov
|
r15630 | self.log.info("Refusing to serve hidden directory, via 404 Error") | ||
raise web.HTTPError(404) | ||||
Brian E. Granger
|
r15073 | breadcrumbs = self.generate_breadcrumbs(path) | ||
Brian E. Granger
|
r15085 | page_title = self.generate_page_title(path) | ||
Zachary Sailer
|
r12990 | self.write(self.render_template('tree.html', | ||
Brian E. Granger
|
r15085 | page_title=page_title, | ||
Zachary Sailer
|
r12990 | notebook_path=path, | ||
Brian E. Granger
|
r15073 | breadcrumbs=breadcrumbs | ||
MinRK
|
r13055 | )) | ||
Zachary Sailer
|
r12990 | |||
class TreeRedirectHandler(IPythonHandler): | ||||
MinRK
|
r13055 | """Redirect a request to the corresponding tree URL""" | ||
Zachary Sailer
|
r12992 | |||
Zachary Sailer
|
r13014 | @web.authenticated | ||
MinRK
|
r13067 | def get(self, path=''): | ||
MinRK
|
r13135 | url = url_escape(url_path_join( | ||
MinRK
|
r15238 | self.base_url, 'tree', path.strip('/') | ||
MinRK
|
r13135 | )) | ||
self.log.debug("Redirecting %s to %s", self.request.path, url) | ||||
Zachary Sailer
|
r12990 | self.redirect(url) | ||
Zachary Sailer
|
r13003 | |||
Brian E. Granger
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
Zachary Sailer
|
r12990 | default_handlers = [ | ||
Thomas Kluyver
|
r13916 | (r"/tree%s" % notebook_path_regex, TreeHandler), | ||
(r"/tree%s" % path_regex, TreeHandler), | ||||
MinRK
|
r13055 | (r"/tree", TreeHandler), | ||
MinRK
|
r17675 | (r"/?", TreeRedirectHandler), | ||
Zachary Sailer
|
r12990 | ] | ||