Mercurial > hg > Feedworm
annotate backend/couchdb/Preferences.py @ 172:214addba1f93
always return an int value for daysToKeepFeedEntries
author | dirk |
---|---|
date | Fri, 09 Sep 2011 17:17:39 +0200 |
parents | 91a24f499318 |
children | 2c91b5653878 |
rev | line source |
---|---|
169
91a24f499318
introdue an abstraction for the name of the database so it can be changed via commandline parameter
dirk
parents:
158
diff
changeset
|
1 |
91a24f499318
introdue an abstraction for the name of the database so it can be changed via commandline parameter
dirk
parents:
158
diff
changeset
|
2 import CouchDb |
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.
dirk
parents:
156
diff
changeset
|
3 |
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.
dirk
parents:
156
diff
changeset
|
4 DAYS_TO_KEEP_FEED_ENTRIES = "daysToKeepFeedEntries" |
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.
dirk
parents:
156
diff
changeset
|
5 HIDE_READ_FEED_ENTRIES = "hideReadFeedEntries" |
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.
dirk
parents:
156
diff
changeset
|
6 PROXY_HOST = "proxyHost" |
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.
dirk
parents:
156
diff
changeset
|
7 PROXY_PORT = "proxyPort" |
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.
dirk
parents:
156
diff
changeset
|
8 SHOW_ONLY_UNREAD_FEEDS = "showOnlyUnreadFeeds" |
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.
dirk
parents:
156
diff
changeset
|
9 START_MAXIMIZED = "startMaximized" |
133
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
10 |
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
11 class Preferences(object): |
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
12 def __init__(self, database): |
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
13 self.database = database |
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.
dirk
parents:
156
diff
changeset
|
14 self._initDocument() |
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.
dirk
parents:
156
diff
changeset
|
15 self.documentIsDirty = False |
133
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
16 |
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.
dirk
parents:
156
diff
changeset
|
17 def _initDocument(self): |
169
91a24f499318
introdue an abstraction for the name of the database so it can be changed via commandline parameter
dirk
parents:
158
diff
changeset
|
18 viewResults = self.database.view(CouchDb.preference()) |
133
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
19 try: |
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.
dirk
parents:
156
diff
changeset
|
20 row = iter(viewResults).next() |
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.
dirk
parents:
156
diff
changeset
|
21 self.document = self.database[row.id] |
133
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
22 except StopIteration: |
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.
dirk
parents:
156
diff
changeset
|
23 empty = { "doctype" : "preferences" } |
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.
dirk
parents:
156
diff
changeset
|
24 doc_id, doc_rev = self.database.save(empty) #@UnusedVariable |
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.
dirk
parents:
156
diff
changeset
|
25 self.document = self.database[doc_id] |
133
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
26 |
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.
dirk
parents:
156
diff
changeset
|
27 def _documentValue(self, key, defaultValue=None): |
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.
dirk
parents:
156
diff
changeset
|
28 if key in self.document.keys(): |
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.
dirk
parents:
156
diff
changeset
|
29 return self.document[key] |
156
2d159eb2a91b
displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
146
diff
changeset
|
30 else: |
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.
dirk
parents:
156
diff
changeset
|
31 return defaultValue |
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.
dirk
parents:
156
diff
changeset
|
32 |
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.
dirk
parents:
156
diff
changeset
|
33 def _setDocumentValue(self, key, value): |
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.
dirk
parents:
156
diff
changeset
|
34 self.document[key] = value |
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.
dirk
parents:
156
diff
changeset
|
35 self.documentIsDirty = True |
156
2d159eb2a91b
displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
146
diff
changeset
|
36 |
133
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
37 def isProxyConfigured(self): |
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
38 return self.proxyHost() is not None |
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
39 |
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
40 def proxyHost(self): |
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.
dirk
parents:
156
diff
changeset
|
41 return self._documentValue(PROXY_HOST) |
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.
dirk
parents:
156
diff
changeset
|
42 |
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.
dirk
parents:
156
diff
changeset
|
43 def setProxyHost(self, hostname): |
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.
dirk
parents:
156
diff
changeset
|
44 if hostname is None: |
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.
dirk
parents:
156
diff
changeset
|
45 if PROXY_HOST in self.document.keys(): |
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.
dirk
parents:
156
diff
changeset
|
46 del self.document[PROXY_HOST] |
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.
dirk
parents:
156
diff
changeset
|
47 self.documentIsDirty = True |
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.
dirk
parents:
156
diff
changeset
|
48 else: |
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.
dirk
parents:
156
diff
changeset
|
49 self._setDocumentValue(PROXY_HOST, hostname) |
133
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
50 |
9e1e6b96d8b0
implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
51 def proxyPort(self): |
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.
dirk
parents:
156
diff
changeset
|
52 return self._documentValue(PROXY_PORT) |
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.
dirk
parents:
156
diff
changeset
|
53 |
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.
dirk
parents:
156
diff
changeset
|
54 def setProxyPort(self, port): |
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.
dirk
parents:
156
diff
changeset
|
55 if port is None: |
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.
dirk
parents:
156
diff
changeset
|
56 if PROXY_PORT in self.document.keys(): |
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.
dirk
parents:
156
diff
changeset
|
57 del self.document[PROXY_PORT] |
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.
dirk
parents:
156
diff
changeset
|
58 self.documenIsDirty = True |
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.
dirk
parents:
156
diff
changeset
|
59 else: |
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.
dirk
parents:
156
diff
changeset
|
60 self._setDocumentValue(PROXY_PORT, port) |
146
8ec20377bcb0
move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
133
diff
changeset
|
61 |
8ec20377bcb0
move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
133
diff
changeset
|
62 def showOnlyUnreadFeeds(self): |
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.
dirk
parents:
156
diff
changeset
|
63 return self._documentValue(SHOW_ONLY_UNREAD_FEEDS, False) |
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.
dirk
parents:
156
diff
changeset
|
64 |
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.
dirk
parents:
156
diff
changeset
|
65 def setShowOnlyUnreadFeeds(self, flag): |
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.
dirk
parents:
156
diff
changeset
|
66 self._setDocumentValue(SHOW_ONLY_UNREAD_FEEDS, flag) |
146
8ec20377bcb0
move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
133
diff
changeset
|
67 |
8ec20377bcb0
move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
133
diff
changeset
|
68 def startMaximized(self): |
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.
dirk
parents:
156
diff
changeset
|
69 return self._documentValue(START_MAXIMIZED, False) |
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.
dirk
parents:
156
diff
changeset
|
70 |
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.
dirk
parents:
156
diff
changeset
|
71 def setStartMaximized(self, flag): |
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.
dirk
parents:
156
diff
changeset
|
72 self._setDocumentValue(START_MAXIMIZED, flag) |
146
8ec20377bcb0
move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
133
diff
changeset
|
73 |
8ec20377bcb0
move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
133
diff
changeset
|
74 def hideReadFeedEntries(self): |
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.
dirk
parents:
156
diff
changeset
|
75 return self._documentValue(HIDE_READ_FEED_ENTRIES, False) |
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.
dirk
parents:
156
diff
changeset
|
76 |
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.
dirk
parents:
156
diff
changeset
|
77 def setHideReadFeedEntries(self, flag): |
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.
dirk
parents:
156
diff
changeset
|
78 self._setDocumentValue(HIDE_READ_FEED_ENTRIES, flag) |
156
2d159eb2a91b
displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
146
diff
changeset
|
79 |
2d159eb2a91b
displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
146
diff
changeset
|
80 def daysToKeepFeedEntries(self): |
172 | 81 value = self._documentValue(DAYS_TO_KEEP_FEED_ENTRIES, 90) |
82 return int(value) | |
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.
dirk
parents:
156
diff
changeset
|
83 |
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.
dirk
parents:
156
diff
changeset
|
84 def setDaysToKeepFeedEntries(self, days): |
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.
dirk
parents:
156
diff
changeset
|
85 self._setDocumentValue(DAYS_TO_KEEP_FEED_ENTRIES, days) |
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.
dirk
parents:
156
diff
changeset
|
86 |
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.
dirk
parents:
156
diff
changeset
|
87 def commit(self): |
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.
dirk
parents:
156
diff
changeset
|
88 if self.documentIsDirty: |
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.
dirk
parents:
156
diff
changeset
|
89 self.database.save(self.document) |
156
2d159eb2a91b
displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
146
diff
changeset
|
90 |
2d159eb2a91b
displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
146
diff
changeset
|
91 def rollback(self): |
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.
dirk
parents:
156
diff
changeset
|
92 if self.documentIsDirty: |
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.
dirk
parents:
156
diff
changeset
|
93 self._initDocument() |