attempt to add a message to the request using the messages app return the message storage on the request if it exists otherwise return an empty list return the minimum level of messages to be recorded the default level is the messagelevel setting if this is not found use the info level set the minimum level of messages to be recorded and return true if the level was recorded successfully if set to none use the default level see the getlevel function add a message with the debug level addmessage request constants debug message extratagsextratags failsilentlyfailsilently def inforequest message extratags failsilentlyfalse add a message with the success level addmessage request constants success message extratagsextratags failsilentlyfailsilently def warningrequest message extratags failsilentlyfalse add a message with the error level addmessage request constants error message extratagsextratags failsilentlyfailsilently attempt to add a message to the request using the messages app return the message storage on the request if it exists otherwise return an empty list return the minimum level of messages to be recorded the default level is the message_level setting if this is not found use the info level set the minimum level of messages to be recorded and return true if the level was recorded successfully if set to none use the default level see the get_level function add a message with the debug level add a message with the info level add a message with the success level add a message with the warning level add a message with the error level
from django.contrib.messages import constants
from django.contrib.messages.storage import default_storage

__all__ = (
    "add_message",
    "get_messages",
    "get_level",
    "set_level",
    "debug",
    "info",
    "success",
    "warning",
    "error",
    "MessageFailure",
)


class MessageFailure(Exception):
    pass


def add_message(request, level, message, extra_tags="", fail_silently=False):
    
    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)


def get_messages(request):
    
    return getattr(request, "_messages", [])


def get_level(request):
    
    storage = getattr(request, "_messages", default_storage(request))
    return storage.level


def set_level(request, level):
    
    if not hasattr(request, "_messages"):
        return False
    request._messages.level = level
    return True


def debug(request, message, extra_tags="", fail_silently=False):
    
    add_message(
        request,
        constants.DEBUG,
        message,
        extra_tags=extra_tags,
        fail_silently=fail_silently,
    )


def info(request, message, extra_tags="", fail_silently=False):
    
    add_message(
        request,
        constants.INFO,
        message,
        extra_tags=extra_tags,
        fail_silently=fail_silently,
    )


def success(request, message, extra_tags="", fail_silently=False):
    
    add_message(
        request,
        constants.SUCCESS,
        message,
        extra_tags=extra_tags,
        fail_silently=fail_silently,
    )


def warning(request, message, extra_tags="", fail_silently=False):
    
    add_message(
        request,
        constants.WARNING,
        message,
        extra_tags=extra_tags,
        fail_silently=fail_silently,
    )


def error(request, message, extra_tags="", fail_silently=False):
    
    add_message(
        request,
        constants.ERROR,
        message,
        extra_tags=extra_tags,
        fail_silently=fail_silently,
    )