Refs #27795 -- Replaced force_text() with str() in contrib.messages

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2017-05-10 22:39:08 +02:00
parent d1e5516c06
commit f87256b15e
1 changed files with 5 additions and 16 deletions

View File

@ -1,6 +1,5 @@
from django.conf import settings from django.conf import settings
from django.contrib.messages import constants, utils from django.contrib.messages import constants, utils
from django.utils.encoding import force_text
LEVEL_TAGS = utils.get_level_tags() LEVEL_TAGS = utils.get_level_tags()
@ -21,34 +20,24 @@ class Message:
""" """
Prepare the message for serialization by forcing the ``message`` Prepare the message for serialization by forcing the ``message``
and ``extra_tags`` to str in case they are lazy translations. and ``extra_tags`` to str in case they are lazy translations.
Known "safe" types (None, int, etc.) are not converted (see Django's
``force_text`` implementation for details).
""" """
self.message = force_text(self.message, strings_only=True) self.message = str(self.message)
self.extra_tags = force_text(self.extra_tags, strings_only=True) self.extra_tags = str(self.extra_tags) if self.extra_tags is not None else None
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, Message) and self.level == other.level and \ return isinstance(other, Message) and self.level == other.level and \
self.message == other.message self.message == other.message
def __str__(self): def __str__(self):
return force_text(self.message) return str(self.message)
@property @property
def tags(self): def tags(self):
extra_tags = force_text(self.extra_tags, strings_only=True) return ' '.join(tag for tag in [self.extra_tags, self.level_tag] if tag)
if extra_tags and self.level_tag:
return ' '.join([extra_tags, self.level_tag])
elif extra_tags:
return extra_tags
elif self.level_tag:
return self.level_tag
return ''
@property @property
def level_tag(self): def level_tag(self):
return force_text(LEVEL_TAGS.get(self.level, ''), strings_only=True) return LEVEL_TAGS.get(self.level, '')
class BaseStorage: class BaseStorage: