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
|
|
|
from django.utils.functional import lazy, memoize
|
|
|
|
|
|
|
|
__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',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MessageFailure(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def add_message(request, level, message, extra_tags='', fail_silently=False):
|
|
|
|
"""
|
2011-04-08 06:01:23 +08:00
|
|
|
Attempts to add a message to the request using the 'messages' app.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
|
|
|
if hasattr(request, '_messages'):
|
|
|
|
return request._messages.add(level, message, extra_tags)
|
|
|
|
if not fail_silently:
|
2011-04-08 06:01:23 +08:00
|
|
|
raise MessageFailure('You cannot add messages without installing '
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware')
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_messages(request):
|
|
|
|
"""
|
|
|
|
Returns the message storage on the request if it exists, otherwise returns
|
2011-04-08 06:01:23 +08:00
|
|
|
an empty list.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
|
|
|
if hasattr(request, '_messages'):
|
|
|
|
return request._messages
|
2011-04-08 06:01:23 +08:00
|
|
|
else:
|
|
|
|
return []
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
|
2010-01-12 10:41:57 +08:00
|
|
|
def get_level(request):
|
|
|
|
"""
|
|
|
|
Returns the minimum level of messages to be recorded.
|
|
|
|
|
|
|
|
The default level is the ``MESSAGE_LEVEL`` setting. If this is not found,
|
|
|
|
the ``INFO`` level is used.
|
|
|
|
"""
|
|
|
|
if hasattr(request, '_messages'):
|
|
|
|
storage = request._messages
|
|
|
|
else:
|
|
|
|
storage = default_storage(request)
|
|
|
|
return storage.level
|
|
|
|
|
|
|
|
|
|
|
|
def set_level(request, level):
|
|
|
|
"""
|
|
|
|
Sets the minimum level of messages to be recorded, returning ``True`` if
|
|
|
|
the level was recorded successfully.
|
|
|
|
|
|
|
|
If set to ``None``, the default level will be used (see the ``get_level``
|
|
|
|
method).
|
|
|
|
"""
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Adds a message with the ``DEBUG`` level.
|
|
|
|
"""
|
|
|
|
add_message(request, constants.DEBUG, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def info(request, message, extra_tags='', fail_silently=False):
|
|
|
|
"""
|
|
|
|
Adds a message with the ``INFO`` level.
|
|
|
|
"""
|
|
|
|
add_message(request, constants.INFO, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def success(request, message, extra_tags='', fail_silently=False):
|
|
|
|
"""
|
|
|
|
Adds a message with the ``SUCCESS`` level.
|
|
|
|
"""
|
|
|
|
add_message(request, constants.SUCCESS, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def warning(request, message, extra_tags='', fail_silently=False):
|
|
|
|
"""
|
|
|
|
Adds a message with the ``WARNING`` level.
|
|
|
|
"""
|
|
|
|
add_message(request, constants.WARNING, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
|
|
|
|
|
|
|
|
def error(request, message, extra_tags='', fail_silently=False):
|
|
|
|
"""
|
|
|
|
Adds a message with the ``ERROR`` level.
|
|
|
|
"""
|
|
|
|
add_message(request, constants.ERROR, message, extra_tags=extra_tags,
|
|
|
|
fail_silently=fail_silently)
|