view backend/couchdb/CouchDbBackend.py @ 172:214addba1f93

always return an int value for daysToKeepFeedEntries
author dirk
date Fri, 09 Sep 2011 17:17:39 +0200
parents 91a24f499318
children d0ced79b5030
line wrap: on
line source


from FeedUpdater import FeedUpdater
from Preferences import Preferences
from backend.AbstractBackend import AbstractBackend
from backend.couchdb.Feed import Feed
from backend.couchdb.FeedEntry import FeedEntry
import couchdb
from backend.couchdb import CouchDb

class CouchDbBackend(AbstractBackend):
    '''
    Backend that uses CouchDB for persistence
    '''

    def __init__(self):
        CouchDb.init()
        server = couchdb.Server()
        self.database = server[CouchDb.database]
        self.prefs = None

    def preferences(self):
        if self.prefs is None:
            self.prefs = Preferences(self.database)
        return self.prefs

    #
    # handling of feeds
    #

    def getFeeds(self):
        if self.preferences().showOnlyUnreadFeeds():
            self.feeds = self._getUnreadFeeds()
        else:
            # make sure that the results are actually fetched into memory, otherwise we'll pass
            # a ViewResults instance around which is not what we want
            self.feeds = list(Feed.all(self.database))
        return self.feeds

    def _getUnreadFeeds(self):
        raise Exception("not yet implemented")

    def _retrieveEntriesForSelectedFeed(self, hideReadEntries):
        # TODO how to hide read entries if requested?
        viewResults = FeedEntry.view(self.database, CouchDb.feedEntriesByFeed(),
            key=self.selectedFeed.id)
        return list(viewResults)

    def markSelectedFeedAsRead(self):
        for feedEntry in self.entriesForSelectedFeed():
            feedEntry.markRead(self.database)

    #
    # handling of the selected feed entry
    #

    def _markSelectedFeedEntryRead(self):
        self.selectedFeedEntry.markRead(self.database)

    def toggleSelectedFeedEntryRead(self):
        self.selectedFeedEntry.toggleRead(self.database)


    def toggleRead(self, feedEntry):
        raise Exception("not yet implemented")

    def markAllEntriesRead(self, feed):
        raise Exception("not yet implemented")

    def createFeed(self, url):
        feed = Feed.create(url)
        feed.store(self.database)
        FeedUpdater(self.database, self.preferences()).update(feed)

    def updateFeed(self, feed, changes):
        for key in changes.keys():
            feed[key] = changes[key]
        feed.store(self.database)

    def deleteSelectedFeed(self):
        viewResults = self.database.view(CouchDb.feedEntriesByFeed(), key=self.selectedFeed.id)
        for row in viewResults:
            del self.database[row.id]
        del self.database[self.selectedFeed.id]

    def entriesForFeed(self, feed, hideReadEntries):
        viewName = CouchDb.feedEntriesByFeed()
        if hideReadEntries:
            viewName = CouchDb.unreadFeedEntriesByFeed()
        return list(FeedEntry.view(self.database, viewName))

    def markFeedEntriesAsRead(self, indices):
        for index in indices:
            feedEntry = self.entriesForSelectedFeed()[index]
            feedEntry.markRead(self.database)

    def updateAllFeeds(self):
        # TODO use a view instead of iterating all feeds
        allFeeds = Feed.all(self.database)
        for feed in allFeeds:
            if feed.needsUpdate():
                FeedUpdater(self.database, self.preferences()).update(feed)

    def expireFeedEntries(self):
        print("Expiring feeds is not yet implemented")
#        raise Exception("not yet implemented")