Skip to content

Commit

Permalink
fix(notebook): Allow notebooks to be trusted without triggering
Browse files Browse the repository at this point in the history
a save. Closes #195
  • Loading branch information
Madhu94 committed Aug 5, 2017
1 parent fcc5b0f commit 2208917
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
12 changes: 12 additions & 0 deletions notebook/services/contents/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ def get(self, path):
put = patch = post = delete = get


class TrustNotebooksHandler(IPythonHandler):
""" Handles trust/signing of notebooks """

@json_errors
@web.authenticated
@gen.coroutine
def post(self,path=''):
cm = self.contents_manager
yield gen.maybe_future(cm.trust_notebook(path))
self.set_status(201)
self.finish()
#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
Expand All @@ -318,6 +329,7 @@ def get(self, path):
(r"/api/contents%s/checkpoints" % path_regex, CheckpointsHandler),
(r"/api/contents%s/checkpoints/%s" % (path_regex, _checkpoint_id_regex),
ModifyCheckpointsHandler),
(r"/api/contents%s/trust" % path_regex, TrustNotebooksHandler),
(r"/api/contents%s" % path_regex, ContentsHandler),
(r"/api/notebooks/?(.*)", NotebooksRedirectHandler),
]
4 changes: 2 additions & 2 deletions notebook/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def trust_notebook(self, path):
nb = model['content']
self.log.warning("Trusting notebook %s", path)
self.notary.mark_cells(nb, True)
self.save(model, path)
self.check_and_sign(nb, path)

def check_and_sign(self, nb, path=''):
"""Check for trusted cells, and sign the notebook.
Expand All @@ -443,7 +443,7 @@ def check_and_sign(self, nb, path=''):
if self.notary.check_cells(nb):
self.notary.sign(nb)
else:
self.log.warning("Saving untrusted notebook %s", path)
self.log.warning("Notebook %s is not trusted", path)

def mark_trusted_cells(self, nb, path=''):
"""Mark cells as trusted if the notebook signature matches.
Expand Down
20 changes: 17 additions & 3 deletions notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2876,10 +2876,24 @@ define([
cell.output_area.trusted = true;
}
}
nb.events.on('notebook_saved.Notebook', function () {
window.location.reload();
// If its write only and dirty, save before
// trusting
var pr;
if(nb.writable && nb.dirty) {
pr = nb.save_notebook();
}
else {
pr = Promise.resolve();
}
return pr.then(function() {
nb.contents.trust(nb.notebook_path)
.then(function(res) {
nb.events.trigger("trust_changed.Notebook", true);
window.location.reload();
}, function(err) {
console.log(err);
});
});
nb.save_notebook();
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions notebook/static/services/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ define(function(require) {
return utils.promising_ajax(url, settings);
};

Contents.prototype.trust = function(path) {
var settings = {
processData : false,
type : "POST",
contentType: 'application/json',
};
var url = this.api_url(path, "trust");
return utils.promising_ajax(url, settings);
}

Contents.prototype.save = function(path, model) {
/**
* We do the call with settings so we can set cache to false.
Expand Down

0 comments on commit 2208917

Please sign in to comment.