Skip to content
Browse files

Add functions to mimic the django messages API calls

To avoid importing and using the constants from django.contrib.messages,
add functions to mimic the way we can easily send messages in the
conrtib package::

   >>> messages.success(barry, "Barry, your report is ready")

(same with debug/info/warning/error)
  • Loading branch information...
1 parent edfb951 commit ed5f587252e6ec913c0a88f9e0191001067e8519 @twidi twidi committed
Showing with 117 additions and 12 deletions.
  1. +10 −0 README.rst
  2. +56 −0 async_messages/messages.py
  3. +51 −12 tests/tests.py
View
10 README.rst
@@ -108,6 +108,16 @@ Send multiple messages to a single user::
>>> # do more awesome stuff
>>> message_user(barry, "Barry, your report is ready")
+Alternative way to send a message to a single user, imitating the django.contrib.messages API::
+
+ >>> from async_messages import messages
+ >>> barry = User.objects.get(username='barry')
+ >>> messages.debug(barry, "Barry was here")
+ >>> messages.info(barry, "Hi, Barry")
+ >>> messages.success(barry, "Barry, your report is ready")
+ >>> messages.warning(barry, "Barry, you didn't lock your session")
+ >>> messages.error(barry, "You are not Barry")
+
Contributing
============
View
56 async_messages/messages.py
@@ -0,0 +1,56 @@
+from django.contrib.messages import constants
+from . import message_user
+
+"""
+Mimic the django.contrib.messages API
+"""
+
+
+def debug(user, message):
+ """
+ Adds a message with the ``DEBUG`` level.
+
+ :param user: User instance
+ :param message: Message to show
+ """
+ message_user(user, message, constants.DEBUG)
+
+
+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)
+
+
+def success(user, message):
+ """
+ Adds a message with the ``SUCCESS`` level.
+
+ :param user: User instance
+ :param message: Message to show
+ """
+ message_user(user, message, constants.SUCCESS)
+
+
+def warning(user, message):
+ """
+ Adds a message with the ``WARNING`` level.
+
+ :param user: User instance
+ :param message: Message to show
+ """
+ message_user(user, message, constants.WARNING)
+
+
+def error(user, message):
+ """
+ Adds a message with the ``ERROR`` level.
+
+ :param user: User instance
+ :param message: Message to show
+ """
+ message_user(user, message, constants.ERROR)
View
63 tests/tests.py
@@ -1,8 +1,9 @@
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
+from django.contrib.messages import constants, set_level
-from async_messages import message_user, message_users
+from async_messages import message_user, message_users, messages
class MiddlewareTests(TestCase):
@@ -16,29 +17,67 @@ def setUp(self):
def test_message_appears_for_user(self):
message_user(self.user, "Hello")
response = self.client.get('/')
- messages = list(response.context['messages'])
- self.assertEqual(1, len(messages))
- self.assertEqual('Hello', str(messages[0]))
+ msgs = list(response.context['messages'])
+ self.assertEqual(1, len((msgs)))
+ self.assertEqual('Hello', str((msgs)[0]))
def test_message_appears_all_users(self):
message_users(User.objects.all(), "Hello")
response = self.client.get('/')
- messages = list(response.context['messages'])
- self.assertEqual(1, len(messages))
- self.assertEqual('Hello', str(messages[0]))
+ msgs = list(response.context['messages'])
+ self.assertEqual(1, len((msgs)))
+ self.assertEqual('Hello', str((msgs)[0]))
def test_message_queue(self):
message_user(self.user, "First Message")
message_user(self.user, "Second Message")
response = self.client.get('/')
- messages = list(response.context['messages'])
- self.assertEqual(2, len(messages))
- self.assertEqual('Second Message', str(messages[1]))
+ msgs = list(response.context['messages'])
+ self.assertEqual(2, len((msgs)))
+ self.assertEqual('Second Message', str((msgs)[1]))
class AnonynousUserTests(TestCase):
def test_anonymous(self):
client = Client()
response = client.get('/')
- messages = list(response.context['messages'])
- self.assertEqual(0, len(messages))
+ msgs = list(response.context['messages'])
+ self.assertEqual(0, len((msgs)))
+
+
+class TestMessagesApi(TestCase):
+ def setUp(self):
+ username, password = 'david', 'password'
+ self.user = User.objects.create_user(username, "django-async@test.com", password)
+ self.client = Client()
+ self.client.login(username=username, password=password)
+
+ def assertMessageOk(self, level):
+ response = self.client.get('/')
+ msgs = list(response.context['messages'])
+ self.assertEqual(1, len((msgs)))
+ self.assertEqual('Hello', str((msgs)[0]))
+
+ def test_info(self):
+ messages.info(self.user, "Hello")
+ self.assertMessageOk(constants.INFO)
+
+ def test_success(self):
+ messages.success(self.user, "Hello")
+ self.assertMessageOk(constants.SUCCESS)
+
+ def test_warning(self):
+ messages.warning(self.user, "Hello")
+ self.assertMessageOk(constants.WARNING)
+
+ def test_error(self):
+ messages.error(self.user, "Hello")
+ self.assertMessageOk(constants.ERROR)
+
+ def test_debug(self):
+ messages.debug(self.user, "Hello")
+ # 0 messages because by default django.contrib.messages ignore DEBUG
+ # messages (this can be changed using set_level)
+ response = self.client.get('/')
+ msgs = list(response.context['messages'])
+ self.assertEqual(0, len((msgs)))

0 comments on commit ed5f587

Please sign in to comment.
Something went wrong with that request. Please try again.