handlers.py
103 lines
| 3.5 KiB
| text/x-python
|
PythonLexer
|
r10642 | """Tornado handlers for the tree view. | ||
|
r10641 | |||
Authors: | ||||
* Brian Granger | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
|
r10642 | # Copyright (C) 2011 The IPython Development Team | ||
|
r10641 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r13055 | import os | ||
|
r10641 | |||
|
r11644 | from tornado import web | ||
|
r13916 | from ..base.handlers import IPythonHandler, notebook_path_regex, path_regex | ||
|
r15097 | from ..utils import url_path_join, path2url, url2path, url_escape, is_hidden | ||
|
r10641 | |||
#----------------------------------------------------------------------------- | ||||
|
r10642 | # Handlers | ||
|
r10641 | #----------------------------------------------------------------------------- | ||
|
r13055 | class TreeHandler(IPythonHandler): | ||
"""Render the tree view, listing notebooks, clusters, etc.""" | ||||
|
r10641 | |||
|
r15073 | def generate_breadcrumbs(self, path): | ||
|
r15238 | breadcrumbs = [(url_escape(url_path_join(self.base_url, 'tree')), '')] | ||
|
r15073 | comps = path.split('/') | ||
ncomps = len(comps) | ||||
for i in range(ncomps): | ||||
if comps[i]: | ||||
|
r15238 | link = url_escape(url_path_join(self.base_url, 'tree', *comps[0:i+1])) | ||
|
r15073 | breadcrumbs.append((link, comps[i])) | ||
return breadcrumbs | ||||
|
r15085 | def generate_page_title(self, path): | ||
comps = path.split('/') | ||||
if len(comps) > 3: | ||||
for i in range(len(comps)-2): | ||||
comps.pop(0) | ||||
|
r16636 | page_title = url_path_join(*comps) | ||
|
r15085 | if page_title: | ||
|
r15089 | return page_title+'/' | ||
|
r15085 | else: | ||
|
r15089 | return 'Home' | ||
|
r15085 | |||
|
r11644 | @web.authenticated | ||
|
r13067 | def get(self, path='', name=None): | ||
|
r13117 | path = path.strip('/') | ||
|
r12990 | nbm = self.notebook_manager | ||
|
r13033 | if name is not None: | ||
|
r13055 | # is a notebook, redirect to notebook handler | ||
|
r13135 | url = url_escape(url_path_join( | ||
|
r15238 | self.base_url, 'notebooks', path, name | ||
|
r13135 | )) | ||
self.log.debug("Redirecting %s to %s", self.request.path, url) | ||||
|
r13055 | self.redirect(url) | ||
|
r12990 | else: | ||
|
r15630 | if not nbm.path_exists(path=path): | ||
|
r15097 | # Directory is hidden or does not exist. | ||
|
r13055 | raise web.HTTPError(404) | ||
|
r15630 | elif nbm.is_hidden(path): | ||
self.log.info("Refusing to serve hidden directory, via 404 Error") | ||||
raise web.HTTPError(404) | ||||
|
r15073 | breadcrumbs = self.generate_breadcrumbs(path) | ||
|
r15085 | page_title = self.generate_page_title(path) | ||
|
r12990 | self.write(self.render_template('tree.html', | ||
|
r13055 | project=self.project_dir, | ||
|
r15085 | page_title=page_title, | ||
|
r12990 | notebook_path=path, | ||
|
r15073 | breadcrumbs=breadcrumbs | ||
|
r13055 | )) | ||
|
r12990 | |||
class TreeRedirectHandler(IPythonHandler): | ||||
|
r13055 | """Redirect a request to the corresponding tree URL""" | ||
|
r12992 | |||
|
r13014 | @web.authenticated | ||
|
r13067 | def get(self, path=''): | ||
|
r13135 | url = url_escape(url_path_join( | ||
|
r15238 | self.base_url, 'tree', path.strip('/') | ||
|
r13135 | )) | ||
self.log.debug("Redirecting %s to %s", self.request.path, url) | ||||
|
r12990 | self.redirect(url) | ||
|
r13003 | |||
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
|
r12990 | default_handlers = [ | ||
|
r13916 | (r"/tree%s" % notebook_path_regex, TreeHandler), | ||
(r"/tree%s" % path_regex, TreeHandler), | ||||
|
r13055 | (r"/tree", TreeHandler), | ||
|
r17678 | (r"/?", TreeRedirectHandler), | ||
|
r12990 | ] | ||