def test_add_message_for(self): now = timezone.now() + timezone.timedelta(days=-1) url = 'http://example.com/error' user2 = get_user_model().objects.create_user("another_user", "*****@*****.**", "123456") add_message_for([user2, self.user], stored_messages.STORED_ERROR, 'Multiple errors', 'extra', now, url) self.assertEqual(Inbox.objects.count(), 2) self.assertEqual(MessageArchive.objects.count(), 2) self.assertEqual(Inbox.objects.get(user=user2.id).message.tags, 'extra') self.assertEqual(Inbox.objects.get(user=self.user).message.tags, 'extra') self.assertEqual(Inbox.objects.get(user=user2.id).message.date, now) self.assertEqual(Inbox.objects.get(user=self.user).message.date, now) self.assertEqual(Inbox.objects.get(user=user2.id).message.url, url) self.assertEqual(Inbox.objects.get(user=self.user).message.url, url) self.assertEqual(Inbox.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual(Inbox.objects.get(user=self.user).message.message, "Multiple errors") self.assertEqual(MessageArchive.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual(MessageArchive.objects.get(user=self.user).message.message, "Multiple errors")
def test_mark_all_read(self): for i in range(20): stored_messages.add_message_for([self.user], stored_messages.INFO, "unicode message ❤") inbox = Inbox.objects.filter(user=self.user) self.assertEqual(len(inbox), 20) mark_all_read(self.user) self.assertEqual(Inbox.objects.filter(user=self.user).count(), 0)
def test_add_message_for(self): now = timezone.now() + timezone.timedelta(days=-1) url = 'http://example.com/error' user2 = get_user_model().objects.create_user("another_user", "*****@*****.**", "123456") add_message_for([user2, self.user], stored_messages.STORED_ERROR, 'Multiple errors', 'extra', now, url) self.assertEqual(Inbox.objects.count(), 2) self.assertEqual(MessageArchive.objects.count(), 2) self.assertEqual( Inbox.objects.get(user=user2.id).message.tags, 'extra') self.assertEqual( Inbox.objects.get(user=self.user).message.tags, 'extra') self.assertEqual(Inbox.objects.get(user=user2.id).message.date, now) self.assertEqual(Inbox.objects.get(user=self.user).message.date, now) self.assertEqual(Inbox.objects.get(user=user2.id).message.url, url) self.assertEqual(Inbox.objects.get(user=self.user).message.url, url) self.assertEqual( Inbox.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual( Inbox.objects.get(user=self.user).message.message, "Multiple errors") self.assertEqual( MessageArchive.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual( MessageArchive.objects.get(user=self.user).message.message, "Multiple errors")
def test_stored_messages_archive_empty_for_unauthenticated_user(self): stored_messages.add_message_for([self.user], stored_messages.INFO, "unicode message ❤") t = Template("{% load stored_messages_tags %}" "{% stored_messages_archive 15 %}") render = t.render(RequestContext(self.factory.get("/"))) self.assertInHTML("<li>[test_user] unicode message ❤</li>", render, 0)
def test_add_message_for(self): user2 = get_user_model().objects.create_user("another_user", "*****@*****.**", "123456") add_message_for([user2, self.user], stored_messages.STORED_ERROR, "Multiple errors") self.assertEqual(Inbox.objects.count(), 2) self.assertEqual(MessageArchive.objects.count(), 2) self.assertEqual(Inbox.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual(Inbox.objects.get(user=self.user).message.message, "Multiple errors") self.assertEqual(MessageArchive.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual(MessageArchive.objects.get(user=self.user).message.message, "Multiple errors")
def test_add_message_for(self): user2 = get_user_model().objects.create_user("another_user", "*****@*****.**", "123456") add_message_for([user2, self.user], stored_messages.STORED_ERROR, 'Multiple errors') self.assertEqual(Inbox.objects.count(), 2) self.assertEqual(MessageArchive.objects.count(), 2) self.assertEqual(Inbox.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual(Inbox.objects.get(user=self.user).message.message, "Multiple errors") self.assertEqual(MessageArchive.objects.get(user=user2.id).message.message, "Multiple errors") self.assertEqual(MessageArchive.objects.get(user=self.user).message.message, "Multiple errors")
def send_to_staff(messages, level=None, tags=''): if not messages: return level = level or stored_messages.STORED_ERROR if isinstance(messages, six.string_types): messages = [messages] users = get_user_model().objects.filter(is_staff=True) for message in messages: rendered = render_to_string("bazaar/message.html", {"message": message}) stored_messages.add_message_for(users, level, rendered, tags)
def send_to_staff(messages, level=None): if not messages: return level = level or stored_messages.STORED_ERROR if isinstance(messages, six.string_types): messages = [messages] users = get_user_model().objects.filter(is_staff=True) for message in messages: rendered = render_to_string("bazaar/message.html", {"message": message}) stored_messages.add_message_for(users, level, rendered)
def _create_messages(self): # create a message for i in range(20): stored_messages.add_message_for([self.user], stored_messages.INFO, "unicode message ❤")
def _notifications_send(verb_slug, actor, target, action_object, message, comment, item, reviewdocument, send_to_all=False): """ Send persistent messages (notifications) for this user github notifications style stored_messages.STORED_DEBUG, stored_messages.STORED_INFO, stored_messages.STORED_SUCCESS, stored_messages.STORED_WARNING, stored_messages.STORED_ERROR update the user.profile.has_notifications """ if verb_slug in NOTIFICATIONS_WHITELIST: # catch when we have no stored message if stored_messages is None: logger.critical('django-stored-messages is not installed') return None if message: from toolkit.api.serializers.user import LiteUserSerializer query_set = target.participants.select_related('profile') # # If we are not sending this message to all participants then exclude the originator # if send_to_all is False: query_set = query_set.exclude(id=actor.pk) # Because we cant mixn the ApiMixin class ot the django User Object actor = LiteUserSerializer(actor, context={'request': None}).data if hasattr(action_object, 'api_serializer') is True: action_object = action_object.api_serializer(action_object, context={'request': None}).data if hasattr(item, 'api_serializer') is True: item = item.api_serializer(item, context={'request': None}).data if hasattr(reviewdocument, 'api_serializer') is True: reviewdocument = reviewdocument.api_serializer(item, context={'request': None}).data target_class_name = target.__class__.__name__ if target_class_name == 'Workspace': target = target.api_serializer(target, context={'request': None}).data else: raise Exception('Unknown target_class_name: %s' % target_class_name) stored_messages.add_message_for(users=query_set.all(), level=stored_messages.STORED_INFO, message=message, extra_tags='', fail_silently=False, verb_slug=verb_slug, actor=actor, action_object=action_object, target=target, comment=comment, item=item, reviewdocument=reviewdocument) # # @TODO move into manager? # for u in query_set.exclude(profile__has_notifications=True): profile = u.profile profile.has_notifications = True profile.save(update_fields=['has_notifications']) # # Send pusher notification # for u in query_set.values('username'): run_task(youve_got_notifications, username=u.get('username'), event='notifications.new')