From 52a2f081fe43eab418cf04ea44f0a764be218c26 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 13 Dec 2011 17:22:13 -0800 Subject: [PATCH] Split read-only logic into three functions: read_only, logged_in, and login_available. Move display logic from javascript into templates. --- IPython/frontend/html/notebook/handlers.py | 51 ++++++++++++++----- .../static/js/projectdashboardmain.js | 11 +--- .../html/notebook/templates/layout.html | 4 +- .../html/notebook/templates/login.html | 6 +++ .../html/notebook/templates/logout.html | 18 +++++-- .../html/notebook/templates/notebook.html | 4 +- .../notebook/templates/projectdashboard.html | 16 +++--- 7 files changed, 71 insertions(+), 39 deletions(-) diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py index a34a7474f..f275d1864 100644 --- a/IPython/frontend/html/notebook/handlers.py +++ b/IPython/frontend/html/notebook/handlers.py @@ -138,23 +138,30 @@ class AuthenticatedHandler(RequestHandler): user_id = 'anonymous' return user_id + @property + def logged_in(self): + """Is a user currently logged in? + + """ + user = self.get_current_user() + return (user and not user == 'anonymous') + + @property + def login_available(self): + """May a user proceed to log in? + + This returns True if login capability is available, irrespective of + whether the user is already logged in or not. + + """ + return bool(self.application.password) + @property def read_only(self): """Is the notebook read-only? - None -- notebook is read-only, but the user can log-in to edit - True -- notebook is read-only, no log-in available - False -- no read-only mode available, user must log in - """ - user = self.get_current_user() - if user and user != 'anonymous': - return False - elif self.application.read_only: - if self.application.password: - return None - else: - return True + return self.application.read_only @property def ws_url(self): @@ -177,6 +184,8 @@ class ProjectDashboardHandler(AuthenticatedHandler): 'projectdashboard.html', project=project, base_project_url=u'/', base_kernel_url=u'/', read_only=self.read_only, + logged_in=self.logged_in, + login_available=self.login_available ) @@ -186,6 +195,8 @@ class LoginHandler(AuthenticatedHandler): self.render('login.html', next=self.get_argument('next', default='/'), read_only=self.read_only, + logged_in=self.logged_in, + login_available=self.login_available, message=message ) @@ -211,7 +222,17 @@ class LogoutHandler(AuthenticatedHandler): def get(self): self.clear_cookie('username') - self.render('logout.html', message={'info': 'Successfully logged out.'}) + if self.login_available: + message = {'info': 'Successfully logged out.'} + else: + message = {'warning': 'Cannot log out. Notebook authentication ' + 'is disabled.'} + + self.render('logout.html', + read_only=self.read_only, + logged_in=self.logged_in, + login_available=self.login_available, + message=message) class NewHandler(AuthenticatedHandler): @@ -227,6 +248,8 @@ class NewHandler(AuthenticatedHandler): base_project_url=u'/', base_kernel_url=u'/', kill_kernel=False, read_only=False, + logged_in=self.logged_in, + login_available=self.login_available, mathjax_url=self.application.ipython_app.mathjax_url, ) @@ -246,6 +269,8 @@ class NamedNotebookHandler(AuthenticatedHandler): base_project_url=u'/', base_kernel_url=u'/', kill_kernel=False, read_only=self.read_only, + logged_in=self.logged_in, + login_available=self.login_available, mathjax_url=self.application.ipython_app.mathjax_url, ) diff --git a/IPython/frontend/html/notebook/static/js/projectdashboardmain.js b/IPython/frontend/html/notebook/static/js/projectdashboardmain.js index a62caa383..516b406ba 100644 --- a/IPython/frontend/html/notebook/static/js/projectdashboardmain.js +++ b/IPython/frontend/html/notebook/static/js/projectdashboardmain.js @@ -28,18 +28,9 @@ $(document).ready(function () { $('div#right_panel').addClass('box-flex'); IPython.read_only = $('meta[name=read_only]').attr("content") == 'True'; - IPython.notebook_list = new IPython.NotebookList('div#notebook_list'); IPython.login_widget = new IPython.LoginWidget('span#login_widget'); - - if (IPython.read_only){ - // unhide login button if it's relevant - $('span#login_widget').removeClass('hidden'); - $('#drag_info').remove(); - } else { - $('#new_notebook').removeClass('hidden'); - $('#drag_info').removeClass('hidden'); - } + IPython.notebook_list.load_list(); // These have display: none in the css file and are made visible here to prevent FLOUC. diff --git a/IPython/frontend/html/notebook/templates/layout.html b/IPython/frontend/html/notebook/templates/layout.html index 93d0602e1..8ca2dcf40 100644 --- a/IPython/frontend/html/notebook/templates/layout.html +++ b/IPython/frontend/html/notebook/templates/layout.html @@ -23,9 +23,9 @@