Source code for superdjango.views.messages

"""

|alpha|

Messages provide feedback to users as things happen within an application. The ``MessageMixin`` enables this feedback
for a view.

"""
__author__ = "Shawn Davis <shawn@superdjango.com>"
__maintainer__ = "Shawn Davis <shawn@superdjango.com>"
__version__ = "1.2.0-a"

# Imports

from django.contrib import messages

# Exports

__all__ = (
    "MessageAPIBinder",
    "MessageAPIWrapper",
    "MessageMixin",
)

# Classes


[docs]class MessageAPIWrapper(object): """Wraps the ``django.contrib.messages.api`` module to capture the current request instance for function calls."""
[docs] def __init__(self, request): """Initialize the wrapper. :param request: The current HTTPRequest instance. """ self.request = request
def debug(self, message): messages.debug(self.request, message) def error(self, message): messages.error(self.request, message) def info(self, message): messages.info(self.request, message) def success(self, message): messages.success(self.request, message) def warning(self, message): # noinspection PyUnresolvedReferences messages.warning(self.request, message)
[docs]class MessageAPIBinder(object): """Binds the ``MessageAPIWrapper`` to a view.""" def __get__(self, instance, owner): return MessageAPIWrapper(instance.request)
# Mixins
[docs]class MessageMixin(object): """Add a ``messages``` attribute on the view instance that wraps ``django.contrib.messages``. Many HTML/CSS frameworks such as Twitter Bootstrap or UIKit have styles for ``info``, ``success``, and ``warning``, but use ``danger`` for the equivalent of ``error``. You can address this by adding the following to your ``settings.py`` file: .. code-block:: python from django.contrib import messages # Map "error" to "danger" for Bootstrap/UIKit alerts. from django.contrib.messages import constants as messages MESSAGE_TAGS = { messages.ERROR: "danger", } See https://docs.djangoproject.com/en/stable/ref/contrib/messages/ """ messages = MessageAPIBinder()