fix dashboard upload

This commit is contained in:
MinRK 2013-10-07 14:45:24 -07:00
parent c21ea96476
commit 4e12521082
5 changed files with 30 additions and 27 deletions

View File

@ -192,16 +192,18 @@ class FileNotebookManager(NotebookManager):
the notebook model. If contents=True, returns the 'contents'
dict in the model as well.
"""
os_path = self.get_os_path(name, path)
if not os.path.isfile(os_path):
if not self.notebook_exists(name=name, path=path):
raise web.HTTPError(404, u'Notebook does not exist: %s' % name)
os_path = self.get_os_path(name, path)
info = os.stat(os_path)
last_modified = tz.utcfromtimestamp(info.st_mtime)
created = tz.utcfromtimestamp(info.st_ctime)
# Create the notebook model.
model ={}
model['name'] = name
model['path'] = path
model['last_modified'] = last_modified
model['created'] = last_modified
if content is True:
with open(os_path, 'r') as f:
try:
@ -211,7 +213,7 @@ class FileNotebookManager(NotebookManager):
model['content'] = nb
return model
def save_notebook_model(self, model, name, path=''):
def save_notebook_model(self, model, name='', path=''):
"""Save the notebook model and return the model with no content."""
if 'content' not in model:
@ -295,7 +297,7 @@ class FileNotebookManager(NotebookManager):
try:
os.rename(old_os_path, new_os_path)
except Exception as e:
raise web.HTTPError(400, u'Unknown error renaming notebook: %s %s' % (old_os_path, e))
raise web.HTTPError(500, u'Unknown error renaming notebook: %s %s' % (old_os_path, e))
# Move the checkpoints
old_checkpoints = self.list_checkpoints(old_name, old_path)

View File

@ -93,10 +93,7 @@ class NotebookHandler(IPythonHandler):
"""Create a new notebook in the location given by 'notebook_path'."""
nbm = self.notebook_manager
model = self.get_json_body()
if name is not None:
raise web.HTTPError(400, 'No name can be provided when POSTing a new notebook.')
model = nbm.create_notebook_model(model, path)
location = nbm.notebook_dir + model[u'path'] + model[u'name']
location = self.notebook_location(model[u'name'], model[u'path'])
self.set_header(u'Location', location)
self.set_header(u'Last-Modified', model[u'last_modified'])

View File

@ -18,12 +18,7 @@ Authors:
#-----------------------------------------------------------------------------
import os
import uuid
from urllib import quote, unquote
from tornado import web
from IPython.html.utils import url_path_join
from IPython.config.configurable import LoggingConfigurable
from IPython.nbformat import current
from IPython.utils.traitlets import List, Dict, Unicode, TraitError
@ -123,13 +118,16 @@ class NotebookManager(LoggingConfigurable):
def create_notebook_model(self, model=None, path=''):
"""Create a new untitled notebook and return its model with no content."""
name = self.increment_filename('Untitled', path)
untitled = self.increment_filename('Untitled', path)
if model is None:
model = {}
metadata = current.new_metadata(name=u'')
nb = current.new_notebook(metadata=metadata)
model['content'] = nb
model['name'] = name
model['name'] = name = untitled
model['path'] = path
else:
name = model.setdefault('name', untitled)
model['path'] = path
model = self.save_notebook_model(model, name, path)
return model

View File

@ -1661,13 +1661,11 @@ var IPython = (function (IPython) {
* @method save_notebook
*/
Notebook.prototype.save_notebook = function () {
// We may want to move the name/id/nbformat logic inside toJSON?
var data = this.toJSON();
var model = {};
// Create a JSON model to be sent to the server.
model['name'] = this.notebook_name;
model['path'] = this.notebook_path;
model['content'] = data
var model = {};
model.name = this.notebook_name;
model.path = this.notebook_path;
model.content = this.toJSON();
model.content.nbformat = this.nbformat;
model.content.nbformat_minor = this.nbformat_minor;
// time the ajax call for autosave tuning purposes.

View File

@ -232,7 +232,7 @@ var IPython = (function (IPython) {
NotebookList.prototype.add_notebook_data = function (data, item) {
item.data('nbdata',data);
item.data('nbdata', data);
};
@ -315,29 +315,37 @@ var IPython = (function (IPython) {
var nbname = item.find('.item_name > input').attr('value');
var nbformat = item.data('nbformat');
var nbdata = item.data('nbdata');
var content_type = 'text/plain';
var content_type = 'application/json';
if (nbformat === 'json') {
content_type = 'application/json';
// pass
} else if (nbformat === 'py') {
content_type = 'application/x-python';
// TODO: re-enable non-ipynb upload
}
var model = {
content : JSON.parse(nbdata),
name : nbname,
path : that.notebookPath()
};
var settings = {
processData : false,
cache : false,
type : 'POST',
dataType : 'json',
data : nbdata,
data : JSON.stringify(model),
headers : {'Content-Type': content_type},
success : function (data, status, xhr) {
that.add_link(data, nbname, item);
that.add_delete_button(item);
},
error : function (data, status, xhr) {
console.log(data, status);
}
};
var qs = $.param({name:nbname, format:nbformat});
var url = utils.url_path_join(
that.baseProjectUrl(),
'notebooks?' + qs
'api/notebooks',
that.notebookPath()
);
$.ajax(url, settings);
return false;