def _add_love(self, love): ''' We want to write two notifications - someone loved your find - someone loved your love ''' from user.models import UserNotificationSetting feeds = [] activity = love.create_notification_activity() # send notification about the find created_by_id = love.entity.created_by_id if love.user_id != created_by_id: enabled = UserNotificationSetting.objects.enabled_for( created_by_id, LoveVerb) if enabled: feed = NotificationFeed(created_by_id) activity.extra_context['find'] = True logger.info('notifying item finder %s', created_by_id) feed.add(activity) feeds.append(activity) # send notification about the love if love.user_id != love.influencer_id and love.influencer_id: if love.influencer_id != created_by_id: enabled = UserNotificationSetting.objects.enabled_for( love.influencer_id, LoveVerb) if enabled: logger.info('notifying influencer %s', love.influencer_id) feed = NotificationFeed(love.influencer_id) activity.extra_context.pop('find', True) feed.add(activity) feeds.append(feed) return feeds
def _add_love(self, love): ''' We want to write two notifications - someone loved your find - someone loved your love ''' from user.models import UserNotificationSetting feeds = [] activity = love.create_notification_activity() # send notification about the find created_by_id = love.entity.created_by_id if love.user_id != created_by_id: enabled = UserNotificationSetting.objects.enabled_for( created_by_id, LoveVerb) if enabled: feed = NotificationFeed(created_by_id) activity.extra_context['find'] = True logger.info('notifying item finder %s', created_by_id) feed.add(activity) feeds.append(activity) # send notification about the love if love.user_id != love.influencer_id and love.influencer_id: if love.influencer_id != created_by_id: enabled = UserNotificationSetting.objects.enabled_for( love.influencer_id, LoveVerb) if enabled: logger.info('notifying influencer %s', love.influencer_id) feed = NotificationFeed(love.influencer_id) activity.extra_context.pop('find', True) feed.add(activity) feeds.append(feed) return feeds
def _follow(self, follow): ''' Thierry and 3 other people started following you ''' activity = follow.create_activity() feed = NotificationFeed(follow.target_id) feed.add(activity) return feed
def _add_to_list(self, list_item): ''' Guyon added your find to their list back in black Guyon and 3 other people added your finds to their lists ''' activity = list_item.create_activity() user_id = list_item.list.user_id created_by_id = list_item.entity.created_by_id if user_id != created_by_id: feed = NotificationFeed(created_by_id) feed.add(activity) return feed
def _add_to_list(self, list_item): ''' Guyon added your find to their list back in black Guyon and 3 other people added your finds to their lists ''' activity = list_item.create_activity() user_id = list_item.list.user_id created_by_id = list_item.entity.created_by_id if user_id != created_by_id: feed = NotificationFeed(created_by_id) feed.add(activity) return feed
def _follow(self, follow): ''' Thierry and 3 other people started following you ''' from user.models import UserNotificationSetting feed = None enabled = UserNotificationSetting.objects.enabled_for( follow.target_id, FollowVerb) if enabled: activity = follow.create_activity() feed = NotificationFeed(follow.target_id) feed.add(activity) return feed
def _follow(self, follow): ''' Thierry and 3 other people started following you ''' from user.models import UserNotificationSetting feed = None enabled = UserNotificationSetting.objects.enabled_for( follow.target_id, FollowVerb) if enabled: activity = follow.create_activity() feed = NotificationFeed(follow.target_id) feed.add(activity) return feed
def test_mark_all(self): loves = Love.objects.all()[:3] feed = NotificationFeed(13) feed.delete() activities = [l.create_activity() for l in loves] # so we have something to compare to aggregator = RecentVerbAggregator() aggregated_activities = aggregator.aggregate(activities) # insert into the feed feed.add_many(activities) self.assertEqual(feed.count_unseen(), len(aggregated_activities)) # verify if we denormalize correctly self.assertEqual(feed.count_unseen(), feed.get_denormalized_count()) # sanity check self.assertNotEqual(feed.count_unseen(), 0) # Activity gets inserted and marked read # a new activity is appended which updates the last_seen field # the activity is now not seen # # however mark_all will not update # first insert activity = activities[0] activity.time = datetime.datetime.now() feed.add(activity) self.assertNotEqual(feed.count_unseen(), 0) feed.mark_all(seen=True) self.assertEqual(feed.count_unseen(), 0) # check if an updated activity still gets marked import time time.sleep(1) activity.time = datetime.datetime.now() # hack to make sure its duplicate activity.extra_context['foo'] = 'bar' feed.add(activity) self.assertEqual(feed.count_unseen(), 1) # mark as read again feed.mark_all(seen=True) self.assertEqual(feed.count_unseen(), 0)
def test_mark_all(self): loves = Love.objects.all()[:3] feed = NotificationFeed(13) feed.delete() activities = [l.create_activity() for l in loves] # so we have something to compare to aggregator = RecentVerbAggregator() aggregated_activities = aggregator.aggregate(activities) # insert into the feed feed.add_many(activities) self.assertEqual(feed.count_unseen(), len(aggregated_activities)) # verify if we denormalize correctly self.assertEqual(feed.count_unseen(), feed.get_denormalized_count()) # sanity check self.assertNotEqual(feed.count_unseen(), 0) # Activity gets inserted and marked read # a new activity is appended which updates the last_seen field # the activity is now not seen # # however mark_all will not update # verify that we have zero unseen after mark all feed.mark_all(seen=True) self.assertEqual(feed.count_unseen(), 0) # an update to an activity should kick the count back to one activity = activities[0] # check if an updated activity still gets marked import time time.sleep(1) activity.time = datetime.datetime.now() # hack to make sure its not duplicate activity.extra_context['foo'] = 'bar' feed.add(activity) self.assertEqual(feed.count_unseen(), 1) # mark as read again feed.mark_all(seen=True) self.assertEqual(feed.count_unseen(), 0)
def test_notification_feed(self): loves = Love.objects.all()[:10] feed = NotificationFeed(13) # slow version activities = [] feed.delete() for love in loves: activity = Activity(love.user, LoveVerb, love, love.user, time=love.created_at, extra_context=dict(hello='world')) activities.append(activity) feed.add(activity) assert feed.contains(activity) # so we have something to compare to aggregator = RecentVerbAggregator() aggregated_activities = aggregator.aggregate(activities) # check the feed feed_loves = feed[:20] self.assertEqual(len(aggregated_activities), len(feed_loves)) # now the fast version feed.delete() self.assertEqual(int(feed.count()), 0) feed.add_many(activities) for activity in activities: assert feed.contains(activity) # test if we aggregated correctly self.assertEqual(feed.count_unseen(), len(aggregated_activities)) # verify if we denormalize correctly self.assertEqual(feed.count_unseen(), feed.get_denormalized_count()) # sanity check self.assertNotEqual(feed.count_unseen(), 0) # test marking as seen or read feed.mark_all(seen=True) # verify that the new count is 0 self.assertEqual(feed.count_unseen(), 0) # verify if we denormalize correctly self.assertEqual(feed.count_unseen(), feed.get_denormalized_count())
def test_notification_feed(self): loves = Love.objects.all()[:10] feed = NotificationFeed(13) # slow version activities = [] feed.delete() for love in loves: activity = Activity(love.user, LoveVerb, love, love.user, time=love.created_at, extra_context=dict(hello='world')) activities.append(activity) feed.add(activity) assert feed.contains(activity) # so we have something to compare to aggregator = RecentVerbAggregator() aggregated_activities = aggregator.aggregate(activities) # check the feed feed_loves = feed[:20] self.assertEqual(len(aggregated_activities), len(feed_loves)) # now the fast version feed.delete() self.assertEqual(int(feed.count()), 0) feed.add_many(activities) for activity in activities: assert feed.contains(activity) # test if we aggregated correctly self.assertEqual(feed.count_unseen(), len(aggregated_activities)) # verify if we denormalize correctly self.assertEqual(feed.count_unseen(), feed.get_denormalized_count()) # sanity check self.assertNotEqual(feed.count_unseen(), 0) # test marking as seen or read feed.mark_all(seen=True) # verify that the new count is 0 self.assertEqual(feed.count_unseen(), 0) # verify if we denormalize correctly self.assertEqual(feed.count_unseen(), feed.get_denormalized_count())
def test_add_remove(self): ''' Try to remove an aggregated activity ''' from datetime import datetime, time, timedelta love = Love.objects.all()[:1][0] feed = NotificationFeed(13) # slow version activities = [] feed.delete() activity = love.create_activity() activities.append(activity) feed.add(activity) assert feed.contains(activity) # sticking in the same activity with a different time should fail # within the same day should fail activity.time = activity.time - timedelta(seconds=120) try: feed.add(activity) raise ValueError( 'DuplicateActivityException should have been raised') except feedly_exceptions.DuplicateActivityException, e: pass
def test_add_remove(self): ''' Try to remove an aggregated activity ''' from datetime import datetime, time, timedelta love = Love.objects.all()[:1][0] feed = NotificationFeed(13) # slow version activities = [] feed.delete() activity = love.create_activity() activities.append(activity) feed.add(activity) assert feed.contains(activity) # sticking in the same activity with a different time should fail # within the same day should fail activity.time = activity.time - timedelta(seconds=120) try: feed.add(activity) raise ValueError( 'DuplicateActivityException should have been raised') except feedly_exceptions.DuplicateActivityException, e: pass
def test_serialization(self): ''' Test if serialization doesnt take up too much memory ''' notification_feed = NotificationFeed(self.bogus_user.id) notification_feed.delete() love = Love.objects.all()[:10][0] follow = Follow.objects.all()[:10][0] list_item = ListItem.objects.all()[:1][0] notification_feed.add(love.create_activity()) notification_feed.add(follow.create_activity()) notification_feed.add(list_item.create_activity()) size = notification_feed.size() self.assertLess(size, 500)
def test_serialization(self): ''' Test if serialization doesnt take up too much memory ''' notification_feed = NotificationFeed(self.bogus_user.id) notification_feed.delete() love = Love.objects.all()[:10][0] follow = Follow.objects.all()[:10][0] list_item = ListItem.objects.all()[:1][0] notification_feed.add(love.create_activity()) notification_feed.add(follow.create_activity()) notification_feed.add(list_item.create_activity()) size = notification_feed.size() self.assertLess(size, 500)