當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。