From 81edd9fca9b0003809182f0ce0107dd6f66835bb 2011-10-28 21:45:41 From: MinRK Date: 2011-10-28 21:45:41 Subject: [PATCH] move read_only flag to page-level contents of LPanel are not drawn until after collapse read_only is in a tag --- diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py index ff903d5..8e543e3 100644 --- a/IPython/frontend/html/notebook/handlers.py +++ b/IPython/frontend/html/notebook/handlers.py @@ -41,7 +41,7 @@ except ImportError: @decorator def not_if_readonly(f, self, *args, **kwargs): - if self.application.ipython_app.read_only: + if self.application.read_only: raise web.HTTPError(403, "Notebook server is read-only") else: return f(self, *args, **kwargs) @@ -57,7 +57,7 @@ def authenticate_unless_readonly(f, self, *args, **kwargs): @web.authenticated def auth_f(self, *args, **kwargs): return f(self, *args, **kwargs) - if self.application.ipython_app.read_only: + if self.application.read_only: return f(self, *args, **kwargs) else: return auth_f(self, *args, **kwargs) @@ -77,9 +77,20 @@ class AuthenticatedHandler(web.RequestHandler): if user_id is None: # prevent extra Invalid cookie sig warnings: self.clear_cookie('username') - if not self.application.password and not self.application.ipython_app.read_only: + if not self.application.password and not self.application.read_only: user_id = 'anonymous' return user_id + + @property + def read_only(self): + if self.application.read_only: + if self.application.password: + return self.get_current_user() is None + else: + return True + else: + return False + class ProjectDashboardHandler(AuthenticatedHandler): @@ -90,21 +101,24 @@ class ProjectDashboardHandler(AuthenticatedHandler): project = nbm.notebook_dir self.render( 'projectdashboard.html', project=project, - base_project_url=u'/', base_kernel_url=u'/' + base_project_url=u'/', base_kernel_url=u'/', + read_only=self.read_only, ) class LoginHandler(AuthenticatedHandler): def get(self): - self.render('login.html', next=self.get_argument('next', default='/')) + self.render('login.html', + next=self.get_argument('next', default='/'), + read_only=self.read_only, + ) def post(self): pwd = self.get_argument('password', default=u'') if self.application.password and pwd == self.application.password: self.set_secure_cookie('username', str(uuid.uuid4())) - url = self.get_argument('next', default='/') - self.redirect(url) + self.redirect(self.get_argument('next', default='/')) class NewHandler(AuthenticatedHandler): @@ -118,7 +132,8 @@ class NewHandler(AuthenticatedHandler): 'notebook.html', project=project, notebook_id=notebook_id, base_project_url=u'/', base_kernel_url=u'/', - kill_kernel=False + kill_kernel=False, + read_only=False, ) @@ -130,11 +145,13 @@ class NamedNotebookHandler(AuthenticatedHandler): project = nbm.notebook_dir if not nbm.notebook_exists(notebook_id): raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) + self.render( 'notebook.html', project=project, notebook_id=notebook_id, base_project_url=u'/', base_kernel_url=u'/', - kill_kernel=False + kill_kernel=False, + read_only=self.read_only, ) @@ -393,12 +410,6 @@ class NotebookRootHandler(AuthenticatedHandler): @authenticate_unless_readonly def get(self): - # communicate read-only via Allow header - if self.application.ipython_app.read_only and not self.get_current_user(): - self.set_header('Allow', 'GET') - else: - self.set_header('Allow', ', '.join(self.SUPPORTED_METHODS)) - nbm = self.application.notebook_manager files = nbm.list_notebooks() self.finish(jsonapi.dumps(files)) @@ -427,12 +438,6 @@ class NotebookHandler(AuthenticatedHandler): format = self.get_argument('format', default='json') last_mod, name, data = nbm.get_notebook(notebook_id, format) - # communicate read-only via Allow header - if self.application.ipython_app.read_only and not self.get_current_user(): - self.set_header('Allow', 'GET') - else: - self.set_header('Allow', ', '.join(self.SUPPORTED_METHODS)) - if format == u'json': self.set_header('Content-Type', 'application/json') self.set_header('Content-Disposition','attachment; filename="%s.ipynb"' % name) diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py index 71a53f9..8ae1084 100644 --- a/IPython/frontend/html/notebook/notebookapp.py +++ b/IPython/frontend/html/notebook/notebookapp.py @@ -105,6 +105,7 @@ class NotebookWebApplication(web.Application): self.log = log self.notebook_manager = notebook_manager self.ipython_app = ipython_app + self.read_only = self.ipython_app.read_only #----------------------------------------------------------------------------- diff --git a/IPython/frontend/html/notebook/static/js/leftpanel.js b/IPython/frontend/html/notebook/static/js/leftpanel.js index 4a41294..380510a 100644 --- a/IPython/frontend/html/notebook/static/js/leftpanel.js +++ b/IPython/frontend/html/notebook/static/js/leftpanel.js @@ -65,8 +65,10 @@ var IPython = (function (IPython) { LeftPanel.prototype.create_children = function () { this.notebook_section = new IPython.NotebookSection('div#notebook_section'); - this.cell_section = new IPython.CellSection('div#cell_section'); - this.kernel_section = new IPython.KernelSection('div#kernel_section'); + if (! IPython.read_only){ + this.cell_section = new IPython.CellSection('div#cell_section'); + this.kernel_section = new IPython.KernelSection('div#kernel_section'); + } this.help_section = new IPython.HelpSection('div#help_section'); } diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index b55d7fb..c27f701 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -14,7 +14,7 @@ var IPython = (function (IPython) { var utils = IPython.utils; var Notebook = function (selector) { - this.read_only = false; + this.read_only = IPython.read_only; this.element = $(selector); this.element.scroll(); this.element.data("notebook", this); @@ -979,13 +979,6 @@ var IPython = (function (IPython) { Notebook.prototype.notebook_loaded = function (data, status, xhr) { var allowed = xhr.getResponseHeader('Allow'); - if (allowed && allowed.indexOf('PUT') == -1){ - this.read_only = true; - // unhide login button if it's relevant - $('span#login_widget').removeClass('hidden'); - }else{ - this.read_only = false; - } this.fromJSON(data); if (this.ncells() === 0) { this.insert_code_cell_below(); @@ -993,9 +986,7 @@ var IPython = (function (IPython) { IPython.save_widget.status_save(); IPython.save_widget.set_notebook_name(data.metadata.name); this.dirty = false; - if (this.read_only) { - this.handle_read_only(); - }else{ + if (! this.read_only) { this.start_kernel(); } // fromJSON always selects the last cell inserted. We need to wait @@ -1006,16 +997,6 @@ var IPython = (function (IPython) { }, 50); }; - - Notebook.prototype.handle_read_only = function(){ - IPython.left_panel.collapse(); - IPython.save_widget.element.find('button#save_notebook').addClass('hidden'); - $('button#new_notebook').addClass('hidden'); - $('div#cell_section').addClass('hidden'); - $('div#kernel_section').addClass('hidden'); - } - - IPython.Notebook = Notebook; diff --git a/IPython/frontend/html/notebook/static/js/notebooklist.js b/IPython/frontend/html/notebook/static/js/notebooklist.js index 06156d8..316ef0e 100644 --- a/IPython/frontend/html/notebook/static/js/notebooklist.js +++ b/IPython/frontend/html/notebook/static/js/notebooklist.js @@ -73,15 +73,6 @@ var IPython = (function (IPython) { NotebookList.prototype.list_loaded = function (data, status, xhr) { - var allowed = xhr.getResponseHeader('Allow'); - if (allowed && allowed.indexOf('PUT') == -1){ - this.read_only = true; - $('#new_notebook').addClass('hidden'); - // unhide login button if it's relevant - $('span#login_widget').removeClass('hidden'); - }else{ - this.read_only = false; - } var len = data.length; // Todo: remove old children for (var i=0; i + + diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html index 40e69bc..0d924fc 100644 --- a/IPython/frontend/html/notebook/templates/notebook.html +++ b/IPython/frontend/html/notebook/templates/notebook.html @@ -40,7 +40,8 @@ - + + diff --git a/IPython/frontend/html/notebook/templates/projectdashboard.html b/IPython/frontend/html/notebook/templates/projectdashboard.html index fb87e60..386e261 100644 --- a/IPython/frontend/html/notebook/templates/projectdashboard.html +++ b/IPython/frontend/html/notebook/templates/projectdashboard.html @@ -12,6 +12,8 @@ + +