Python django.contrib.messages.constants.INFO Examples
The following are 30
code examples of django.contrib.messages.constants.INFO().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.contrib.messages.constants
, or try the search function
.
Example #1
Source File: background_messages.py From feedsubs with MIT License | 6 votes |
def message_user(user, message, level=constants.INFO): """Send a message to a particular user. A list of messages is stored in the cache to allow having multiple messages queued up for a user. This non-atomic read-modify-write makes it possible to lose messages under concurrency. :param user: User instance :param message: Message to show :param level: Message level """ if user.id is None: raise ValueError('Anonymous users cannot send messages') user_key = _user_key(user) messages = cache.get(user_key) or [] messages.append((message, level)) cache.set(user_key, messages)
Example #2
Source File: form_views.py From django-is-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
def post(self, request, *args, **kwargs): form = self.get_form() fieldsets = self.generate_fieldsets() is_valid = form.is_valid() is_changed = self.is_changed(form) if is_valid and is_changed: return self.form_valid(form) else: if is_valid and not is_changed: return self.form_invalid( form, msg=_('No changes have been submitted.'), msg_level=constants.INFO, fieldsets=fieldsets ) return self.form_invalid(form, fieldsets=fieldsets)
Example #3
Source File: test_cookie.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #4
Source File: test_cookie.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #5
Source File: test_fallback.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_no_fallback(self): """ (1) A short number of messages whose data size doesn't exceed what is allowed in a cookie will all be stored in the CookieBackend. (2) If the CookieBackend can store all messages, the SessionBackend won't be written to at all. """ storage = self.get_storage() response = self.get_response() # Overwrite the _store method of the fallback storage to prove it isn't # used (it would cause a TypeError: 'NoneType' object is not callable). self.get_session_storage(storage)._store = None for i in range(5): storage.add(constants.INFO, str(i) * 100) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 5) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 0)
Example #6
Source File: test_fallback.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_session_fallback(self): """ If the data exceeds what is allowed in a cookie, messages which did not fit are stored in the SessionBackend. """ storage = self.get_storage() response = self.get_response() # see comment in CookieTests.test_cookie_max_length() msg_size = int((CookieStorage.max_cookie_size - 54) / 4.5 - 37) for i in range(5): storage.add(constants.INFO, str(i) * msg_size) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 4) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 1)
Example #7
Source File: test_fallback.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_no_fallback(self): """ (1) A short number of messages whose data size doesn't exceed what is allowed in a cookie will all be stored in the CookieBackend. (2) If the CookieBackend can store all messages, the SessionBackend won't be written to at all. """ storage = self.get_storage() response = self.get_response() # Overwrite the _store method of the fallback storage to prove it isn't # used (it would cause a TypeError: 'NoneType' object is not callable). self.get_session_storage(storage)._store = None for i in range(5): storage.add(constants.INFO, str(i) * 100) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 5) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 0)
Example #8
Source File: test_cookie.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #9
Source File: test_cookie.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_max_cookie_length(self): """ If the data exceeds what is allowed in a cookie, older messages are removed before saving (and returned by the ``update`` method). """ storage = self.get_storage() response = self.get_response() # When storing as a cookie, the cookie has constant overhead of approx # 54 chars, and each message has a constant overhead of about 37 chars # and a variable overhead of zero in the best case. We aim for a message # size which will fit 4 messages into the cookie, but not 5. # See also FallbackTest.test_session_fallback msg_size = int((CookieStorage.max_cookie_size - 54) / 4.5 - 37) for i in range(5): storage.add(constants.INFO, str(i) * msg_size) unstored_messages = storage.update(response) cookie_storing = self.stored_messages_count(storage, response) self.assertEqual(cookie_storing, 4) self.assertEqual(len(unstored_messages), 1) self.assertEqual(unstored_messages[0].message, '0' * msg_size)
Example #10
Source File: test_fallback.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_session_fallback(self): """ If the data exceeds what is allowed in a cookie, messages which did not fit are stored in the SessionBackend. """ storage = self.get_storage() response = self.get_response() # see comment in CookieTests.test_cookie_max_length() msg_size = int((CookieStorage.max_cookie_size - 54) / 4.5 - 37) for i in range(5): storage.add(constants.INFO, str(i) * msg_size) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 4) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 1)
Example #11
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_add_update(self): storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'Test message 1') storage.add(constants.INFO, 'Test message 1', extra_tags='tag') storage.update(response) storing = self.stored_messages_count(storage, response) self.assertEqual(storing, 2)
Example #12
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_default_level(self): # get_level works even with no storage on the request. request = self.get_request() self.assertEqual(get_level(request), constants.INFO) # get_level returns the default level if it hasn't been set. storage = self.get_storage() request._messages = storage self.assertEqual(get_level(request), constants.INFO) # Only messages of sufficient level get recorded. add_level_messages(storage) self.assertEqual(len(storage), 5)
Example #13
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_add_lazy_translation(self): storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, gettext_lazy('lazy message')) storage.update(response) storing = self.stored_messages_count(storage, response) self.assertEqual(storing, 1)
Example #14
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_add(self): storage = self.get_storage() self.assertFalse(storage.added_new) storage.add(constants.INFO, 'Test message 1') self.assertTrue(storage.added_new) storage.add(constants.INFO, 'Test message 2', extra_tags='tag') self.assertEqual(len(storage), 2)
Example #15
Source File: test_fallback.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_session_fallback_only(self): """ Large messages, none of which fit in a cookie, are stored in the SessionBackend (and nothing is stored in the CookieBackend). """ storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'x' * 5000) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 0) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 1)
Example #16
Source File: test_cookie.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_cookie_setings(self): """ CookieStorage honors SESSION_COOKIE_DOMAIN, SESSION_COOKIE_SECURE, and SESSION_COOKIE_HTTPONLY (#15618, #20972). """ # Test before the messages have been consumed storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'test') storage.update(response) self.assertIn('test', response.cookies['messages'].value) self.assertEqual(response.cookies['messages']['domain'], '.example.com') self.assertEqual(response.cookies['messages']['expires'], '') self.assertIs(response.cookies['messages']['secure'], True) self.assertIs(response.cookies['messages']['httponly'], True) self.assertEqual(response.cookies['messages']['samesite'], 'Strict') # Test deletion of the cookie (storing with an empty value) after the messages have been consumed storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'test') for m in storage: pass # Iterate through the storage to simulate consumption of messages. storage.update(response) self.assertEqual(response.cookies['messages'].value, '') self.assertEqual(response.cookies['messages']['domain'], '.example.com') self.assertEqual(response.cookies['messages']['expires'], 'Thu, 01 Jan 1970 00:00:00 GMT')
Example #17
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_tags(self): storage = self.get_storage() storage.level = 0 add_level_messages(storage) storage.add(constants.INFO, 'A generic info message', extra_tags=None) tags = [msg.tags for msg in storage] self.assertEqual(tags, ['info', '', 'extra-tag debug', 'warning', 'error', 'success', 'info'])
Example #18
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_default_level(self): # get_level works even with no storage on the request. request = self.get_request() self.assertEqual(get_level(request), constants.INFO) # get_level returns the default level if it hasn't been set. storage = self.get_storage() request._messages = storage self.assertEqual(get_level(request), constants.INFO) # Only messages of sufficient level get recorded. add_level_messages(storage) self.assertEqual(len(storage), 5)
Example #19
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_existing_add_read_update(self): storage = self.get_existing_storage() response = self.get_response() storage.add(constants.INFO, 'Test message 3') list(storage) # Simulates a read storage.update(response) storing = self.stored_messages_count(storage, response) self.assertEqual(storing, 0)
Example #20
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_existing_read_add_update(self): storage = self.get_existing_storage() response = self.get_response() list(storage) # Simulates a read storage.add(constants.INFO, 'Test message 3') storage.update(response) storing = self.stored_messages_count(storage, response) self.assertEqual(storing, 1)
Example #21
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_existing_add(self): storage = self.get_existing_storage() self.assertFalse(storage.added_new) storage.add(constants.INFO, 'Test message 3') self.assertTrue(storage.added_new)
Example #22
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_existing_add(self): storage = self.get_existing_storage() self.assertFalse(storage.added_new) storage.add(constants.INFO, 'Test message 3') self.assertTrue(storage.added_new)
Example #23
Source File: base.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_tags(self): storage = self.get_storage() storage.level = 0 add_level_messages(storage) storage.add(constants.INFO, 'A generic info message', extra_tags=None) tags = [msg.tags for msg in storage] self.assertEqual(tags, ['info', '', 'extra-tag debug', 'warning', 'error', 'success', 'info'])
Example #24
Source File: test_cookie.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_cookie_setings(self): """ CookieStorage honors SESSION_COOKIE_DOMAIN, SESSION_COOKIE_SECURE, and SESSION_COOKIE_HTTPONLY (#15618, #20972). """ # Test before the messages have been consumed storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'test') storage.update(response) self.assertIn('test', response.cookies['messages'].value) self.assertEqual(response.cookies['messages']['domain'], '.example.com') self.assertEqual(response.cookies['messages']['expires'], '') self.assertIs(response.cookies['messages']['secure'], True) self.assertIs(response.cookies['messages']['httponly'], True) self.assertEqual(response.cookies['messages']['samesite'], 'Strict') # Test deletion of the cookie (storing with an empty value) after the messages have been consumed storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'test') for m in storage: pass # Iterate through the storage to simulate consumption of messages. storage.update(response) self.assertEqual(response.cookies['messages'].value, '') self.assertEqual(response.cookies['messages']['domain'], '.example.com') self.assertEqual(response.cookies['messages']['expires'], 'Thu, 01 Jan 1970 00:00:00 GMT')
Example #25
Source File: test_fallback.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_session_fallback_only(self): """ Large messages, none of which fit in a cookie, are stored in the SessionBackend (and nothing is stored in the CookieBackend). """ storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'x' * 5000) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 0) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 1)
Example #26
Source File: utils.py From esdc-ce with Apache License 2.0 | 5 votes |
def info(self, msg): self.add_message(constants.INFO, msg)
Example #27
Source File: messages.py From avos with Apache License 2.0 | 5 votes |
def info(request, message, extra_tags='', fail_silently=False): """Adds a message with the ``INFO`` level.""" add_message(request, constants.INFO, message, extra_tags=extra_tags, fail_silently=fail_silently)
Example #28
Source File: base_tags.py From jorvik with GNU General Public License v3.0 | 5 votes |
def level_to_bootstrap(message): map = { constants.DEBUG: 'alert-info', constants.INFO: 'alert-info', constants.SUCCESS: 'alert-success', constants.WARNING: 'alert-warning', constants.ERROR: 'alert-danger', } return map.get(message.level, 'alert-info')
Example #29
Source File: form_views.py From django-is-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
def post(self, request, *args, **kwargs): form = self.get_form() fieldsets = self.generate_fieldsets_with_inline_view_instances(form.instance) inline_views = get_inline_views_from_fieldsets(fieldsets) inline_form_views = self._filter_inline_form_views(inline_views) inline_forms_is_valid = True for inline_form_view in inline_form_views: inline_forms_is_valid = inline_form_view.is_valid() and inline_forms_is_valid is_valid = form.is_valid() is_changed = self.is_changed(form, inline_form_views=inline_form_views) if is_valid and inline_forms_is_valid and is_changed: return self.form_valid(form, inline_form_views=inline_form_views, inline_views=inline_views, fieldsets=fieldsets) else: if is_valid and not is_changed: return self.form_invalid( form, inline_form_views=inline_form_views, inline_views=inline_views, msg=_('No changes have been submitted.'), msg_level=constants.INFO, fieldsets=fieldsets ) return self.form_invalid( form, inline_form_views=inline_form_views, inline_views=inline_views, fieldsets=fieldsets )
Example #30
Source File: background_messages.py From feedsubs with MIT License | 5 votes |
def info(user, message): """ Adds a message with the ``INFO`` level. :param user: User instance :param message: Message to show """ message_user(user, message, constants.INFO)