Ticket #14976: messages.patch
File messages.patch, 7.3 KB (added by , 14 years ago) |
---|
-
django/contrib/messages/api.py
13 13 pass 14 14 15 15 16 def add_message(request, level, message, extra_tags='', fail_silently=False ):16 def add_message(request, level, message, extra_tags='', fail_silently=False, is_html=False): 17 17 """ 18 18 Attempts to add a message to the request using the 'messages' app, falling 19 19 back to the user's message_set if MessageMiddleware hasn't been enabled. 20 20 """ 21 21 if hasattr(request, '_messages'): 22 return request._messages.add(level, message, extra_tags )22 return request._messages.add(level, message, extra_tags, is_html) 23 23 if hasattr(request, 'user') and request.user.is_authenticated(): 24 24 return request.user.message_set.create(message=message) 25 25 if not fail_silently: … … 74 74 return True 75 75 76 76 77 def debug(request, message, extra_tags='', fail_silently=False ):77 def debug(request, message, extra_tags='', fail_silently=False, is_html=False): 78 78 """ 79 79 Adds a message with the ``DEBUG`` level. 80 80 """ 81 81 add_message(request, constants.DEBUG, message, extra_tags=extra_tags, 82 fail_silently=fail_silently )82 fail_silently=fail_silently, is_html=is_html) 83 83 84 84 85 def info(request, message, extra_tags='', fail_silently=False ):85 def info(request, message, extra_tags='', fail_silently=False, is_html=False): 86 86 """ 87 87 Adds a message with the ``INFO`` level. 88 88 """ 89 89 add_message(request, constants.INFO, message, extra_tags=extra_tags, 90 fail_silently=fail_silently )90 fail_silently=fail_silently, is_html=is_html) 91 91 92 92 93 def success(request, message, extra_tags='', fail_silently=False ):93 def success(request, message, extra_tags='', fail_silently=False, is_html=False): 94 94 """ 95 95 Adds a message with the ``SUCCESS`` level. 96 96 """ 97 97 add_message(request, constants.SUCCESS, message, extra_tags=extra_tags, 98 fail_silently=fail_silently )98 fail_silently=fail_silently, is_html=is_html) 99 99 100 100 101 def warning(request, message, extra_tags='', fail_silently=False ):101 def warning(request, message, extra_tags='', fail_silently=False, is_html=False): 102 102 """ 103 103 Adds a message with the ``WARNING`` level. 104 104 """ 105 105 add_message(request, constants.WARNING, message, extra_tags=extra_tags, 106 fail_silently=fail_silently )106 fail_silently=fail_silently, is_html=is_html) 107 107 108 108 109 def error(request, message, extra_tags='', fail_silently=False ):109 def error(request, message, extra_tags='', fail_silently=False, is_html=False): 110 110 """ 111 111 Adds a message with the ``ERROR`` level. 112 112 """ 113 113 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
13 13 or template. 14 14 """ 15 15 16 def __init__(self, level, message, extra_tags=None ):16 def __init__(self, level, message, extra_tags=None, is_html=False): 17 17 self.level = int(level) 18 18 self.message = message 19 19 self.extra_tags = extra_tags 20 self.is_html = is_html 20 21 21 22 def _prepare(self): 22 23 """ … … 137 138 messages = self._loaded_messages + self._queued_messages 138 139 return self._store(messages, response) 139 140 140 def add(self, level, message, extra_tags='' ):141 def add(self, level, message, extra_tags='', is_html=False): 141 142 """ 142 143 Queues a message to be stored. 143 144 … … 152 153 return 153 154 # Add the message. 154 155 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) 156 157 self._queued_messages.append(message) 157 158 158 159 def _get_level(self): -
django/contrib/messages/storage/cookie.py
15 15 def default(self, obj): 16 16 if isinstance(obj, Message): 17 17 message = [self.message_key, obj.level, obj.message] 18 # If no tags, stores a blank string 18 19 if obj.extra_tags: 19 20 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) 20 28 return message 21 29 return super(MessageEncoder, self).default(obj) 22 30 … … 29 37 def process_messages(self, obj): 30 38 if isinstance(obj, list) and obj: 31 39 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 32 50 return Message(*obj[1:]) 33 51 return [self.process_messages(item) for item in obj] 34 52 if isinstance(obj, dict): -
django/contrib/messages/storage/session.py
1 from django.contrib.messages.storage.base import BaseStorage 1 from django.contrib.messages.storage.base import BaseStorage, Message 2 2 3 3 4 4 class SessionStorage(BaseStorage): … … 20 20 always stores everything it is given, so return True for the 21 21 all_retrieved flag. 22 22 """ 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 24 30 25 31 def _store(self, messages, response, *args, **kwargs): 26 32 """ -
django/contrib/messages/tests/base.py
207 207 """ 208 208 settings.MESSAGE_LEVEL = constants.DEBUG 209 209 data = { 210 'messages': ['Test message %d' % x for x in xrange( 10)],210 'messages': ['Test message %d' % x for x in xrange(9)], 211 211 } 212 212 show_url = reverse('django.contrib.messages.tests.urls.show') 213 213 messages = []