From bd48758673396d21a2258e3336e2d940994f44eb 2011-12-14 01:22:13
From: Stefan van der Walt <stefan@sun.ac.za>
Date: 2011-12-14 01:22:13
Subject: [PATCH] Split read-only logic into three functions: read_only, logged_in, and login_available.  Move display logic from javascript into templates.

---

diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py
index a34a747..f275d18 100644
--- a/IPython/frontend/html/notebook/handlers.py
+++ b/IPython/frontend/html/notebook/handlers.py
@@ -139,22 +139,29 @@ class AuthenticatedHandler(RequestHandler):
         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 a62caa3..516b406 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 93d0602..8ca2dcf 100644
--- a/IPython/frontend/html/notebook/templates/layout.html
+++ b/IPython/frontend/html/notebook/templates/layout.html
@@ -23,9 +23,9 @@
 <div id="header">
     <span id="ipython_notebook"><h1><img src='static/ipynblogo.png' alt='IPython Notebook'/></h1></span>
     <span id="login_widget">
-      {% if current_user and current_user != 'anonymous' %}
+      {% if logged_in %}
         <button id="logout">Logout</button>
-      {% elif read_only is None %}
+      {% elif login_available and not logged_in %}
         <button id="login">Login</button>
       {% end %}
     </span>
diff --git a/IPython/frontend/html/notebook/templates/login.html b/IPython/frontend/html/notebook/templates/login.html
index c2877ee..161f285 100644
--- a/IPython/frontend/html/notebook/templates/login.html
+++ b/IPython/frontend/html/notebook/templates/login.html
@@ -1,10 +1,16 @@
 {% extends layout.html %}
 
 {% block content_panel %}
+
+    {% if login_available %}
+
     <form action="/login?next={{url_escape(next)}}" method="post">
         Password: <input type="password" name="password" id="focus">
         <input type="submit" value="Sign in" id="signin">
     </form>
+
+    {% end %}
+
 {% end %}
 
 {% block script %}
diff --git a/IPython/frontend/html/notebook/templates/logout.html b/IPython/frontend/html/notebook/templates/logout.html
index 62ea4d8..94b9936 100644
--- a/IPython/frontend/html/notebook/templates/logout.html
+++ b/IPython/frontend/html/notebook/templates/logout.html
@@ -1,11 +1,19 @@
 {% extends layout.html %}
 
 {% block content_panel %}
-  {% if current_user and current_user != 'anonymous' %}
-    Proceed to the <a href="/">front page</a>.
-  {% else %}
-    Proceed to the <a href="/login">login page</a>.
-  {% end %}
+  <ul>
+    {% if read_only or not login_available %}
+
+    Proceed to the <a href="/">list of notebooks</a>.</li>
+
+    {% else %}
+
+    Proceed to the <a href="/login">login page</a>.</li>
+
+    {% end %}
+
+  </ul>
+
 {% end %}
 
 {% block script %}
diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html
index abcdded..5d313af 100644
--- a/IPython/frontend/html/notebook/templates/notebook.html
+++ b/IPython/frontend/html/notebook/templates/notebook.html
@@ -52,9 +52,9 @@
     <span id="login_widget">
       {% comment  This is a temporary workaround to hide the logout button %}
       {% comment  when appropriate until notebook.html is templated %}
-      {% if current_user and current_user != 'anonymous' %}
+      {% if logged_in %}
         <button id="logout">Logout</button>
-      {% elif read_only is None %}
+      {% elif not logged_in and login_available %}
         <button id="login">Login</button>
       {% end %}
     </span>
diff --git a/IPython/frontend/html/notebook/templates/projectdashboard.html b/IPython/frontend/html/notebook/templates/projectdashboard.html
index 993ab14..d3036af 100644
--- a/IPython/frontend/html/notebook/templates/projectdashboard.html
+++ b/IPython/frontend/html/notebook/templates/projectdashboard.html
@@ -19,17 +19,19 @@ data-base-kernel-url={{base_kernel_url}}
 {% end %}
 
 {% block content_panel %}
+    {% if logged_in or not read_only %}
+
     <div id="content_toolbar">
-        <span id="drag_info" class="hidden">Drag files onto the list to import
+        <span id="drag_info">Drag files onto the list to import
         notebooks.</span>
 
-        {% if read_only == False %}
-          <span id="notebooks_buttons">
-              <button id="new_notebook" class="hidden">New Notebook</button>
-          </span>
-        {% end %}
-
+        <span id="notebooks_buttons">
+          <button id="new_notebook">New Notebook</button>
+        </span>
     </div>
+
+    {% end %}
+
     <div id="notebook_list">
         <div id="project_name"><h2>{{project}}</h2></div>
     </div>