changeset 147:b290e29a94b5

use couchdb's mapping API instead of manually coding around Rows - much leaner code :-)
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 25 Aug 2011 11:05:05 +0200
parents 8ec20377bcb0
children c5a427d46703
files backend/couchdb/Feed.py backend/couchdb/FeedEntry.py backend/couchdb/FeedUpdater.py
diffstat 3 files changed, 42 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/backend/couchdb/Feed.py	Thu Aug 25 07:01:45 2011 +0200
+++ b/backend/couchdb/Feed.py	Thu Aug 25 11:05:05 2011 +0200
@@ -1,34 +1,28 @@
 
+from couchdb.mapping import BooleanField, DateTimeField, Document, IntegerField, TextField
 from datetime import datetime, timedelta
 
-DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
+class Feed(Document):
+    doctype = TextField(default="feed")
+    title = TextField()
+    rss_url = TextField()
+    update_interval = IntegerField(default=60)
+    next_update = DateTimeField()
+    auto_load_entry_link = BooleanField(default=False)
+    always_open_in_browser = BooleanField(default=False)
 
-class Feed(object):
     @staticmethod
     def all(database):
-        viewResults = database.view("feedtest/feeds")
-        return [Feed(row) for row in viewResults]
-
-    def __init__(self, row):
-        self.row = row
-
-    def __getattr__(self, key):
-        return self.row.value[key]
+        return Feed.view(database, "feedtest/feeds")
 
     def needsUpdate(self):
-        updateDate = self._nextUpdateDate()
-        delta = datetime.now() - updateDate
+        delta = datetime.now() - self.next_update
         return delta.total_seconds() > self._updateIntervalInSeconds()
 
-    def incrementedUpdateDate(self):
-        updateDate = self._nextUpdateDate()
+    def incrementNextUpdateDate(self):
         updateInterval = self._updateIntervalInSeconds()
         delta = timedelta(seconds=updateInterval)
-        return updateDate + delta
-
-    def _nextUpdateDate(self):
-        nextUpdateString = self.next_update
-        return datetime.strptime(nextUpdateString, DATE_FORMAT)
+        self.next_update = self.next_update + delta
 
     def _updateIntervalInSeconds(self):
         return self.update_interval * 60
--- a/backend/couchdb/FeedEntry.py	Thu Aug 25 07:01:45 2011 +0200
+++ b/backend/couchdb/FeedEntry.py	Thu Aug 25 11:05:05 2011 +0200
@@ -1,18 +1,24 @@
 
-class FeedEntry(object):
+from couchdb.mapping import BooleanField, DateTimeField, Document, TextField
+from datetime import datetime
+
+class FeedEntry(Document):
+    doctype = TextField(default="feedEntry")
+    create_timestamp = DateTimeField(default=datetime.now())
+    read = BooleanField(default=False)
+    link = TextField()
+    title = TextField()
+    summary = TextField()
+    updated = DateTimeField()
+    feed = TextField()
+
     @staticmethod
     def findByLink(link, database):
-        viewResults = database.view("feedtest/feedEntry_by_link")
-        resultsForKey = viewResults[link]
-        try:
-            row = iter(resultsForKey).next()
-            return FeedEntry(row)
-        except StopIteration:
-            return None
+#        result = FeedEntry.view(database, "feedtest/feedEntry_by_link", { "key" : link })
+#        try:
+#            row = iter(result).next()
+#            return FeedEntry(row)
+#        except StopIteration:
+#            return None
+        return None
 
-    def __init__(self, row):
-        self.row = row
-
-    def __getattr__(self, key):
-        return self.row.value[key]
-
--- a/backend/couchdb/FeedUpdater.py	Thu Aug 25 07:01:45 2011 +0200
+++ b/backend/couchdb/FeedUpdater.py	Thu Aug 25 11:05:05 2011 +0200
@@ -1,8 +1,6 @@
 
 from FeedEntry import FeedEntry
 from backend.AbstractFeedUpdater import AbstractFeedUpdater
-from datetime import datetime
-import Feed
 import logging
 
 log = logging.getLogger("FeedUpdater")
@@ -19,13 +17,14 @@
 
     def _createFeedEntry(self, entry):
         log.info("new feed entry: " + entry.title)
-        feedEntry = { "type" : "feedEntry", "feed" : self.feed._id, "link" : entry.link,
-             "title" : entry.title, "summary" : entry.summary, "read" : False,
-             "updated" : entry.updated}
-        self.database.save(feedEntry)
+        feedEntry = FeedEntry()
+        feedEntry.feed = self.feed.id
+        feedEntry.link = entry.link
+        feedEntry.title = entry.title
+        feedEntry.summary = entry.summary
+        feedEntry.updated = entry.updated_parsed
+        feedEntry.store(self.database)
 
     def _incrementFeedUpdateDate(self):
-        updateDate = self.feed.incrementedUpdateDate()
-        document = self.database.get(self.feed._id)
-        document["next_update"] = datetime.strftime(updateDate, Feed.DATE_FORMAT)
-        self.database.save(document)
+        self.feed.incrementNextUpdateDate()
+        self.feed.store(self.database)