def test_poll_new_and_existing_subscribers(self, mock_parse): """Test a successful polling of a feed.""" # Set up mock data parser = mock.MagicMock() parser.entries = [self.entry1, self.entry2] mock_parse.return_value = parser Subscription(profile=self.profile1, feed=self.feed).save() Subscription(profile=self.profile2, feed=self.feed).save() entry1 = Entry( link=self.entry1.link, feed=self.feed, title=self.entry1.title, published=datetime.datetime(2013, 9, 25, 4, 0), ) entry1.save() UserEntryDetail(profile=self.profile1, entry=entry1).save() # Verify initial state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 1) self.assertEqual(UserEntryDetail.objects.count(), 1) # Perform poll tasks.poll_feed(self.feed) # Verify final state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 2) self.assertEqual(UserEntryDetail.objects.count(), 4)
def subscription(request): profile = request.user.get_profile() if request.method == "GET": subscriptions = Subscription.objects.filter(profile=profile) subscriptions_json = [{'id': s.feed.id, 'title': s.feed.title, 'unread_entries': profile.unread_entries(s.feed)} for s in subscriptions] return HttpResponse(json.dumps(subscriptions_json), content_type='application/json') if request.method == "POST": link = json.loads(request.body)['link'] if not link.startswith('http://'): link = 'http://{}'.format(link) parser = feedparser.parse(link) feed = parser.feed try: title = feed.title except AttributeError: return HttpResponseBadRequest('Invalid feed') try: feed_obj = Feed.objects.get(link=link) except ObjectDoesNotExist: feed_obj = Feed(link=link, title=title) feed_obj.save() if Subscription.objects.filter(profile=profile, feed=feed_obj).exists(): return HttpResponseBadRequest('You are already subscribed to this ' 'feed') Subscription.objects.get_or_create(profile=profile, feed=feed_obj) try: poll_feed(feed_obj) except AttributeError: return HttpResponseBadRequest('Invalid feed') return HttpResponse()
def form_valid(self, form): user = self.request.user link = form.cleaned_data['link'] if not link.startswith('http://'): link = 'http://{}'.format(link) parser = feedparser.parse(link) feed = parser.feed title = feed.title try: feed_obj = Feed.objects.get(link=link) except ObjectDoesNotExist: feed_obj = Feed(link=link, title=title) feed_obj.save() profile = user.get_profile() Subscription(profile=profile, feed=feed_obj).save() poll_feed(feed_obj) return super(DashboardView, self).form_valid(form)
def test_mark_read_unread(self, mock_parse): parser = mock.MagicMock() parser.entries = [self.entry1, self.entry2] mock_parse.return_value = parser unread_entries = self.profile1.unread_entries(self.feed) self.assertEqual(unread_entries, 0) self.profile1.subscribe(self.feed) tasks.poll_feed(self.feed) unread_entries = self.profile1.unread_entries(self.feed) self.assertEqual(unread_entries, 2) self.profile1.mark_read(self.feed) unread_entries = self.profile1.unread_entries(self.feed) self.assertEqual(unread_entries, 0) self.profile1.mark_unread(self.feed) unread_entries = self.profile1.unread_entries(self.feed) self.assertEqual(unread_entries, 2)
def test_poll_new_subscriber(self, mock_parse): """Test a successful polling of a feed.""" # Set up mock data parser = mock.MagicMock() parser.entries = [self.entry1, self.entry2] mock_parse.return_value = parser Subscription(profile=self.profile1, feed=self.feed).save() # Verify initial state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 0) self.assertEqual(UserEntryDetail.objects.count(), 0) # Perform poll tasks.poll_feed(self.feed) # Verify final state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 2) self.assertEqual(UserEntryDetail.objects.count(), 2)
def test_poll_after_unsubscribe(self, mock_parse): # Set up mock data parser = mock.MagicMock() parser.entries = [self.entry1] mock_parse.return_value = parser Subscription(profile=self.profile1, feed=self.feed).save() # Verify initial state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 0) self.assertEqual(UserEntryDetail.objects.count(), 0) # Perform poll tasks.poll_feed(self.feed) # Verify state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 1) self.assertEqual(UserEntryDetail.objects.count(), 1) # Unsubscribe self.profile1.unsubscribe(self.feed) # Verify state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 1) self.assertEqual(UserEntryDetail.objects.count(), 0) # Resubscribe self.profile1.subscribe(self.feed) # Perform poll (find another entry) parser.entries = [self.entry1, self.entry2] tasks.poll_feed(self.feed) # Verify final state self.assertEqual(Feed.objects.count(), 1) self.assertEqual(Entry.objects.count(), 2) self.assertEqual(UserEntryDetail.objects.count(), 2)