Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.assertTrue(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify subscribers
user = utils.create_user()
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(len(TopicNotification.objects.all()), 2)
self.assertFalse(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify mentions
mentioned = utils.create_user()
mentions = {mentioned.username: mentioned, }
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=mentions)
self.assertEqual(TopicNotification.objects.get(user=mentioned, comment=comment).action, MENTION)
self.assertFalse(TopicNotification.objects.get(user=mentioned, comment=comment).is_read)
# Should mark the topic as unread
user_unread = utils.create_user()
topic = utils.create_topic(self.category)
topic_unread_creator = TopicUnread.objects.create(user=user, topic=topic, is_read=True)
topic_unread_subscriber = TopicUnread.objects.create(user=user_unread, topic=topic, is_read=True)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertTrue(TopicUnread.objects.get(pk=topic_unread_creator.pk).is_read)
self.assertFalse(TopicUnread.objects.get(pk=topic_unread_subscriber.pk).is_read)
# Should increase topic's comment counter
topic = utils.create_topic(self.category)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(Topic.objects.get(pk=topic.pk).comment_count, 1)
self.assertEqual(len(TopicNotification.objects.all()), 1)
self.assertTrue(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify subscribers
user = utils.create_user()
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(len(TopicNotification.objects.all()), 2)
self.assertFalse(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify mentions
mentioned = utils.create_user()
mentions = {mentioned.username: mentioned, }
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=mentions)
self.assertEqual(TopicNotification.objects.get(user=mentioned, comment=comment).action, MENTION)
self.assertFalse(TopicNotification.objects.get(user=mentioned, comment=comment).is_read)
# Should mark the topic as unread
user_unread = utils.create_user()
topic = utils.create_topic(self.category)
topic_unread_creator = TopicUnread.objects.create(user=user, topic=topic, is_read=True)
topic_unread_subscriber = TopicUnread.objects.create(user=user_unread, topic=topic, is_read=True)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertTrue(TopicUnread.objects.get(pk=topic_unread_creator.pk).is_read)
self.assertFalse(TopicUnread.objects.get(pk=topic_unread_subscriber.pk).is_read)
# Should increase topic's comment counter
topic = utils.create_topic(self.category)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
* Should update/create the comment bookmark
* Should mark the topic notification as read
* Should create or mark the topic (unread) as read
* Should increase the view_counter
"""
req = RequestFactory().get('/?page=1')
req.user = self.user
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
comment = utils.create_comment(topic=topic)
notification = TopicNotification.objects.create(user=topic.user, topic=topic, comment=comment, is_read=False)
unread = TopicUnread.objects.create(user=topic.user, topic=topic, is_read=False)
utils_topic.topic_viewed(req, topic)
self.assertEqual(len(CommentBookmark.objects.filter(user=self.user, topic=topic)), 1)
self.assertTrue(TopicNotification.objects.get(pk=notification.pk).is_read)
self.assertTrue(TopicUnread.objects.get(pk=unread.pk).is_read)
self.assertEqual(Topic.objects.get(pk=topic.pk).view_count, 1)
def test_topic_private_notify_access(self):
"""
Should create a notification
"""
private = utils.create_private_topic()
comment = utils.create_comment(topic=private.topic)
notify_access(user=private.user, topic_private=private)
notification = TopicNotification.objects.get(user=private.user, topic=private.topic)
self.assertTrue(notification.is_active)
self.assertFalse(notification.is_read)
self.assertEqual(notification.comment, comment)
* Should update/create the comment bookmark
* Should mark the topic notification as read
* Should create or mark the topic (unread) as read
* Should increase the view_counter
"""
req = RequestFactory().get('/?page=1')
req.user = self.user
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
comment = utils.create_comment(topic=topic)
notification = TopicNotification.objects.create(user=topic.user, topic=topic, comment=comment, is_read=False)
unread = TopicUnread.objects.create(user=topic.user, topic=topic, is_read=False)
utils_topic.topic_viewed(req, topic)
self.assertEqual(len(CommentBookmark.objects.filter(user=self.user, topic=topic)), 1)
self.assertTrue(TopicNotification.objects.get(pk=notification.pk).is_read)
self.assertTrue(TopicUnread.objects.get(pk=unread.pk).is_read)
self.assertEqual(Topic.objects.get(pk=topic.pk).view_count, 1)
self.assertEqual(len(TopicNotification.objects.all()), 1)
self.assertTrue(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify subscribers
user = utils.create_user()
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=None)
self.assertEqual(len(TopicNotification.objects.all()), 2)
self.assertFalse(TopicNotification.objects.get(user=subscriber, topic=self.topic).is_read)
# Should notify mentions
mentioned = utils.create_user()
mentions = {mentioned.username: mentioned, }
comment = utils.create_comment(user=user, topic=self.topic)
comment_posted(comment=comment, mentions=mentions)
self.assertEqual(TopicNotification.objects.get(user=mentioned, comment=comment).action, MENTION)
self.assertFalse(TopicNotification.objects.get(user=mentioned, comment=comment).is_read)
# Should mark the topic as unread
user_unread = utils.create_user()
topic = utils.create_topic(self.category)
topic_unread_creator = TopicUnread.objects.create(user=user, topic=topic, is_read=True)
topic_unread_subscriber = TopicUnread.objects.create(user=user_unread, topic=topic, is_read=True)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
self.assertTrue(TopicUnread.objects.get(pk=topic_unread_creator.pk).is_read)
self.assertFalse(TopicUnread.objects.get(pk=topic_unread_subscriber.pk).is_read)
# Should increase topic's comment counter
topic = utils.create_topic(self.category)
comment = utils.create_comment(user=user, topic=topic)
comment_posted(comment=comment, mentions=None)
"""
Should set is_read=False to all notifiers/users
"""
creator = utils.create_user()
subscriber = utils.create_user()
topic = utils.create_topic(self.category)
comment = utils.create_comment(user=creator, topic=topic)
TopicNotification.objects.create(
user=creator, topic=topic, comment=comment,
is_active=True, is_read=True)
TopicNotification.objects.create(
user=subscriber, topic=topic, comment=comment,
is_active=True, is_read=True)
TopicNotification.notify_new_comment(comment)
notification = TopicNotification.objects.get(
user=subscriber, topic=topic)
self.assertTrue(notification.is_active)
self.assertFalse(notification.is_read)
self.assertEqual(notification.action, COMMENT)
# Author should not be notified of its own comment
notification2 = TopicNotification.objects.get(
user=creator, topic=topic)
self.assertTrue(notification2.is_read)
def test_topic_notification_notify_new_mentions_unactive(self):
"""
set is_read=False when user gets mentioned
even if is_active=False
"""
(TopicNotification.objects
.filter(pk=self.topic_notification.pk)
.update(is_active=False))
mentions = {self.user.username: self.user, }
comment = utils.create_comment(topic=self.topic_notification.topic)
TopicNotification.notify_new_mentions(
comment=comment, mentions=mentions)
self.assertEqual(TopicNotification.objects.get(
pk=self.topic_notification.pk).action, MENTION)
self.assertFalse(TopicNotification.objects.get(
pk=self.topic_notification.pk).is_read)
def render_notification_form(user, topic, next=None):
# TODO: remove form and use notification_activate and notification_deactivate ?
try:
notification = TopicNotification.objects.get(user=user, topic=topic)
except TopicNotification.DoesNotExist:
notification = None
initial = {}
if notification:
initial['is_active'] = not notification.is_active
form = NotificationForm(initial=initial)
return {'form': form, 'topic_id': topic.pk, 'notification': notification, 'next': next}