handlers.py
73 lines
| 2.4 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r10642 | """Tornado handlers for the tree view. | ||
Brian E. Granger
|
r10641 | |||
Authors: | ||||
* Brian Granger | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r10642 | # Copyright (C) 2011 The IPython Development Team | ||
Brian E. Granger
|
r10641 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r13055 | import os | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r11644 | from tornado import web | ||
from ..base.handlers import IPythonHandler | ||||
MinRK
|
r13055 | from ..utils import url_path_join, path2url, url2path | ||
MinRK
|
r13067 | from ..services.notebooks.handlers import _notebook_path_regex, _path_regex | ||
Brian E. Granger
|
r10641 | |||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r10642 | # Handlers | ||
Brian E. Granger
|
r10641 | #----------------------------------------------------------------------------- | ||
MinRK
|
r13055 | class TreeHandler(IPythonHandler): | ||
"""Render the tree view, listing notebooks, clusters, etc.""" | ||||
Brian E. Granger
|
r10641 | |||
MinRK
|
r11644 | @web.authenticated | ||
MinRK
|
r13067 | def get(self, path='', name=None): | ||
Zachary Sailer
|
r12990 | nbm = self.notebook_manager | ||
Zachary Sailer
|
r13033 | if name is not None: | ||
MinRK
|
r13055 | # is a notebook, redirect to notebook handler | ||
url = url_path_join(self.base_project_url, 'notebooks', path, name) | ||||
self.redirect(url) | ||||
Zachary Sailer
|
r12990 | else: | ||
MinRK
|
r13067 | if not nbm.path_exists(path=path): | ||
MinRK
|
r13055 | # no such directory, 404 | ||
raise web.HTTPError(404) | ||||
Zachary Sailer
|
r12990 | self.write(self.render_template('tree.html', | ||
MinRK
|
r13055 | project=self.project_dir, | ||
MinRK
|
r13067 | tree_url_path=path, | ||
Zachary Sailer
|
r12990 | notebook_path=path, | ||
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=''): | ||
url = url_path_join(self.base_project_url, 'tree', path).rstrip('/') | ||||
MinRK
|
r13055 | self.log.debug("Redirecting %s to %s", self.request.uri, url) | ||
Zachary Sailer
|
r12990 | self.redirect(url) | ||
Zachary Sailer
|
r13003 | |||
Brian E. Granger
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
Zachary Sailer
|
r12990 | default_handlers = [ | ||
MinRK
|
r13067 | (r"/tree/(.*)/", TreeRedirectHandler), | ||
(r"/tree/?%s" % _notebook_path_regex, TreeHandler), | ||||
(r"/tree/?%s" % _path_regex, TreeHandler), | ||||
Zachary Sailer
|
r12990 | (r"/tree/", TreeRedirectHandler), | ||
MinRK
|
r13055 | (r"/tree", TreeHandler), | ||
(r"/", TreeRedirectHandler), | ||||
Zachary Sailer
|
r12990 | ] | ||