Ticket #14976: messages.patch

File messages.patch, 7.3 KB (added by Ted, 14 years ago)
  • django/contrib/messages/api.py

     
    1313    pass
    1414
    1515
    16 def add_message(request, level, message, extra_tags='', fail_silently=False):
     16def add_message(request, level, message, extra_tags='', fail_silently=False, is_html=False):
    1717    """
    1818    Attempts to add a message to the request using the 'messages' app, falling
    1919    back to the user's message_set if MessageMiddleware hasn't been enabled.
    2020    """
    2121    if hasattr(request, '_messages'):
    22         return request._messages.add(level, message, extra_tags)
     22        return request._messages.add(level, message, extra_tags, is_html)
    2323    if hasattr(request, 'user') and request.user.is_authenticated():
    2424        return request.user.message_set.create(message=message)
    2525    if not fail_silently:
     
    7474    return True
    7575
    7676
    77 def debug(request, message, extra_tags='', fail_silently=False):
     77def debug(request, message, extra_tags='', fail_silently=False, is_html=False):
    7878    """
    7979    Adds a message with the ``DEBUG`` level.
    8080    """
    8181    add_message(request, constants.DEBUG, message, extra_tags=extra_tags,
    82                 fail_silently=fail_silently)
     82                fail_silently=fail_silently, is_html=is_html)
    8383
    8484
    85 def info(request, message, extra_tags='', fail_silently=False):
     85def info(request, message, extra_tags='', fail_silently=False, is_html=False):
    8686    """
    8787    Adds a message with the ``INFO`` level.
    8888    """
    8989    add_message(request, constants.INFO, message, extra_tags=extra_tags,
    90                 fail_silently=fail_silently)
     90                fail_silently=fail_silently, is_html=is_html)
    9191
    9292
    93 def success(request, message, extra_tags='', fail_silently=False):
     93def success(request, message, extra_tags='', fail_silently=False, is_html=False):
    9494    """
    9595    Adds a message with the ``SUCCESS`` level.
    9696    """
    9797    add_message(request, constants.SUCCESS, message, extra_tags=extra_tags,
    98                 fail_silently=fail_silently)
     98                fail_silently=fail_silently, is_html=is_html)
    9999
    100100
    101 def warning(request, message, extra_tags='', fail_silently=False):
     101def warning(request, message, extra_tags='', fail_silently=False, is_html=False):
    102102    """
    103103    Adds a message with the ``WARNING`` level.
    104104    """
    105105    add_message(request, constants.WARNING, message, extra_tags=extra_tags,
    106                 fail_silently=fail_silently)
     106                fail_silently=fail_silently, is_html=is_html)
    107107
    108108
    109 def error(request, message, extra_tags='', fail_silently=False):
     109def error(request, message, extra_tags='', fail_silently=False, is_html=False):
    110110    """
    111111    Adds a message with the ``ERROR`` level.
    112112    """
    113113    add_message(request, constants.ERROR, message, extra_tags=extra_tags,
    114                 fail_silently=fail_silently)
     114                fail_silently=fail_silently, is_html=is_html)
  • django/contrib/messages/storage/base.py

     
    1313    or template.
    1414    """
    1515
    16     def __init__(self, level, message, extra_tags=None):
     16    def __init__(self, level, message, extra_tags=None, is_html=False):
    1717        self.level = int(level)
    1818        self.message = message
    1919        self.extra_tags = extra_tags
     20        self.is_html = is_html
    2021
    2122    def _prepare(self):
    2223        """
     
    137138            messages = self._loaded_messages + self._queued_messages
    138139            return self._store(messages, response)
    139140
    140     def add(self, level, message, extra_tags=''):
     141    def add(self, level, message, extra_tags='', is_html=False):
    141142        """
    142143        Queues a message to be stored.
    143144
     
    152153            return
    153154        # Add the message.
    154155        self.added_new = True
    155         message = Message(level, message, extra_tags=extra_tags)
     156        message = Message(level, message, extra_tags=extra_tags, is_html=is_html)
    156157        self._queued_messages.append(message)
    157158
    158159    def _get_level(self):
  • django/contrib/messages/storage/cookie.py

     
    1515    def default(self, obj):
    1616        if isinstance(obj, Message):
    1717            message = [self.message_key, obj.level, obj.message]
     18            # If no tags, stores a blank string
    1819            if obj.extra_tags:
    1920                message.append(obj.extra_tags)
     21            else:
     22                message.append("")
     23            # Stores is_html flag as a 1 or 0
     24            if obj.is_html:
     25                message.append(1)
     26            else:
     27                message.append(0)
    2028            return message
    2129        return super(MessageEncoder, self).default(obj)
    2230
     
    2937    def process_messages(self, obj):
    3038        if isinstance(obj, list) and obj:
    3139            if obj[0] == MessageEncoder.message_key:
     40                if len(obj) == 5:
     41                    # Legacy messages are len 3 or 4
     42                    # Translates is_html from 1,0 to True,False
     43                    # Translates a blank extra tags from "" to None
     44                    if obj[4] == 1:
     45                        obj[4] = True
     46                    else:
     47                        obj[4] = False
     48                    if obj[3] == "":
     49                        obj[3] = None
    3250                return Message(*obj[1:])
    3351            return [self.process_messages(item) for item in obj]
    3452        if isinstance(obj, dict):
  • django/contrib/messages/storage/session.py

     
    1 from django.contrib.messages.storage.base import BaseStorage
     1from django.contrib.messages.storage.base import BaseStorage, Message
    22
    33
    44class SessionStorage(BaseStorage):
     
    2020        always stores everything it is given, so return True for the
    2121        all_retrieved flag.
    2222        """
    23         return self.request.session.get(self.session_key), True
     23        messages = self.request.session.get(self.session_key)
     24        if messages:  # Either list or none
     25            for message in messages:
     26                # Ensures legacy messages get an is_html property
     27                if isinstance(message, Message) and not hasattr(message, "is_html"):
     28                    message.is_html = False
     29        return messages, True
    2430
    2531    def _store(self, messages, response, *args, **kwargs):
    2632        """
  • django/contrib/messages/tests/base.py

     
    207207        """
    208208        settings.MESSAGE_LEVEL = constants.DEBUG
    209209        data = {
    210             'messages': ['Test message %d' % x for x in xrange(10)],
     210            'messages': ['Test message %d' % x for x in xrange(9)],
    211211        }
    212212        show_url = reverse('django.contrib.messages.tests.urls.show')
    213213        messages = []
Back to Top