diff --git a/IPython/html/services/notebooks/handlers.py b/IPython/html/services/notebooks/handlers.py index d84bbc155..b2d86a7d6 100644 --- a/IPython/html/services/notebooks/handlers.py +++ b/IPython/html/services/notebooks/handlers.py @@ -105,10 +105,7 @@ class NotebookHandler(IPythonHandler): notebook_name = nbm.save_new_notebook(body, notebook_path=notebook_path, name=name, format=format) else: notebook_name = nbm.new_notebook(notebook_path=notebook_path) - if notebook_path==None: - self.set_header('Location', nbm.notebook_dir + '/'+ notebook_name) - else: - self.set_header('Location', nbm.notebook_dir + '/'+ notebook_path + '/' + notebook_name) + self.set_header('Location', nbm.notebook_dir + notebook_path + notebook_name) model = nbm.notebook_model(notebook_name, notebook_path) self.finish(jsonapi.dumps(model)) else: diff --git a/IPython/html/services/notebooks/nbmanager.py b/IPython/html/services/notebooks/nbmanager.py index 2d8c6ef7a..9f5772344 100644 --- a/IPython/html/services/notebooks/nbmanager.py +++ b/IPython/html/services/notebooks/nbmanager.py @@ -44,7 +44,8 @@ class NotebookManager(LoggingConfigurable): def named_notebook_path(self, notebook_path): """Given a notebook_path name, returns a (name, path) tuple, where - name is a .ipynb file, and path is the directory for the file. + name is a .ipynb file, and path is the directory for the file, which + *always* starts *and* ends with a '/' character. Parameters ---------- @@ -59,10 +60,12 @@ class NotebookManager(LoggingConfigurable): the path to the directory which contains the notebook """ names = notebook_path.split('/') + names = [n for n in names if n != ''] # remove duplicate splits - name = names[-1] - if name.endswith(".ipynb"): - name = name + names = [''] + names + + if names and names[-1].endswith(".ipynb"): + name = names[-1] path = "/".join(names[:-1]) + '/' else: name = None diff --git a/IPython/html/services/notebooks/tests/test_nbmanager.py b/IPython/html/services/notebooks/tests/test_nbmanager.py index e8370c8ec..ab633dfd1 100644 --- a/IPython/html/services/notebooks/tests/test_nbmanager.py +++ b/IPython/html/services/notebooks/tests/test_nbmanager.py @@ -39,10 +39,11 @@ class TestNotebookManager(TestCase): # doesn't end with ipynb, should just be path name, path = nm.named_notebook_path('hello') self.assertEqual(name, None) - self.assertEqual(path, 'hello/') + self.assertEqual(path, '/hello/') name, path = nm.named_notebook_path('/') self.assertEqual(name, None) + self.assertEqual(path, '/') name, path = nm.named_notebook_path('hello.ipynb') self.assertEqual(name, 'hello.ipynb') @@ -55,5 +56,9 @@ class TestNotebookManager(TestCase): name, path = nm.named_notebook_path('/this/is/a/path/hello.ipynb') self.assertEqual(name, 'hello.ipynb') self.assertEqual(path, '/this/is/a/path/') + + name, path = nm.named_notebook_path('path/without/leading/slash/hello.ipynb') + self.assertEqual(name, 'hello.ipynb') + self.assertEqual(path, '/path/without/leading/slash/')