Skip to content

Commit da2d54f

Browse files
authored
Merge pull request #2718 from Madhu94/trust_without_save
Allow notebooks to be trusted without triggering a save
2 parents 114b916 + 2208917 commit da2d54f

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

notebook/services/contents/handlers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ def get(self, path):
307307
put = patch = post = delete = get
308308

309309

310+
class TrustNotebooksHandler(IPythonHandler):
311+
""" Handles trust/signing of notebooks """
312+
313+
@json_errors
314+
@web.authenticated
315+
@gen.coroutine
316+
def post(self,path=''):
317+
cm = self.contents_manager
318+
yield gen.maybe_future(cm.trust_notebook(path))
319+
self.set_status(201)
320+
self.finish()
310321
#-----------------------------------------------------------------------------
311322
# URL to handler mappings
312323
#-----------------------------------------------------------------------------
@@ -318,6 +329,7 @@ def get(self, path):
318329
(r"/api/contents%s/checkpoints" % path_regex, CheckpointsHandler),
319330
(r"/api/contents%s/checkpoints/%s" % (path_regex, _checkpoint_id_regex),
320331
ModifyCheckpointsHandler),
332+
(r"/api/contents%s/trust" % path_regex, TrustNotebooksHandler),
321333
(r"/api/contents%s" % path_regex, ContentsHandler),
322334
(r"/api/notebooks/?(.*)", NotebooksRedirectHandler),
323335
]

notebook/services/contents/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def trust_notebook(self, path):
426426
nb = model['content']
427427
self.log.warning("Trusting notebook %s", path)
428428
self.notary.mark_cells(nb, True)
429-
self.save(model, path)
429+
self.check_and_sign(nb, path)
430430

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

448448
def mark_trusted_cells(self, nb, path=''):
449449
"""Mark cells as trusted if the notebook signature matches.

notebook/static/notebook/js/notebook.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,10 +2876,24 @@ define([
28762876
cell.output_area.trusted = true;
28772877
}
28782878
}
2879-
nb.events.on('notebook_saved.Notebook', function () {
2880-
window.location.reload();
2879+
// If its write only and dirty, save before
2880+
// trusting
2881+
var pr;
2882+
if(nb.writable && nb.dirty) {
2883+
pr = nb.save_notebook();
2884+
}
2885+
else {
2886+
pr = Promise.resolve();
2887+
}
2888+
return pr.then(function() {
2889+
nb.contents.trust(nb.notebook_path)
2890+
.then(function(res) {
2891+
nb.events.trigger("trust_changed.Notebook", true);
2892+
window.location.reload();
2893+
}, function(err) {
2894+
console.log(err);
2895+
});
28812896
});
2882-
nb.save_notebook();
28832897
}
28842898
}
28852899
}

notebook/static/services/contents.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ define(function(require) {
161161
return utils.promising_ajax(url, settings);
162162
};
163163

164+
Contents.prototype.trust = function(path) {
165+
var settings = {
166+
processData : false,
167+
type : "POST",
168+
contentType: 'application/json',
169+
};
170+
var url = this.api_url(path, "trust");
171+
return utils.promising_ajax(url, settings);
172+
}
173+
164174
Contents.prototype.save = function(path, model) {
165175
/**
166176
* We do the call with settings so we can set cache to false.

0 commit comments

Comments
 (0)