2009-12-10 00:57:23 +08:00
|
|
|
from django.contrib.messages import constants
|
2010-01-12 10:41:57 +08:00
|
|
|
from django.contrib.messages.storage import default_storage
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
'add_message', 'get_messages',
|
2010-03-21 21:47:50 +08:00
|
|
|
'get_level', 'set_level',
|
2009-12-10 00:57:23 +08:00
|
|
|
'debug', 'info', 'success', 'warning', 'error',
|
2013-11-23 22:35:24 +08:00
|
|
|
'MessageFailure',
|
2009-12-10 00:57:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MessageFailure(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def add_message(request, level, message, extra_tags='', fail_silently=False):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Attempt to add a message to the request using the 'messages' app.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
2017-01-10 07:03:10 +08:00
|
|
|
try:
|
|
|
|
messages = request._messages
|
|
|
|
except AttributeError:
|
|
|
|
if not hasattr(request, 'META'):
|
|
|
|
raise TypeError(
|
|
|
|
"add_message() argument must be an HttpRequest object, not "
|
|
|
|
"'%s'." % request.__class__.__name__
|
|
|
|
)
|
|
|
|
if not fail_silently:
|
|
|
|
raise MessageFailure(
|
|
|
|
'You cannot add messages without installing '
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware'
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return messages.add(level, message, extra_tags)
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_messages(request):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the message storage on the request if it exists, otherwise return
|
2011-04-08 06:01:23 +08:00
|
|
|
an empty list.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
2015-09-27 08:03:42 +08:00
|
|
|
return getattr(request, '_messages', [])
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
|
2010-01-12 10:41:57 +08:00
|
|
|
def get_level(request):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the minimum level of messages to be recorded.
|
2010-01-12 10:41:57 +08:00
|
|
|
|
|
|
|
The default level is the ``MESSAGE_LEVEL`` setting. If this is not found,
|
2017-01-25 04:31:57 +08:00
|
|
|
use the ``INFO`` level.
|
2010-01-12 10:41:57 +08:00
|
|
|
"""
|
2015-09-27 08:03:42 +08:00
|
|
|
storage = getattr(request, '_messages', default_storage(request))
|
2010-01-12 10:41:57 +08:00
|
|
|
return storage.level
|
|
|
|
|
|
|
|
|
|
|
|
def set_level(request, level):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Set the minimum level of messages to be recorded, and return ``True`` if
|
2010-01-12 10:41:57 +08:00
|
|
|
the level was recorded successfully.
|
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
If set to ``None``, use the default level (see the get_level() function).
|
2010-01-12 10:41:57 +08:00
|
|
|
"""
|
|
|
|
if not hasattr(request, '_messages'):
|
|
|
|
return False
|
|
|
|
request._messages.level = level
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2009-12-10 00:57:23 +08:00
|
|
|
def debug(request, message, extra_tags='', fail_silently=False):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Add a message with the ``DEBUG`` level."""
|
2009-12-10 00:57:23 +08:00
|
|
|
add_message(request, constants.DEBUG, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def info(request, message, extra_tags='', fail_silently=False):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Add a message with the ``INFO`` level."""
|
2009-12-10 00:57:23 +08:00
|
|
|
add_message(request, constants.INFO, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def success(request, message, extra_tags='', fail_silently=False):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Add a message with the ``SUCCESS`` level."""
|
2009-12-10 00:57:23 +08:00
|
|
|
add_message(request, constants.SUCCESS, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def warning(request, message, extra_tags='', fail_silently=False):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Add a message with the ``WARNING`` level."""
|
2009-12-10 00:57:23 +08:00
|
|
|
add_message(request, constants.WARNING, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def error(request, message, extra_tags='', fail_silently=False):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Add a message with the ``ERROR`` level."""
|
2009-12-10 00:57:23 +08:00
|
|
|
add_message(request, constants.ERROR, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|