def save(self): feed_link = self.cleaned_data['feed'] try: feed = Feed.objects.get(link=feed_link) except Feed.DoesNotExist: fp = feedparser.parse(feed_link) feed = Feed(title=fp.feed.title, link=feed_link, last_updated=None, is_active=True) feed.save() feed.update() #self.account.feeds.add(feed) try: AccountFeed(account=self.account, feed=feed, title=feed.title).save() except: pass return feed
def add_feed(request): account = request.account unread_feeds = account.get_unread_feeds() if request.POST and request.POST.has_key('link'): data = request.POST.copy() try: fp = feedparser.parse(data['link']) if fp.has_key('feed') and fp.feed.has_key('title'): feeds = [data['link']] else: feeds = feedfinder.feeds(data['link']) if not feeds: request.session['error'] = "No feeds found." return HttpResponseRedirect('/feeds/unread/') elif len(feeds) == 1: try: feed = Feed.objects.get(link=feeds[0]) except Feed.DoesNotExist: # add the feed to the db fp = feedparser.parse(feeds[0]) feed = Feed(title=fp.feed.title, link=feeds[0], is_active=True) feed.save() feed.update() else: pass # already added try: AccountFeed(account=account, feed=feed, title=feed.title).save() except: pass return HttpResponseRedirect("/feeds/%s/" % (feed.slug,)) else: if data.has_key('feed'): form = SelectFeedForm(data, account=account, link=data['link'], feeds=feeds) if form.is_valid(): feed = form.save() return HttpResponseRedirect("/feeds/%s/" % (feed.slug,)) else: initial = { 'link': data['link'], 'feed': feeds[0] } form = SelectFeedForm(initial=initial, account=account, link=data['link'], feeds=feeds) return render_to_response('feeds/select.html', { 'feeds': unread_feeds, 'form': form, 'collapse_sidebar': True }, context_instance=RequestContext(request)) except UnicodeDecodeError: request.session['error'] = """ Encoding error. <a href="http://feedparser.org/">Universal Feed Parser</a> threw an exception while trying to make sense of the feed. When this happens it is almost certainly the feed's fault. """.strip().replace('\n', ' ') except feedfinder.TimeoutError: request.session['error'] = "Timeout" # TODO: handle properly return HttpResponseRedirect('/feeds/unread/')
def add_feed(request): account = request.account unread_feeds = account.get_unread_feeds() if request.POST and request.POST.has_key('link'): data = request.POST.copy() try: fp = feedparser.parse(data['link']) if fp.has_key('feed') and fp.feed.has_key('title'): feeds = [data['link']] else: feeds = feedfinder.feeds(data['link']) if not feeds: request.session['error'] = "No feeds found." return HttpResponseRedirect('/feeds/unread/') elif len(feeds) == 1: try: feed = Feed.objects.get(link=feeds[0]) except Feed.DoesNotExist: # add the feed to the db fp = feedparser.parse(feeds[0]) feed = Feed(title=fp.feed.title, link=feeds[0], is_active=True) feed.save() feed.update() else: pass # already added try: AccountFeed(account=account, feed=feed, title=feed.title).save() except: pass return HttpResponseRedirect("/feeds/%s/" % (feed.slug, )) else: if data.has_key('feed'): form = SelectFeedForm(data, account=account, link=data['link'], feeds=feeds) if form.is_valid(): feed = form.save() return HttpResponseRedirect("/feeds/%s/" % (feed.slug, )) else: initial = {'link': data['link'], 'feed': feeds[0]} form = SelectFeedForm(initial=initial, account=account, link=data['link'], feeds=feeds) return render_to_response( 'feeds/select.html', { 'feeds': unread_feeds, 'form': form, 'collapse_sidebar': True }, context_instance=RequestContext(request)) except UnicodeDecodeError: request.session['error'] = """ Encoding error. <a href="http://feedparser.org/">Universal Feed Parser</a> threw an exception while trying to make sense of the feed. When this happens it is almost certainly the feed's fault. """.strip().replace('\n', ' ') except feedfinder.TimeoutError: request.session['error'] = "Timeout" # TODO: handle properly return HttpResponseRedirect('/feeds/unread/')