Logout link in nav, refs #875

This commit is contained in:
Simon Willison 2020-06-29 11:40:40 -07:00
commit 2115d7e345
7 changed files with 73 additions and 2 deletions

View file

@ -44,6 +44,7 @@ from .database import Database, QueryInterrupted
from .utils import (
async_call_with_supported_arguments,
call_with_supported_arguments,
display_actor,
escape_css_string,
escape_sqlite,
format_bytes,
@ -736,6 +737,8 @@ class Datasette:
template_context = {
**context,
**{
"actor": request.actor if request else None,
"display_actor": display_actor,
"app_css_hash": self.app_css_hash(),
"zip": zip,
"body_scripts": body_scripts,

View file

@ -100,6 +100,14 @@ table a:visited {
.hd .crumbs {
float: left;
}
.hd .logout {
float: right;
text-align: right;
padding-left: 1em;
}
.hd .logout form {
display: inline;
}
.ft {
margin: 1em 0;
padding: 0.5em 1em 0 1em;
@ -367,3 +375,13 @@ p.zero-results {
border: 1px solid red;
background-color: pink;
}
button.button-as-link {
background: none;
border: none;
padding: 0;
color: blue;
text-decoration: none;
cursor: pointer;
font-size: 1em;
}

View file

@ -14,7 +14,17 @@
</head>
<body class="{% block body_class %}{% endblock %}">
<nav class="hd">{% block nav %}{% endblock %}</nav>
<nav class="hd">{% block nav %}
{% if actor %}
<div class="logout">
<strong>{{ display_actor(actor) }}</strong> &middot;
<form action="/-/logout" method="post">
<input type="hidden" name="csrftoken" value="{{ csrftoken() }}">
<button class="button-as-link">Log out</button>
</form>
</div>
{% endif %}
{% endblock %}</nav>
<div class="bd">
{% block messages %}

View file

@ -13,7 +13,7 @@
<h1>Log out</h1>
<p>You are logged in as <strong>{{ actor.id or actor }}</strong></p>
<p>You are logged in as <strong>{{ display_actor(actor) }}</strong></p>
<form action="/-/logout" method="post">
<div>

View file

@ -924,3 +924,10 @@ def resolve_env_secrets(config, environ):
return [resolve_env_secrets(value, environ) for value in config]
else:
return config
def display_actor(actor):
for key in ("display", "name", "username", "login", "id"):
if actor.get(key):
return actor[key]
return str(actor)