Last active
June 20, 2021 21:09
-
-
Save jhargis/90d519613e86f9b22d0a to your computer and use it in GitHub Desktop.
bottle-flash2 - Fixed up abandoned bottle-flash[https://pypi.python.org/pypi/bottle-flash/]. Made api ver 2 ready. Plugin enables flash messages in bottle similar to django and flask.
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
from bottle import request, response, PluginError | |
class FlashMsgPlugin(object): | |
"""Usage: | |
from bottle import Bottle | |
from bottle_flash2 import FlashMsgPlugin | |
app = Bottle() | |
COOKIE_SECRET = 'your_secret_key' | |
app.install(FlashPlugin(secret=COOKIE_SECRET)) | |
@route('/logout', name='logout') | |
def logout(): | |
app.flash('Logged out successfully !') | |
redirect(url('login')) | |
. To consume the flashed messages, | |
. you would do something like the following (jinja2). | |
. Here I am assuming that I get the "app" in the template context. | |
{% set messages = app.get_flashed_messages() %} | |
{% if messages %} | |
<div id="flash_messages"> | |
<ul> | |
{% for m in messages %} | |
<li>{{ m[0] }}</li> | |
{% endfor %} | |
</ul> | |
</div> | |
{% endif %} | |
. This can be achieved with the bottle_renderer_ plugin, | |
. or by adding to the defaults dict like this.. | |
Jinja2Template.defaults = {'app': app} | |
. placing the app in all template contexts. | |
""" | |
name = 'flash' | |
api = 2 | |
def __init__(self, key=name, secret=None): | |
self.key = key | |
self.app = None | |
self.secret = secret | |
def setup(self, app): | |
""" Make sure that other installed plugins don't affect the same | |
keyword argument and check if metadata is available.""" | |
for other in app.plugins: | |
if not isinstance(other, FlashMsgPlugin): | |
continue | |
if other.keyword == self.keyword: | |
raise PluginError("Found another flashmsg plugin " | |
"with conflicting settings (" | |
"non-unique keyword).") | |
self.app = app | |
self.app.add_hook('before_request', self.load_flashed) | |
self.app.add_hook('after_request', self.set_flashed) | |
self.app.flash = self.flash | |
self.app.get_flashed_messages = self.get_flashed_messages | |
def load_flashed(self): | |
m = request.get_cookie(self.key, secret=self.secret) | |
if m is not None: | |
response.flash_messages = m | |
def set_flashed(self): | |
if hasattr(response, 'flash_messages'): | |
return response.set_cookie(self.key, | |
response.flash_messages, | |
self.secret) | |
def flash(self, message, level=None): | |
if not hasattr(response, 'flash_messages'): | |
response.flash_messages = [] | |
response.flash_messages.append((message, level)) | |
def get_flashed_messages(self): | |
if hasattr(response, 'flash_messages'): | |
m = response.flash_messages | |
response.delete_cookie(self.key) | |
delattr(response, 'flash_messages') | |
return m | |
def apply(self, callback, context): | |
return callback | |
""" | |
The MIT License (MIT) | |
Copyright (c) 2014 | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for getting this working.
Just a quick note, at line 10 replace FlashPlugin with FlashMsgPlugin.