Example #1
0
def add_message(request,
                level,
                message,
                extra_tags='',
                fail_silently=False,
                buttons=None):
    """
   Attempts to add a message to the request using the 'messages' app.
   """
    if not isinstance(request, HttpRequest):
        raise TypeError(
            "add_message() argument must be an HttpRequest object, "
            "not '%s'." % request.__class__.__name__)
    if hasattr(request, '_messages'):
        if buttons:
            b = ''.join([
                '<td><a class="btn btn-{style}" href="{href}">{text}</a></td>'.
                format(text=text, style=style, href=href)
                for text, style, href in buttons
            ])
        else:
            b = ''
        return request._messages.add(
            level,
            mark_safe(('<table style="width: 95%;">'
                       '<tr>'
                       '<td>{msg}</td>'
                       '<td></td>'
                       '{buttons}'
                       '</tr>'
                       '</table>').format(msg=message, buttons=b)))
    if not fail_silently:
        raise MessageFailure(
            'You cannot add messages without installing '
            'django.contrib.messages.middleware.MessageMiddleware')
Example #2
0
def add_message(request, level, message, extra_tags='', fail_silently=False, *args, **kwargs):
    """
    Attempts to add a message to the request using the 'messages' app.
    """
    if hasattr(request, '_messages'):
        return request._messages.add(level, message, extra_tags, *args, **kwargs)
    if not fail_silently:
        raise MessageFailure('You cannot add messages without installing '
                             'django.contrib.messages.middleware.MessageMiddleware')
Example #3
0
def add_message(request, level, message, extra_tags='', fail_silently=False, **kwargs):
    """
    Attempts to add a message to the request using the 'messages' app, falling
    back to the user's message_set if MessageMiddleware hasn't been enabled.
    """
    if hasattr(request, 'user') and request.user.is_authenticated():
        # can pass in content_object and meta now
        return create_offline_message(request.user, message, level, **kwargs)
    if hasattr(request, '_messages'):
        return request._messages.add(level, message, extra_tags)
    if not fail_silently:
        raise MessageFailure(   'Without the django.contrib.messages '
                                'middleware, messages can only be added to '
                                'authenticated users.')