view backend/couchdb/CouchDbBackend.py @ 158:e8bb107a74e1

all preferences are stored in a single JSON document in the couchdb backend. PreferencesDialog converts QString to a proper Python datatybe before sending it to the backend.
author dirk
date Mon, 29 Aug 2011 02:33:51 +0200
parents a05719a6175e
children 2940e74c9663
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

DATABASE = "feedtest"

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

    def __init__(self):
        server = couchdb.Server()
        self.database = server[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, "feedtest/feedEntries_by_feed",
            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):
        raise Exception("not yet implemented")

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

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

    def entriesForFeed(self, feed, hideReadEntries):
        viewName = "feedtest/feedEntries_by_feed"
        if hideReadEntries:
            viewName = "feedtest/unread_feedEntries_by_feed"
        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(feed, self.database).update()

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