Redirect paths from the notebooks route to the tree route if they are directories (#7446)

This commit is contained in:
Andy Schoenberger 2024-09-09 06:19:50 -04:00 committed by GitHub
parent 43b8cceb69
commit 59f8c306f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View File

@ -203,8 +203,16 @@ class NotebookHandler(NotebookBaseHandler):
"""A notebook page handler.""" """A notebook page handler."""
@web.authenticated @web.authenticated
def get(self, path: str | None = None) -> t.Any: # noqa: ARG002 async def get(self, path: str = "") -> t.Any:
"""Get the notebook page.""" """Get the notebook page. Redirect if it's a directory."""
path = path.strip("/")
cm = self.contents_manager
if await ensure_async(cm.dir_exists(path=path)):
url = ujoin(self.base_url, "tree", url_escape(path))
self.log.debug("Redirecting %s to %s since path is a directory", self.request.path, url)
self.redirect(url)
return None
tpl = self.render_template("notebooks.html", page_config=self.get_page_config()) tpl = self.render_template("notebooks.html", page_config=self.get_page_config())
return self.write(tpl) return self.write(tpl)

View File

@ -3,7 +3,7 @@ import os
import pytest import pytest
from tornado.httpclient import HTTPClientError from tornado.httpclient import HTTPClientError
from notebook.app import JupyterNotebookApp, TreeHandler from notebook.app import JupyterNotebookApp, NotebookHandler, TreeHandler
@pytest.fixture() @pytest.fixture()
@ -32,6 +32,16 @@ async def test_notebook_handler(notebooks, jp_fetch):
html = r.body.decode() html = r.body.decode()
assert "Jupyter Notebook" in html assert "Jupyter Notebook" in html
redirected_url = None
def redirect(self, url):
nonlocal redirected_url
redirected_url = url
NotebookHandler.redirect = redirect
await jp_fetch("notebooks", "jlab_test_notebooks")
assert redirected_url == "/a%40b/tree/jlab_test_notebooks"
async def test_tree_handler(notebooks, notebookapp, jp_fetch): async def test_tree_handler(notebooks, notebookapp, jp_fetch):
app: JupyterNotebookApp = notebookapp app: JupyterNotebookApp = notebookapp