Commit 8cb3f0fd authored by Jan Čermák's avatar Jan Čermák

added convenience functions and docstrings to messages

parent 10d9d218
......@@ -15,18 +15,36 @@ ERROR = (30, "error")
class Message(object):
def __init__(self, text, level, extra_classes=None):
"""
Create new message instance.
:param text: text of the message
:param level: severity level
:param extra_classes: extra classes of message
"""
self.text = text
self.level = level
self.extra_classes = extra_classes
@property
def classes(self):
"""
Classes of the message.
:return: space-separated list of classes
"""
if self.extra_classes:
return " ".join([self.level[1], self.extra_classes])
return self.level[1]
def get_messages(level=None, min_level=None):
"""
Generator function yielding messages, optionally filtered by severity level.
:param level: get messages with exact level
:param min_level: get messages with level specified or higher
"""
def should_show():
if level and msg.level[0] == level[0]:
return True
......@@ -46,7 +64,54 @@ def get_messages(level=None, min_level=None):
yield msg
def info(text, extra_classes=None):
"""
Add new info message.
:param text: text of the message
:param extra_classes: extra classes of the message
"""
add_message(text, INFO, extra_classes)
def success(text, extra_classes=None):
"""
Add new success message.
:param text: text of the message
:param extra_classes: extra classes of the message
"""
add_message(text, SUCCESS, extra_classes)
def warning(text, extra_classes=None):
"""
Add new warning message.
:param text: text of the message
:param extra_classes: extra classes of the message
"""
add_message(text, WARNING, extra_classes)
def error(text, extra_classes=None):
"""
Add new error message.
:param text: text of the message
:param extra_classes: extra classes of the message
"""
add_message(text, ERROR, extra_classes)
def add_message(text, level=INFO, extra_classes=None):
"""
Add new message.
:param text: text of the message
:param level: severity level
:param extra_classes: extra classes of the message
"""
session = bottle.request.environ['beaker.session']
messages = session.get(_SESSION_KEY, [])
messages.append(Message(text, level, extra_classes))
......@@ -54,5 +119,11 @@ def add_message(text, level=INFO, extra_classes=None):
def set_template_defaults(template):
"""
Add template functions as template defaults to supplied Bottle template
adapter.
:param template: Bottle template adapter - class inheriting from BaseTemplate
"""
template.defaults['get_messages'] = get_messages
template.defaults['get_alert_messages'] = functools.partial(get_messages, min_level=WARNING)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment