Skip to content

Commit

Permalink
Merge pull request ipython#4527 from minrk/files
Browse files Browse the repository at this point in the history
Add redirect for 1.0-style 'files/' prefix links, this makes it easier to develop notebooks on master and share them with 1.x users.
  • Loading branch information
fperez committed Nov 13, 2013
2 parents ec6e7e1 + 998329f commit 559f04f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
14 changes: 13 additions & 1 deletion IPython/html/notebook/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Imports
#-----------------------------------------------------------------------------

import os
from tornado import web
HTTPError = web.HTTPError

Expand Down Expand Up @@ -61,7 +62,18 @@ def get(self, path=''):
url = url_path_join(self.base_project_url, 'tree', path)
else:
# otherwise, redirect to /files
# TODO: This should check if it's actually a file
if '/files/' in path:
# redirect without files/ iff it would 404
# this preserves pre-2.0-style 'files/' links
# FIXME: this is hardcoded based on notebook_path,
# but so is the files handler itself,
# so it should work until both are cleaned up.
parts = path.split('/')
files_path = os.path.join(nbm.notebook_dir, *parts)
self.log.warn("filespath: %s", files_path)
if not os.path.exists(files_path):
path = path.replace('/files/', '/', 1)

url = url_path_join(self.base_project_url, 'files', path)
url = url_escape(url)
self.log.debug("Redirecting %s to %s", self.request.path, url)
Expand Down
40 changes: 37 additions & 3 deletions IPython/html/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class FilesTest(NotebookTestBase):
def test_hidden_files(self):
not_hidden = [
u'å b',
pjoin(u'å b/ç. d')
u'å b/ç. d',
]
hidden = [
u'.å b',
pjoin(u'å b/.ç d')
u'å b/.ç d',
]
dirs = not_hidden + hidden

Expand All @@ -40,7 +40,7 @@ def test_hidden_files(self):
path = pjoin(nbdir, d.replace('/', os.sep))
r = requests.get(url_path_join(url, 'files', d, 'foo'))
r.raise_for_status()
self.assertEqual(r.content, b'foo')
self.assertEqual(r.text, 'foo')
r = requests.get(url_path_join(url, 'files', d, '.foo'))
self.assertEqual(r.status_code, 403)

Expand All @@ -49,3 +49,37 @@ def test_hidden_files(self):
for foo in ('foo', '.foo'):
r = requests.get(url_path_join(url, 'files', d, foo))
self.assertEqual(r.status_code, 403)

def test_old_files_redirect(self):
"""pre-2.0 'files/' prefixed links are properly redirected"""
nbdir = self.notebook_dir.name
base = self.base_url()

os.mkdir(pjoin(nbdir, 'files'))
os.makedirs(pjoin(nbdir, 'sub', 'files'))

for prefix in ('', 'sub'):
with open(pjoin(nbdir, prefix, 'files', 'f1.txt'), 'w') as f:
f.write(prefix + '/files/f1')
with open(pjoin(nbdir, prefix, 'files', 'f2.txt'), 'w') as f:
f.write(prefix + '/files/f2')
with open(pjoin(nbdir, prefix, 'f2.txt'), 'w') as f:
f.write(prefix + '/f2')
with open(pjoin(nbdir, prefix, 'f3.txt'), 'w') as f:
f.write(prefix + '/f3')

url = url_path_join(base, 'notebooks', prefix, 'files', 'f1.txt')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, prefix + '/files/f1')

url = url_path_join(base, 'notebooks', prefix, 'files', 'f2.txt')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, prefix + '/files/f2')

url = url_path_join(base, 'notebooks', prefix, 'files', 'f3.txt')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, prefix + '/f3')

0 comments on commit 559f04f

Please sign in to comment.