def __init__(self, user_id, redis=None): ''' User id (the user for which we want to read/write notifications) ''' AggregatedFeed.__init__(self, user_id, redis=redis) # location to which we denormalize the count self.count_key = self.count_format % self.format_dict # set the pubsub key if we're using it if self.pubsub_main_channel: self.pubsub_key = sign_value(user_id) self.lock_key = self.lock_format % self.format_dict
def add_many(self, activities): with self.redis.lock(self.lock_key, timeout=2): current_activities = AggregatedFeed.add_many(self, activities) # denormalize the count count = self.denormalize_count(current_activities) # return the current state of the notification feed return current_activities
def test_add_remove(self): ''' Try to remove an aggregated activity ''' loves = Love.objects.all()[:1] feed = AggregatedFeed(13) # slow version activities = [] feed.delete() for love in loves: activity = love.create_activity() activities.append(activity) feed.add(activity) assert feed.contains(activity) aggregated_activity = feed[:10][0] feed.remove(aggregated_activity) assert not feed.contains(activity)
def test_aggregated_feed(self): loves = Love.objects.all()[:10] feed = AggregatedFeed(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)