本文整理汇总了Python中django.contrib.messages.storage.base.Message方法的典型用法代码示例。如果您正苦于以下问题:Python base.Message方法的具体用法?Python base.Message怎么用?Python base.Message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.messages.storage.base
的用法示例。
在下文中一共展示了base.Message方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_full_request_response_cycle
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def test_full_request_response_cycle(self):
"""
With the message middleware enabled, messages are properly stored and
retrieved across the full request/redirect/response cycle.
"""
data = {
'messages': ['Test message %d' % x for x in range(5)],
}
show_url = reverse('show_message')
for level in ('debug', 'info', 'success', 'warning', 'error'):
add_url = reverse('add_message', args=(level,))
response = self.client.post(add_url, data, follow=True)
self.assertRedirects(response, show_url)
self.assertIn('messages', response.context)
messages = [Message(self.levels[level], msg) for msg in data['messages']]
self.assertEqual(list(response.context['messages']), messages)
for msg in data['messages']:
self.assertContains(response, msg)
示例2: test_multiple_posts
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def test_multiple_posts(self):
"""
Messages persist properly when multiple POSTs are made before a GET.
"""
data = {
'messages': ['Test message %d' % x for x in range(5)],
}
show_url = reverse('show_message')
messages = []
for level in ('debug', 'info', 'success', 'warning', 'error'):
messages.extend(Message(self.levels[level], msg) for msg in data['messages'])
add_url = reverse('add_message', args=(level,))
self.client.post(add_url, data)
response = self.client.get(show_url)
self.assertIn('messages', response.context)
self.assertEqual(list(response.context['messages']), messages)
for msg in data['messages']:
self.assertContains(response, msg)
示例3: test_json_encoder_decoder
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def test_json_encoder_decoder(self):
"""
A complex nested data structure containing Message
instances is properly encoded/decoded by the custom JSON
encoder/decoder classes.
"""
messages = [
{
'message': Message(constants.INFO, 'Test message'),
'message_list': [
Message(constants.INFO, 'message %s') for x in range(5)
] + [{'another-message': Message(constants.ERROR, 'error')}],
},
Message(constants.INFO, 'message %s'),
]
encoder = MessageEncoder(separators=(',', ':'))
value = encoder.encode(messages)
decoded_messages = json.loads(value, cls=MessageDecoder)
self.assertEqual(messages, decoded_messages)
示例4: test_pre_1_5_message_format
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def test_pre_1_5_message_format(self):
"""
Messages that were set in the cookie before the addition of is_safedata
are decoded correctly (#22426).
"""
# Encode the messages using the current encoder.
messages = [Message(constants.INFO, 'message %s') for x in range(5)]
encoder = MessageEncoder(separators=(',', ':'))
encoded_messages = encoder.encode(messages)
# Remove the is_safedata flag from the messages in order to imitate
# the behavior of before 1.5 (monkey patching).
encoded_messages = json.loads(encoded_messages)
for obj in encoded_messages:
obj.pop(1)
encoded_messages = json.dumps(encoded_messages, separators=(',', ':'))
# Decode the messages in the old format (without is_safedata)
decoded_messages = json.loads(encoded_messages, cls=MessageDecoder)
self.assertEqual(messages, decoded_messages)
示例5: add_unique_message
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def add_unique_message(self, level, message, **kwargs):
""" Add a message, but only after first making sure it's not already been emitted.
This helps guard against any message generator adding the same message
multiple times before the next time the messages are displayed. Once messages
are displayed, `used` is set on the storage object returned by
`get_messages()` and the queue is cleared.
Returns True if message was sent for the first time.
"""
expected = Message(message=message, level=level, **kwargs)
if any(msg == expected for msg in messages.get_messages(self.request)):
return False
messages.add_message(self.request, level, message, **kwargs)
return True
示例6: default
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def default(self, obj):
if isinstance(obj, Message):
# Using 0/1 here instead of False/True to produce more compact json
is_safedata = 1 if isinstance(obj.message, SafeData) else 0
message = [self.message_key, is_safedata, obj.level, obj.message]
if obj.extra_tags:
message.append(obj.extra_tags)
return message
return super(MessageEncoder, self).default(obj)
示例7: process_messages
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def process_messages(self, obj):
if isinstance(obj, list) and obj:
if obj[0] == MessageEncoder.message_key:
if len(obj) == 3:
# Compatibility with previously-encoded messages
return Message(*obj[1:])
if obj[1]:
obj[3] = mark_safe(obj[3])
return Message(*obj[2:])
return [self.process_messages(item) for item in obj]
if isinstance(obj, dict):
return {key: self.process_messages(value)
for key, value in six.iteritems(obj)}
return obj
示例8: default
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def default(self, obj):
if isinstance(obj, Message):
# Using 0/1 here instead of False/True to produce more compact json
is_safedata = 1 if isinstance(obj.message, SafeData) else 0
message = [self.message_key, is_safedata, obj.level, obj.message]
if obj.extra_tags:
message.append(obj.extra_tags)
return message
return super().default(obj)
示例9: process_messages
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def process_messages(self, obj):
if isinstance(obj, list) and obj:
if obj[0] == MessageEncoder.message_key:
if len(obj) == 3:
# Compatibility with previously-encoded messages
return Message(*obj[1:])
if obj[1]:
obj[3] = mark_safe(obj[3])
return Message(*obj[2:])
return [self.process_messages(item) for item in obj]
if isinstance(obj, dict):
return {key: self.process_messages(value)
for key, value in obj.items()}
return obj
示例10: get_existing_storage
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def get_existing_storage(self):
return self.get_storage([
Message(constants.INFO, 'Test message 1'),
Message(constants.INFO, 'Test message 2', extra_tags='tag'),
])
示例11: test_safedata
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def test_safedata(self):
"""
A message containing SafeData is keeping its safe status when
retrieved from the message storage.
"""
def encode_decode(data):
message = Message(constants.DEBUG, data)
encoded = storage._encode(message)
decoded = storage._decode(encoded)
return decoded.message
storage = self.get_storage()
self.assertIsInstance(encode_decode(mark_safe("<b>Hello Django!</b>")), SafeData)
self.assertNotIsInstance(encode_decode("<b>Hello Django!</b>"), SafeData)
示例12: test_safedata
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def test_safedata(self):
"""
A message containing SafeData keeps its safe status when retrieved from
the message storage.
"""
storage = self.get_storage()
message = Message(constants.DEBUG, mark_safe("<b>Hello Django!</b>"))
set_session_data(storage, [message])
self.assertIsInstance(list(storage)[0].message, SafeData)
示例13: add_message
# 需要导入模块: from django.contrib.messages.storage import base [as 别名]
# 或者: from django.contrib.messages.storage.base import Message [as 别名]
def add_message(self, lvl, msg):
self.append(Message(lvl, msg))