Split read-only logic into three functions: read_only, logged_in, and login_available. Move display logic from javascript into templates.

Stefan van der Walt 14 years ago
parent 3bf57a7f5d
commit 52a2f081fe

@ -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,
)

@ -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.

@ -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>

@ -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 %}

@ -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 %}

@ -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>

@ -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>

Loading…
Cancel
Save