当前位置: 首页>>代码示例>>Python>>正文


Python base.Message方法代码示例

本文整理汇总了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) 
开发者ID:nesdis,项目名称:djongo,代码行数:20,代码来源:base.py

示例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) 
开发者ID:nesdis,项目名称:djongo,代码行数:20,代码来源:base.py

示例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) 
开发者ID:nesdis,项目名称:djongo,代码行数:21,代码来源:test_cookie.py

示例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) 
开发者ID:nesdis,项目名称:djongo,代码行数:22,代码来源:test_cookie.py

示例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 
开发者ID:DavidCain,项目名称:mitoc-trips,代码行数:18,代码来源:__init__.py

示例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) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:11,代码来源:cookie.py

示例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 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:16,代码来源:cookie.py

示例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) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:11,代码来源:cookie.py

示例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 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:16,代码来源:cookie.py

示例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'),
        ]) 
开发者ID:nesdis,项目名称:djongo,代码行数:7,代码来源:base.py

示例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) 
开发者ID:nesdis,项目名称:djongo,代码行数:16,代码来源:test_cookie.py

示例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) 
开发者ID:nesdis,项目名称:djongo,代码行数:11,代码来源:test_session.py

示例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)) 
开发者ID:erigones,项目名称:esdc-ce,代码行数:4,代码来源:utils.py


注:本文中的django.contrib.messages.storage.base.Message方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。