changeset 123:862760b161b4 backend

restructured adding a feed so that only the URL is passed into the backend - the rest of the operation is backend-internal
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 22 Aug 2011 10:30:33 +0200
parents f5afe0c1f4d2
children a4b2077c9603
files AddFeed.py MainWindow.py backend/sqlalchemy/FeedUpdater.py backend/sqlalchemy/SqlAlchemyBackend.py
diffstat 4 files changed, 26 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/AddFeed.py	Sun Aug 21 18:39:02 2011 +0200
+++ b/AddFeed.py	Mon Aug 22 10:30:33 2011 +0200
@@ -14,20 +14,13 @@
 
     def accept(self):
         try:
-            self.createFeed()
-            self.session.commit() # TODO this should be the responsibility of the FeedUpdater
+            url = self.getUrl()
+            self.backend.createFeed(url)
         except AttributeError as ae:
             logging.getLogger("AddFeed").info(ae)
-
-            self.exception = ae
-            self.session.rollback() # TODO this should be the responsibility of the FeedUpdater
+            self.exception = ae       
         QtGui.QDialog.accept(self)
 
-    def createFeed(self):
-        url = self.getUrl()
-        # TODO get status and display in status area of the main window
-        self.backend.feedUpdater().createNewFeed(url)
-
     def getUrl(self):
         text = self.ui.url.text()
         # the content of a QLineEdit is a QString, convert it to a Python string
--- a/MainWindow.py	Sun Aug 21 18:39:02 2011 +0200
+++ b/MainWindow.py	Mon Aug 22 10:30:33 2011 +0200
@@ -149,7 +149,6 @@
             message = "Error while adding feed: " + str(addFeed.exception)
             self._updateStatusBar(message)
         else:
-            self.session.commit()
             self.updateFeedList()
 
     def deleteFeed(self):
--- a/backend/sqlalchemy/FeedUpdater.py	Sun Aug 21 18:39:02 2011 +0200
+++ b/backend/sqlalchemy/FeedUpdater.py	Mon Aug 22 10:30:33 2011 +0200
@@ -33,24 +33,24 @@
         else:
             entry.summary = ""
 
+def createNewFeed(url, session):
+    # when updating to python3 see http://code.google.com/p/feedparser/issues/detail?id=260
+    result = feedparser.parse(url)
+    if result.has_key("title"):
+        title = result["feed"].title
+    else:
+        title = url
+    newFeed = Feed(title, url)
+    session.add(newFeed)
+
+    FeedUpdater(session, newFeed).update()
+
+
 class FeedUpdater(object):
     def __init__(self, session, feed):
         self.session = session
         self.feed = feed
 
-    # TODO this is a HACK! creating new instances from itself is bad but required due to the storage of the session.
-    def createNewFeed(self, url):
-        # when updating to python3 see http://code.google.com/p/feedparser/issues/detail?id=260
-        result = feedparser.parse(url)
-        if result.has_key("title"):
-            title = result["feed"].title
-        else:
-            title = url
-        newFeed = Feed(title, url)
-        self.session.add(newFeed)
-
-        FeedUpdater(self.session, newFeed).update()
-
     def update(self):
         log.info("updating " + self.feed.rss_url)
         result = self.getFeed()
--- a/backend/sqlalchemy/SqlAlchemyBackend.py	Sun Aug 21 18:39:02 2011 +0200
+++ b/backend/sqlalchemy/SqlAlchemyBackend.py	Mon Aug 22 10:30:33 2011 +0200
@@ -1,10 +1,10 @@
 
 from Preferences import Preferences
-from FeedUpdater import FeedUpdater
 import Database
+import FeedList
+import FeedUpdater
 import logging
 import util
-import FeedList
 
 class SqlAlchemyBackend(object):
     '''
@@ -28,11 +28,6 @@
     def preferences(self):
         return self.prefs
 
-    def feedUpdater(self):
-        if self.updater is None:
-            self.updater = FeedUpdater(self.session)
-        return self.updater
-
     def getFeeds(self):
         return FeedList.getFeeds(self.session)
 
@@ -44,6 +39,14 @@
         feed.markAllEntriesRead()
         self.session.commit()
 
+    def createFeed(self, url):
+        try:
+            FeedUpdater.createNewFeed(url, self.session)
+            self.session.commit()
+        except AttributeError as ae:
+            self.session.rollback()
+            raise ae
+
     def dispose(self):
         # save all uncommitted state, just in case
         self.session.commit()