-
-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flash messages mechanism, closes #790
- Loading branch information
Showing
14 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}Debug messages{% endblock %} | ||
|
||
{% block content %} | ||
|
||
<h1>Debug messages</h1> | ||
|
||
<p>Set a message:</p> | ||
|
||
<form action="/-/messages" method="POST"> | ||
<div> | ||
<input type="text" name="message" style="width: 40%"> | ||
<div class="select-wrapper"> | ||
<select name="message_type"> | ||
<option>INFO</option> | ||
<option>WARNING</option> | ||
<option>ERROR</option> | ||
<option>all</option> | ||
</select> | ||
</div> | ||
<input type="submit" value="Add message"> | ||
</div> | ||
</form> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from datasette import hookimpl | ||
|
||
|
||
def render_message_debug(datasette, request): | ||
if request.args.get("add_msg"): | ||
msg_type = request.args.get("type", "INFO") | ||
datasette.add_message( | ||
request, request.args["add_msg"], getattr(datasette, msg_type) | ||
) | ||
return {"body": "Hello from message debug"} | ||
|
||
|
||
@hookimpl | ||
def register_output_renderer(datasette): | ||
return [ | ||
{ | ||
"extension": "message", | ||
"render": render_message_debug, | ||
"can_render": lambda: False, | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from .fixtures import app_client | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"qs,expected", | ||
[ | ||
("add_msg=added-message", [["added-message", 1]]), | ||
("add_msg=added-warning&type=WARNING", [["added-warning", 2]]), | ||
("add_msg=added-error&type=ERROR", [["added-error", 3]]), | ||
], | ||
) | ||
def test_add_message_sets_cookie(app_client, qs, expected): | ||
response = app_client.get("/fixtures.message?{}".format(qs)) | ||
signed = response.cookies["ds_messages"] | ||
decoded = app_client.ds.unsign(signed, "messages") | ||
assert expected == decoded | ||
|
||
|
||
def test_messages_are_displayed_and_cleared(app_client): | ||
# First set the message cookie | ||
set_msg_response = app_client.get("/fixtures.message?add_msg=xmessagex") | ||
# Now access a page that displays messages | ||
response = app_client.get("/", cookies=set_msg_response.cookies) | ||
# Messages should be in that HTML | ||
assert "xmessagex" in response.text | ||
# Cookie should have been set that clears messages | ||
assert "" == response.cookies["ds_messages"] |