view backend/couchdb/Preferences.py @ 170:f0afcd1c5656

implement a couchdb mapping field that stores a datetime instance as a JSON array
author dirk
date Fri, 09 Sep 2011 16:22:44 +0200
parents 91a24f499318
children 214addba1f93
line wrap: on
line source


import CouchDb

DAYS_TO_KEEP_FEED_ENTRIES = "daysToKeepFeedEntries"
HIDE_READ_FEED_ENTRIES = "hideReadFeedEntries"
PROXY_HOST = "proxyHost"
PROXY_PORT = "proxyPort"
SHOW_ONLY_UNREAD_FEEDS = "showOnlyUnreadFeeds"
START_MAXIMIZED = "startMaximized"

class Preferences(object):
    def __init__(self, database):
        self.database = database
        self._initDocument()
        self.documentIsDirty = False

    def _initDocument(self):
        viewResults = self.database.view(CouchDb.preference())
        try:
            row = iter(viewResults).next()
            self.document = self.database[row.id]
        except StopIteration:
            empty = { "doctype" : "preferences" }
            doc_id, doc_rev = self.database.save(empty)                 #@UnusedVariable
            self.document = self.database[doc_id]

    def _documentValue(self, key, defaultValue=None):
        if key in self.document.keys():
            return self.document[key]
        else:
            return defaultValue

    def _setDocumentValue(self, key, value):
        self.document[key] = value
        self.documentIsDirty = True

    def isProxyConfigured(self):
        return self.proxyHost() is not None

    def proxyHost(self):
        return self._documentValue(PROXY_HOST)

    def setProxyHost(self, hostname):
        if hostname is None:
            if PROXY_HOST in self.document.keys():
                del self.document[PROXY_HOST]
                self.documentIsDirty = True
        else:
            self._setDocumentValue(PROXY_HOST, hostname)

    def proxyPort(self):
        return self._documentValue(PROXY_PORT)

    def setProxyPort(self, port):
        if port is None:
            if PROXY_PORT in self.document.keys():
                del self.document[PROXY_PORT]
                self.documenIsDirty = True
        else:
            self._setDocumentValue(PROXY_PORT, port)

    def showOnlyUnreadFeeds(self):
        return self._documentValue(SHOW_ONLY_UNREAD_FEEDS, False)

    def setShowOnlyUnreadFeeds(self, flag):
        self._setDocumentValue(SHOW_ONLY_UNREAD_FEEDS, flag)

    def startMaximized(self):
        return self._documentValue(START_MAXIMIZED, False)

    def setStartMaximized(self, flag):
        self._setDocumentValue(START_MAXIMIZED, flag)

    def hideReadFeedEntries(self):
        return self._documentValue(HIDE_READ_FEED_ENTRIES, False)

    def setHideReadFeedEntries(self, flag):
        self._setDocumentValue(HIDE_READ_FEED_ENTRIES, flag)

    def daysToKeepFeedEntries(self):
        return self._documentValue(DAYS_TO_KEEP_FEED_ENTRIES, 90)

    def setDaysToKeepFeedEntries(self, days):
        self._setDocumentValue(DAYS_TO_KEEP_FEED_ENTRIES, days)

    def commit(self):
        if self.documentIsDirty:
            self.database.save(self.document)

    def rollback(self):
        if self.documentIsDirty:
            self._initDocument()