Simplify Tree handlers

- 404 on missing path
- only need one handler for each of redirect and dashboard,
  don't need duplicates.
- project property on Handlers is unclear
This commit is contained in:
MinRK 2013-08-25 12:10:19 +02:00
parent ed66b8b8fe
commit 14fd50f80b
3 changed files with 27 additions and 43 deletions

View File

@ -222,7 +222,7 @@ class IPythonHandler(AuthenticatedHandler):
return self.settings['session_manager']
@property
def project(self):
def project_dir(self):
return self.notebook_manager.notebook_dir
#---------------------------------------------------------------

View File

@ -13,7 +13,6 @@
data-project={{project}}
data-base-project-url={{base_project_url}}
data-notebook-path={{notebook_path}}
data-notebook-name={{notebook_name}}
data-base-kernel-url={{base_kernel_url}}
{% endblock %}
@ -48,7 +47,7 @@ data-base-kernel-url={{base_kernel_url}}
<div id="notebook_list_header" class="row-fluid list_header">
<div id="project_name">
<ul class="breadcrumb">
{% for component in project_component %}
{% for component in tree_url_path.strip('/').split('/') %}
<li>{{component}} <span>/</span></li>
{% endfor %}
</ul>

View File

@ -15,64 +15,49 @@ Authors:
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
from tornado import web
from ..base.handlers import IPythonHandler
from ..utils import url_path_join, path2url, url2path
#-----------------------------------------------------------------------------
# Handlers
#-----------------------------------------------------------------------------
class ProjectDashboardHandler(IPythonHandler):
class TreeHandler(IPythonHandler):
"""Render the tree view, listing notebooks, clusters, etc."""
@web.authenticated
def get(self):
self.write(self.render_template('tree.html',
project=self.project,
project_component=self.project.split('/'),
notebook_path= "/"
))
class ProjectPathDashboardHandler(IPythonHandler):
@web.authenticated
def get(self, notebook_path):
def get(self, notebook_path=""):
nbm = self.notebook_manager
name, path = nbm.named_notebook_path(notebook_path)
if name is not None:
# ends with .ipynb
self.redirect(self.base_project_url + 'notebooks' + path + name)
# is a notebook, redirect to notebook handler
url = url_path_join(self.base_project_url, 'notebooks', path, name)
self.redirect(url)
else:
project = self.project + path
path = nbm.url_encode(path)
location = nbm.get_os_path(path=path)
if not os.path.exists(location):
# no such directory, 404
raise web.HTTPError(404)
self.write(self.render_template('tree.html',
project=project,
project_component=project.split('/')[:-1],
project=self.project_dir,
tree_url_path=path2url(location),
notebook_path=path,
notebook_name=name))
))
class TreeRedirectHandler(IPythonHandler):
@web.authenticated
def get(self):
url = self.base_project_url + 'tree'
self.redirect(url)
class TreePathRedirectHandler(IPythonHandler):
"""Redirect a request to the corresponding tree URL"""
@web.authenticated
def get(self, notebook_path):
url = self.base_project_url + 'tree/'+ notebook_path+'/'
self.redirect(url)
class ProjectRedirectHandler(IPythonHandler):
@web.authenticated
def get(self):
url = self.base_project_url + 'tree'
def get(self, notebook_path=''):
url = url_path_join(self.base_project_url, 'tree', notebook_path)
self.log.debug("Redirecting %s to %s", self.request.uri, url)
self.redirect(url)
@ -84,9 +69,9 @@ class ProjectRedirectHandler(IPythonHandler):
_notebook_path_regex = r"(?P<notebook_path>.+)"
default_handlers = [
(r"/tree/%s/" % _notebook_path_regex, ProjectPathDashboardHandler),
(r"/tree/%s" % _notebook_path_regex, TreePathRedirectHandler),
(r"/tree", ProjectDashboardHandler),
(r"/tree/%s/" % _notebook_path_regex, TreeRedirectHandler),
(r"/tree/%s" % _notebook_path_regex, TreeHandler),
(r"/tree/", TreeRedirectHandler),
(r"/", ProjectRedirectHandler)
(r"/tree", TreeHandler),
(r"/", TreeRedirectHandler),
]