##// END OF EJS Templates
remove unused cm_loadmode
remove unused cm_loadmode

File last commit:

r18262:646ce535
r18287:4a0162b8
Show More
handlers.py
303 lines | 10.1 KiB | text/x-python | PythonLexer
MinRK
rename notebooks service to contents service...
r17524 """Tornado handlers for the contents web service."""
Brian E. Granger
Adding new files.
r10641
MinRK
rename notebooks service to contents service...
r17524 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian E. Granger
Adding new files.
r10641
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 import json
Brian E. Granger
Adding new files.
r10641
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 from tornado import web
Brian E. Granger
Adding new files.
r10641
MinRK
escape URLs in Location headers
r13132 from IPython.html.utils import url_path_join, url_escape
Brian E. Granger
Adding new files.
r10641 from IPython.utils.jsonutil import date_default
Thomas Kluyver
Move notebook URL fragment regexen into IPython.html.base.handlers
r13916 from IPython.html.base.handlers import (IPythonHandler, json_errors,
MinRK
teach contents service about non-notebook files
r17525 file_path_regex, path_regex,
file_name_regex)
Brian E. Granger
Adding new files.
r10641
Zachary Sailer
manual rebase notebooks web services
r12984
MinRK
contents service review...
r17529 def sort_key(model):
"""key function for case-insensitive sort by name and type"""
iname = model['name'].lower()
type_key = {
'directory' : '0',
'notebook' : '1',
'file' : '2',
}.get(model['type'], '9')
return u'%s%s' % (type_key, iname)
MinRK
rename notebooks service to contents service...
r17524 class ContentsHandler(IPythonHandler):
Paul Ivanov
removing contents changes from this PR
r13042
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE')
MinRK
teach contents service about non-notebook files
r17525 def location_url(self, name, path):
MinRK
rename notebooks service to contents service...
r17524 """Return the full URL location of a file.
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 Parameters
----------
name : unicode
MinRK
rename notebooks service to contents service...
r17524 The base name of the file, such as "foo.ipynb".
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 path : unicode
MinRK
rename notebooks service to contents service...
r17524 The API path of the file, such as "foo/bar".
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 """
MinRK
escape URLs in Location headers
r13132 return url_escape(url_path_join(
MinRK
rename notebooks service to contents service...
r17524 self.base_url, 'api', 'contents', path, name
MinRK
escape URLs in Location headers
r13132 ))
Paul Ivanov
removing contents changes from this PR
r13042
MinRK
update notebook creation handlers...
r13129 def _finish_model(self, model, location=True):
"""Finish a JSON request with a model, setting relevant headers, etc."""
if location:
MinRK
rename notebooks service to contents service...
r17524 location = self.location_url(model['name'], model['path'])
MinRK
update notebook creation handlers...
r13129 self.set_header('Location', location)
self.set_header('Last-Modified', model['last_modified'])
self.finish(json.dumps(model, default=date_default))
MinRK
mv services/notebooks services/contents
r17523
Paul Ivanov
removing contents changes from this PR
r13042 @web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def get(self, path='', name=None):
MinRK
updates per review...
r17535 """Return a model for a file or directory.
Brian E. Granger
Adding docstring to NotebookHandler.get.
r13113
MinRK
updates per review...
r17535 A directory model contains a list of models (without content)
of the files and directories it contains.
MinRK
adjust definition of 'path' in notebooks...
r13067 """
MinRK
teach contents service about non-notebook files
r17525 path = path or ''
model = self.contents_manager.get_model(name=name, path=path)
if model['type'] == 'directory':
MinRK
contents service review...
r17529 # group listing by type, then by name (case-insensitive)
MinRK
updates per review...
r17535 # FIXME: sorting should be done in the frontends
MinRK
contents service review...
r17529 model['content'].sort(key=sort_key)
MinRK
update notebook creation handlers...
r13129 self._finish_model(model, location=False)
Brian E. Granger
Adding new files.
r10641
Paul Ivanov
removing contents changes from this PR
r13042 @web.authenticated
MinRK
adjust definition of 'path' in notebooks...
r13067 @json_errors
def patch(self, path='', name=None):
MinRK
update upload and copy...
r13074 """PATCH renames a notebook without re-uploading content."""
MinRK
rename notebooks service to contents service...
r17524 cm = self.contents_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 if name is None:
MinRK
rename notebooks service to contents service...
r17524 raise web.HTTPError(400, u'Filename missing')
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 model = self.get_json_body()
if model is None:
raise web.HTTPError(400, u'JSON body missing')
MinRK
rename notebooks service to contents service...
r17524 model = cm.update(model, name, path)
MinRK
update notebook creation handlers...
r13129 self._finish_model(model)
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 def _copy(self, copy_from, path, copy_to=None):
MinRK
updates per review...
r17535 """Copy a file, optionally specifying the new name.
MinRK
update notebook creation handlers...
r13129 """
MinRK
updates per review...
r17535 self.log.info(u"Copying {copy_from} to {path}/{copy_to}".format(
copy_from=copy_from,
path=path,
copy_to=copy_to or '',
))
MinRK
rename notebooks service to contents service...
r17524 model = self.contents_manager.copy(copy_from, copy_to, path)
MinRK
update notebook creation handlers...
r13129 self.set_status(201)
self._finish_model(model)
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 def _upload(self, model, path, name=None):
MinRK
updates per review...
r17535 """Handle upload of a new file
MinRK
mv services/notebooks services/contents
r17523
MinRK
updates per review...
r17535 If name specified, create it in path/name,
otherwise create a new untitled file in path.
MinRK
update notebook creation handlers...
r13129 """
MinRK
rename notebooks service to contents service...
r17524 self.log.info(u"Uploading file to %s/%s", path, name or '')
MinRK
update notebook creation handlers...
r13129 if name:
model['name'] = name
MinRK
mv services/notebooks services/contents
r17523
MinRK
add support and tests for uploading and saving regular files
r17527 model = self.contents_manager.create_file(model, path)
MinRK
update notebook creation handlers...
r13129 self.set_status(201)
self._finish_model(model)
MinRK
mv services/notebooks services/contents
r17523
MinRK
add support and tests for uploading and saving regular files
r17527 def _create_empty_file(self, path, name=None, ext='.ipynb'):
"""Create an empty file in path
MinRK
mv services/notebooks services/contents
r17523
MinRK
update notebook creation handlers...
r13129 If name specified, create it in path/name.
"""
MinRK
add support and tests for uploading and saving regular files
r17527 self.log.info(u"Creating new file in %s/%s", path, name or '')
MinRK
update notebook creation handlers...
r13129 model = {}
if name:
model['name'] = name
MinRK
add support and tests for uploading and saving regular files
r17527 model = self.contents_manager.create_file(model, path=path, ext=ext)
MinRK
update notebook creation handlers...
r13129 self.set_status(201)
self._finish_model(model)
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 def _save(self, model, path, name):
"""Save an existing file."""
self.log.info(u"Saving file at %s/%s", path, name)
model = self.contents_manager.save(model, name, path)
MinRK
update notebook creation handlers...
r13129 if model['path'] != path.strip('/') or model['name'] != name:
# a rename happened, set Location header
location = True
else:
location = False
self._finish_model(model, location)
MinRK
mv services/notebooks services/contents
r17523
Paul Ivanov
removing contents changes from this PR
r13042 @web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def post(self, path='', name=None):
MinRK
contents service review...
r17529 """Create a new file or directory in the specified path.
MinRK
mv services/notebooks services/contents
r17523
MinRK
contents service review...
r17529 POST creates new files or directories. The server always decides on the name.
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 POST /api/contents/path
Thomas Kluyver
Miscellaneous docs fixes
r13597 New untitled notebook in path. If content specified, upload a
notebook, otherwise start empty.
MinRK
add support and tests for uploading and saving regular files
r17527 POST /api/contents/path
with body {"copy_from" : "OtherNotebook.ipynb"}
Thomas Kluyver
Miscellaneous docs fixes
r13597 New copy of OtherNotebook in path
MinRK
update upload and copy...
r13074 """
MinRK
mv services/notebooks services/contents
r17523
MinRK
update notebook creation handlers...
r13129 if name is not None:
MinRK
teach contents service about non-notebook files
r17525 path = u'{}/{}'.format(path, name)
cm = self.contents_manager
if cm.file_exists(path):
MinRK
updates per review...
r17535 raise web.HTTPError(400, "Cannot POST to existing files, use PUT instead.")
MinRK
mv services/notebooks services/contents
r17523
MinRK
teach contents service about non-notebook files
r17525 if not cm.path_exists(path):
raise web.HTTPError(404, "No such directory: %s" % path)
MinRK
copy_from in json, not in url param
r13137 model = self.get_json_body()
MinRK
mv services/notebooks services/contents
r17523
MinRK
copy_from in json, not in url param
r13137 if model is not None:
copy_from = model.get('copy_from')
MinRK
add support and tests for uploading and saving regular files
r17527 ext = model.get('ext', '.ipynb')
if model.get('content') is not None:
if copy_from:
MinRK
copy_from in json, not in url param
r13137 raise web.HTTPError(400, "Can't upload and copy at the same time.")
MinRK
add support and tests for uploading and saving regular files
r17527 self._upload(model, path)
elif copy_from:
MinRK
rename notebooks service to contents service...
r17524 self._copy(copy_from, path)
MinRK
copy_from in json, not in url param
r13137 else:
MinRK
add support and tests for uploading and saving regular files
r17527 self._create_empty_file(path, ext=ext)
MinRK
update notebook creation handlers...
r13129 else:
MinRK
add support and tests for uploading and saving regular files
r17527 self._create_empty_file(path)
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def put(self, path='', name=None):
MinRK
rename notebooks service to contents service...
r17524 """Saves the file in the location specified by name and path.
MinRK
mv services/notebooks services/contents
r17523
Thomas Kluyver
Miscellaneous docs fixes
r13597 PUT is very similar to POST, but the requester specifies the name,
whereas with POST, the server picks the name.
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 PUT /api/contents/path/Name.ipynb
Thomas Kluyver
Miscellaneous docs fixes
r13597 Save notebook at ``path/Name.ipynb``. Notebook structure is specified
in `content` key of JSON request body. If content is not specified,
create a new empty notebook.
MinRK
updates per review...
r17535 PUT /api/contents/path/Name.ipynb
with JSON body::
{
"copy_from" : "[path/to/]OtherNotebook.ipynb"
}
Thomas Kluyver
Miscellaneous docs fixes
r13597 Copy OtherNotebook to Name
MinRK
update notebook creation handlers...
r13129 """
if name is None:
MinRK
updates per review...
r17535 raise web.HTTPError(400, "name must be specified with PUT.")
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 model = self.get_json_body()
MinRK
copy_from in json, not in url param
r13137 if model:
copy_from = model.get('copy_from')
if copy_from:
if model.get('content'):
raise web.HTTPError(400, "Can't upload and copy at the same time.")
MinRK
rename notebooks service to contents service...
r17524 self._copy(copy_from, path, name)
elif self.contents_manager.file_exists(name, path):
self._save(model, path, name)
MinRK
update notebook creation handlers...
r13129 else:
MinRK
rename notebooks service to contents service...
r17524 self._upload(model, path, name)
MinRK
update notebook creation handlers...
r13129 else:
MinRK
add support and tests for uploading and saving regular files
r17527 self._create_empty_file(path, name)
Brian E. Granger
Adding new files.
r10641
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def delete(self, path='', name=None):
MinRK
rename notebooks service to contents service...
r17524 """delete a file in the given path"""
cm = self.contents_manager
MinRK
teach contents service about non-notebook files
r17525 self.log.warn('delete %s:%s', path, name)
MinRK
rename notebooks service to contents service...
r17524 cm.delete(name, path)
Brian E. Granger
Adding new files.
r10641 self.set_status(204)
self.finish()
Paul Ivanov
removing contents changes from this PR
r13042
MinRK
rename notebooks service to contents service...
r17524 class CheckpointsHandler(IPythonHandler):
MinRK
mv services/notebooks services/contents
r17523
Paul Ivanov
removing contents changes from this PR
r13042 SUPPORTED_METHODS = ('GET', 'POST')
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
change standard money keys
r13015 @web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def get(self, path='', name=None):
MinRK
rename notebooks service to contents service...
r17524 """get lists checkpoints for a file"""
cm = self.contents_manager
checkpoints = cm.list_checkpoints(name, path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 data = json.dumps(checkpoints, default=date_default)
Paul Ivanov
removing contents changes from this PR
r13042 self.finish(data)
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
change standard money keys
r13015 @web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def post(self, path='', name=None):
Paul Ivanov
removing contents changes from this PR
r13042 """post creates a new checkpoint"""
MinRK
rename notebooks service to contents service...
r17524 cm = self.contents_manager
checkpoint = cm.create_checkpoint(name, path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 data = json.dumps(checkpoint, default=date_default)
MinRK
rename notebooks service to contents service...
r17524 location = url_path_join(self.base_url, 'api/contents',
MinRK
use 'id' for checkpoint ID key...
r13122 path, name, 'checkpoints', checkpoint['id'])
MinRK
escape URLs in Location headers
r13132 self.set_header('Location', url_escape(location))
Thomas Kluyver
Fixes for notebook checkpoint APIs
r13110 self.set_status(201)
Paul Ivanov
removing contents changes from this PR
r13042 self.finish(data)
MinRK
rename notebooks service to contents service...
r17524 class ModifyCheckpointsHandler(IPythonHandler):
MinRK
mv services/notebooks services/contents
r17523
Paul Ivanov
removing contents changes from this PR
r13042 SUPPORTED_METHODS = ('POST', 'DELETE')
MinRK
mv services/notebooks services/contents
r17523
Paul Ivanov
removing contents changes from this PR
r13042 @web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def post(self, path, name, checkpoint_id):
MinRK
rename notebooks service to contents service...
r17524 """post restores a file from a checkpoint"""
cm = self.contents_manager
cm.restore_checkpoint(checkpoint_id, name, path)
Paul Ivanov
removing contents changes from this PR
r13042 self.set_status(204)
self.finish()
MinRK
mv services/notebooks services/contents
r17523
Paul Ivanov
removing contents changes from this PR
r13042 @web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
MinRK
adjust definition of 'path' in notebooks...
r13067 def delete(self, path, name, checkpoint_id):
MinRK
rename notebooks service to contents service...
r17524 """delete clears a checkpoint for a given file"""
cm = self.contents_manager
cm.delete_checkpoint(checkpoint_id, name, path)
Paul Ivanov
removing contents changes from this PR
r13042 self.set_status(204)
self.finish()
MinRK
mv services/notebooks services/contents
r17523
MinRK
redirect /api/notebooks to /api/contents...
r18262
class NotebooksRedirectHandler(IPythonHandler):
"""Redirect /api/notebooks to /api/contents"""
SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH', 'POST', 'DELETE')
def get(self, path):
self.log.warn("/api/notebooks is deprecated, use /api/contents")
self.redirect(url_path_join(
self.base_url,
'api/contents',
path
))
put = patch = post = delete = get
Brian E. Granger
More work on the handlers
r10647 #-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
Paul Ivanov
removing contents changes from this PR
r13042
_checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)"
Brian E. Granger
More work on the handlers
r10647
default_handlers = [
MinRK
teach contents service about non-notebook files
r17525 (r"/api/contents%s/checkpoints" % file_path_regex, CheckpointsHandler),
(r"/api/contents%s/checkpoints/%s" % (file_path_regex, _checkpoint_id_regex),
MinRK
rename notebooks service to contents service...
r17524 ModifyCheckpointsHandler),
MinRK
teach contents service about non-notebook files
r17525 (r"/api/contents%s" % file_path_regex, ContentsHandler),
MinRK
rename notebooks service to contents service...
r17524 (r"/api/contents%s" % path_regex, ContentsHandler),
MinRK
redirect /api/notebooks to /api/contents...
r18262 (r"/api/notebooks/?(.*)", NotebooksRedirectHandler),
Brian E. Granger
More work on the handlers
r10647 ]