def mentions(sender, **kwargs): """ Intended to be used as a receiver function for a `post_save` signal on models that have @mentions Implementing model must have an attribute TAG_FIELD where @mentions are stored in raw form This function creates notifications but does not associate mentioned users with the created model instance """ try: from yak.rest_notifications.models import create_notification except ImportError: return if kwargs['created']: # Get the text of the field that holds tags. If there is no field specified, use an empty string. If the field's # value is None, use an empty string. message = getattr(kwargs['instance'], sender.TAG_FIELD, '') or '' content_object = getattr(kwargs['instance'], 'content_object', kwargs['instance']) for user in re.findall(r"@[a-zA-Z0-9_.]+", message): User = get_user_model() try: receiver = User.objects.get(username=user[1:]) mention_type = NotificationType.objects.get(slug="mention") create_notification(receiver, kwargs['instance'].user, content_object, mention_type) # Note that for a Comment, this means the notification is associated with the object commented on, # not the comment itself except User.DoesNotExist: pass
def test_create_notification(self): notification_count = Notification.objects.count() # If receiver is the same as the reporter, a notification is not created create_notification(self.receiver, self.receiver, self.social_obj, self.notification_type) self.assertEqual(notification_count, Notification.objects.count()) # If the receiver and reporter are different, a notification is created create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) self.assertEqual(notification_count + 1, Notification.objects.count())
def test_content_object_serialization(self): """ Content object is serialized using the standard serialization for that object type The `content_objectt` key is replaced for each different type of object serialized """ article = Article.objects.create(title="Cool article") user = UserFactory() create_notification(user, self.reporter, self.social_obj, self.notification_type) create_notification(user, self.receiver, article, self.notification_type) url = reverse("notifications") response = self.assertSchemaGet(url, None, "$notificationResponse", user) self.assertEqual(response.data["count"], 2) self.assertIn("article", response.data["results"][0]) self.assertNotIn("post", response.data["results"][0]) self.assertIn("post", response.data["results"][1]) self.assertNotIn("article", response.data["results"][1])
def test_correct_notification_type_sent(self): setting = NotificationSetting.objects.get( notification_type=self.notification_type, user=self.receiver) # An email and a push are sent if allow_email and allow_push are True create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) self.assertEqual(len(mail.outbox), 1) self.assertTrue(len(self.mock_submit_to_pushwoosh.mock_calls), 1) # No new email is sent if allow_email is False setting.allow_email = False setting.save() create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) self.assertEqual(len(mail.outbox), 1) self.assertTrue(len(self.mock_submit_to_pushwoosh.mock_calls), 2) # If allow_push is False and allow_email True, an email is sent and a push isn't setting.allow_email = True setting.allow_push = False setting.save() create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) self.assertEqual(len(mail.outbox), 2) self.assertTrue(len(self.mock_submit_to_pushwoosh.mock_calls), 2)
def test_can_only_see_own_notifications(self): create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) create_notification(self.reporter, self.receiver, self.social_obj, self.notification_type) # Notifications serialized with thumbnails post = PostFactory() post.thumbnail = settings.PROJECT_ROOT + "/test_app/tests/img/yeti.jpg" post.save() create_notification(self.receiver, self.reporter, post, self.notification_type) url = reverse("notifications") response = self.assertSchemaGet(url, None, "$notificationResponse", self.receiver) self.assertEqual(response.data["count"], self.receiver.notifications_received.count())
def test_correct_notification_type_sent(self): setting = NotificationSetting.objects.get(notification_type=self.notification_type, user=self.receiver) # An email and a push are sent if allow_email and allow_push are True create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) self.assertEqual(len(mail.outbox), 1) self.assertTrue(len(PushClient.invoke.mock_calls), 1) # No new email is sent if allow_email is False setting.allow_email = False setting.save() create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) self.assertEqual(len(mail.outbox), 1) self.assertTrue(len(PushClient.invoke.mock_calls), 2) # If allow_push is False and allow_email True, an email is sent and a push isn't setting.allow_email = True setting.allow_push = False setting.save() create_notification(self.receiver, self.reporter, self.social_obj, self.notification_type) self.assertEqual(len(mail.outbox), 2) self.assertTrue(len(PushClient.invoke.mock_calls), 2)
def perform_create(self, serializer): obj = serializer.save(user=self.request.user) notification_type = NotificationType.objects.get(slug="share") for receiver in obj.shared_with.all(): create_notification(receiver, obj.user, obj.content_object, notification_type)
def perform_create(self, serializer): obj = serializer.save(user=self.request.user) notification_type = NotificationType.objects.get(slug="follow") create_notification(obj.content_object, obj.user, obj.content_object, notification_type)
def perform_create(self, serializer): obj = serializer.save(user=self.request.user) notification_type = NotificationType.objects.get(slug="like") create_notification(obj.content_object.user, obj.user, obj.content_object, notification_type)